LL(1) parsing

10.2.3 LL(1) parsing

  This subsection defines a function ll1 that takes a grammar and a terminal string as input, and returns one tuple: a parse tree, and the rest of the inputstring that has not been parsed. So ll1 takes a grammar, and returns a parser with Rose s as its result type. As mentioned in the beginning of this section, our parser doesn’t need backtracking anymore, since parsing with an LL(1) grammar is deterministic. Therefore, the parser type is adjusted as follows:

  type Parser b a = [b] -> (a,[b]) Using this parser type, the type of the function ll1 is:

  ll1 :: (Symbol s, Ord s) => CFG s -> Parser s (Rose s)

  LL Parsing

  Function ll1 is defined in terms of two functions. Function isll1 :: CFG s -> Bool is a function that checks whether or not a grammar is LL(1). And function gll1 (for generalised LL(1)) produces a list of rose trees for a list of symbols. ll1 is obtained from gll1 by giving gll1 the singleton list containing the start symbol of the grammar as argument.

  ll1 grammar input =

  if isll1 grammar then let ([rose], rest) = gll1 grammar [start grammar] input

  in (rose, rest) else error "ll1: grammar not LL(1)"

  So now we have to implement functions isll1 and gll1. Function isll1 is imple- mented in the following subsection. Function gll1 also uses two functions. Function grammar2ll1table takes a grammar and returns the LL(1) table: the association list that associates productions with their lookahead sets. And function choose takes a terminal symbol, and chooses a production based on the LL(1) table.

  gll1 :: (Symbol s, Ord s) => CFG s -> [s] -> Parser s [Rose s] gll1 grammar =

  let ll1table = grammar2ll1table grammar

  -- The LL(1) table. nt2prods nt = filter (\((n,l),r) -> n==nt) ll1table

  -- nt2prods returns the productions for nonterminal -- nt from the LL(1) table

  selectprod nt t = choose t (nt2prods nt)

  -- selectprod selects the production for nt from the -- LL(1) table that should be taken when t is the next -- symbol in the input.

  in \stack input ->

  case stack of

  -> ([], input) (s:ss) ->

  if isT s then -- match

  let (rts,rest) = gll1 grammar ss (tail input) in if s == head input

  then (Node s []: rts, rest)

  -- The parse tree is a leaf (a node with -- no children).

  else ([Nil], input)

  -- The input cannot be parsed

  else -- expand

  let t = head input

  (rts,zs) = gll1 grammar (selectprod s t) input

  -- Try to parse according to the production -- obtained from the LL(1) table from s.

  (rrs,vs) = gll1 grammar ss zs

  -- Parse the rest of the symbols on the -- stack.

  10.2 LL Parsing: Implementation

  in ((Node s rts): rrs, vs)

  Functions grammar2ll1table and choose, which are used in the above function gll1, are defined as follows. These functions use function lookaheadp, which returns the lookahead set of a production and is defined in one of the following subsections.

  grammar2ll1table :: (Symbol s, Ord s) => CFG s -> [((s,[s]),[s])] grammar2ll1table grammar =

  map (\x -> (x,lookaheadp grammar x)) (prods grammar) choose

  :: Eq a => a -> [((b,c),[a])] -> c

  choose t l =

  let [((s,rhs), ys)] = filter (\((x,p),q) -> t ‘elem‘ q) l in rhs

  Note that function choose assumes that there is exactly one element in the associ- ation list in which the element t occurs.

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