Arithmetical expressions

3.5 Arithmetical expressions

  The goal of this section is to use parser combinators in a concrete application. We will develop a parser for arithmetical expressions, which have the following concrete syntax:

  E → E + E | E - E | E E | E E | (E) | Digs Besides these productions, we also have productions for identifiers and applications

  of functions:

  E → Identifier | Identifier (LoA)

  LoA → | E(,E) ∗

  The parse trees for this grammar are of type Expr:

  data Expr = Con Int

  | Var String | Fun String [Expr] | Expr :+: Expr | Expr :-: Expr | Expr :: Expr | Expr :: Expr

  You can almost recognise the structure of the parser in this type definition. But in order to account for the priorities of the operators, we will use a grammar with three non-terminals ‘expression’, ‘term’ and ‘factor’: an expression is composed of terms separated by + or −; a term is composed of factors separated by ∗ or , and

  a factor is a constant, variable, function call, or expression between parentheses. This grammar appears as a parser in the functions in listing 7. The first parser,

  fact, parses factors.

  fact :: Parser Char Expr fact = Con <> integer

  <|> Var <> identifier <|> Fun <> identifier <> parenthesised (commaList expr) <|> parenthesised expr

  The first alternative is an integer parser which is postprocessed by the ‘semantic function’ Con. The second and third alternative are a variable or function call, depending on the presence of an argument list. In absence of the latter, the function Var is applied, in presence the function Fun. For the fourth alternative there is no semantic function, because the meaning of an expression between parentheses is the meaning of the expression.

  For the definition of a term as a list of factors separated by multiplicative operators we use the function chainr. Recall that chainr repeatedly recognises its first argu- ment (fact), separated by its second argument (a ∗ or ). The parse trees for the individual factors are joined by the constructor functions that appear before <>.

  The function expr is analogous to term, only with additive operators instead of multiplicative operators, and with terms instead of factors.

  3.5 Arithmetical expressions

  -- Type definition for parse tree data Expr = Con Int

  | Var String | Fun String [Expr] | Expr :+: Expr | Expr :-: Expr | Expr :: Expr

  ------------------------------------------------------------- -- Parser for expressions with two priorities

  fact :: Parser Char Expr fact = Con <> integer

  <|> Var <> identifier <|> Fun <> identifier <> parenthesised (commaList expr) <|> parenthesised expr

  integer :: Parser Char Int integer = (const negate <> (symbol ’-’)) ‘option‘ id <> natural

  term :: Parser Char Expr term = chainr fact

  const (::) <> symbol ’’ <|> const (::) <> symbol ’’ )

  expr :: Parser Char Expr expr = chainr term

  const (:+:) <> symbol ’+’ <|> const (:-:) <> symbol ’-’ )

  Listing 7: ExpressionParser.hs

  Parser combinators

  This example clearly shows the strength of parsing with parser combinators. There is no need for a separate formalism for grammars; the production rules of the grammar are combined with higher-order functions. Also, there is no need for a separate parser generator (like ‘yacc’); the functions can be viewed both as description of the grammar and as an executable parser.

  Exercise 3.27 . Modify the functions in listing 7, in such a way that + is parsed as a right

  associative operator, and − is parsed as a left associative operator.

Dokumen yang terkait

Analisis Komparasi Internet Financial Local Government Reporting Pada Website Resmi Kabupaten dan Kota di Jawa Timur The Comparison Analysis of Internet Financial Local Government Reporting on Official Website of Regency and City in East Java

19 819 7

ANTARA IDEALISME DAN KENYATAAN: KEBIJAKAN PENDIDIKAN TIONGHOA PERANAKAN DI SURABAYA PADA MASA PENDUDUKAN JEPANG TAHUN 1942-1945 Between Idealism and Reality: Education Policy of Chinese in Surabaya in the Japanese Era at 1942-1945)

1 29 9

Improving the Eighth Year Students' Tense Achievement and Active Participation by Giving Positive Reinforcement at SMPN 1 Silo in the 2013/2014 Academic Year

7 202 3

Improving the VIII-B Students' listening comprehension ability through note taking and partial dictation techniques at SMPN 3 Jember in the 2006/2007 Academic Year -

0 63 87

The Correlation between students vocabulary master and reading comprehension

16 145 49

The correlation intelligence quatient (IQ) and studenst achievement in learning english : a correlational study on tenth grade of man 19 jakarta

0 57 61

An analysis of moral values through the rewards and punishments on the script of The chronicles of Narnia : The Lion, the witch, and the wardrobe

1 59 47

Improping student's reading comprehension of descriptive text through textual teaching and learning (CTL)

8 140 133

The correlation between listening skill and pronunciation accuracy : a case study in the firt year of smk vocation higt school pupita bangsa ciputat school year 2005-2006

9 128 37

Transmission of Greek and Arabic Veteri

0 1 22