Parser combinators for EBNF

3.4.1 Parser combinators for EBNF

  It is very easy to make new parser combinators for EBNF. As a first example we consider repetition. Given a parser for a construction, many constructs a parser for zero or more occurrences of that construction:

  many

  :: Parser s a -> Parser s [a] many p = list <> p <> many p

  <|> succeed []

  So the EBNF expression P ∗ is implemented by many P. The auxiliary function list takes an element and a list, and combines them in a simple list:

  list x xs = x:xs So list is just (:), and we might have written that in the definition of many, but

  then the definition might have looked too cryptic at first sight. The order in which the alternatives are given only influences the order in which

  solutions are placed in the list of successes. For example, the many combinator can be used in parsing a natural number:

  natural

  :: Parser Char Int

  natural

  = foldl f 0 <> many newdigit

  where

  f a b = a10 + b

  Defined in this way, the natural parser also accepts empty input as a number. If this is not desired, we had better use the many1 parser combinator, which accepts one or more occurrences of a construction, and corresponds to the EBNF expression P + , see Section 2.7. It is defined in listing 5.

  Another combinator from EBNF is the option combinator P ?. It takes a parser as argument, and returns a parser that recognises the same construct, but which also succeeds if that construct is not present in the input string. The definition is given

  Parser combinators

  -- EBNF parser combinators option

  :: Parser s a -> a -> Parser s a

  option p d = p <|> succeed d many

  :: Parser s a -> Parser s [a] many p = list <> p <> many p <|> succeed []

  many1

  :: Parser s a -> Parser s [a]

  many1 p = list <> p <> many p

  pack

  :: Parser s a -> Parser s b -> Parser s c -> Parser s b

  pack p r q = (\x y z -> y) <> p <> r <> q listOf

  :: Parser s a -> Parser s b -> Parser s [a]

  listOf p s = list <> p <> many ((\x y -> y) <> s <> p) -- Auxiliary functions first :: Parser s b -> Parser s b

  first p xs | null r

  | otherwise = [head r]

  where r = p xs greedy, greedy1 :: Parser s b -> Parser s [b]

  greedy

  = first . many

  greedy1 = first . many1 list x xs = x:xs

  Listing 5: EBNF.hs

  3.4 More parser combinators

  in listing 5. It has an additional argument: the value that should be used as result in case the construct is not present. It is a kind of ‘default’ value.

  By the use of the option and many functions, a large amount of backtracking possi- bilities are introduced. This is not always advantageous. For example, if we define

  a parser for identifiers by

  identifier = many1 (satisfy isAlpha)

  a single word may also be parsed as two identifiers. Caused by the order of the alternatives in the definition of many (succeed [] appears as the second alternative), the ‘greedy’ parsing, which accumulates as many letters as possible in the identifier is tried first, but if parsing fails elsewhere in the sentence, also less greedy parsings of the identifier are tried – in vain. You will give a better definition of identifier in Exercise 3.26.

  In situations where from the way the grammar is built we can predict that it is hopeless to try non-greedy results of many, we can define a parser transformer first, that transforms a parser into a parser that only returns the first possible parsing. It does so by taking the first element of the list of successes.

  first :: Parser a b -> Parser a b first p xs | null r

  | otherwise = [head r]

  where r = p xs Using this function, we can create a special ‘take all or nothing’ version of many:

  greedy

  = first . many

  greedy1 = first . many1 If we compose the first function with the option parser combinator:

  obligatory p d = first (option p d) we get a parser which must accept a construction if it is present, but which does not

  fail if it is not present.

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