Implementation of first and last

10.2.7 Implementation of first and last

  Function firsts takes a grammar, and returns for each symbol of the grammar (so also the terminal symbols) the set of terminal symbols with which a sentence derived from that symbol can start. The first set of a terminal symbol is the terminal symbol itself.

  The first set of each symbol consists of that symbol itself, plus the (first) symbols that can be derived from that symbol in one or more steps. So the first set can be computed by an iterative process, just as the function isEmpty.

  Consider the grammar exGrammar again. We start the iteration with

  [(’S’,"S"),(’A’,"A"),(’B’,"B"),(’C’,"C"),(’D’,"D") ,(’a’,"a"),(’b’,"b"),(’d’,"d") ]

  Using the productions of the grammar we can derive in one step the following lists of first symbols.

  [(’S’,"ABC"),(’A’,"S"),(’B’,"Ab"),(’C’,"D"),(’D’,"d")] and the union of these two lists is

  [(’S’,"SABC"),(’A’,"AS"),(’B’,"BAb"),(’C’,"CD"),(’D’,"Dd") ,(’a’,"a"),(’b’,"b"),(’d’,"d")]

  In two steps we can derive

  [(’S’,"SAbD"),(’A’,"ABC"),(’B’,"S"),(’C’,"d"),(’D’,"")] and again we have to take the union of this list with the previous result. We repeat

  this process until the list doesn’t change anymore. For exGrammar this happens when:

  [(’S’,"SABCDabd") ,(’A’,"SABCDabd") ,(’B’,"SABCDabd") ,(’C’,"CDd") ,(’D’,"Dd") ,(’a’,"a") ,(’b’,"b") ,(’d’,"d") ]

  Function firsts is defined as the fixedPoint of a step function that iterates the first computation one more step. The fixedPoint starts with the list that contains all symbols paired with themselves.

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

  fixedPoint (firstStepf grammar) (startSingle grammar) startSingle :: (Ord s, Symbol s) => CFG s -> [(s,[s])]

  startSingle grammar = map (\x -> (x,[x])) (symbols grammar)

  LL Parsing

  The step function takes the old approximation and performs one more iteration step. At each of these iteration steps we have to add the start list with which the iteration started again.

  firstStepf :: (Ord s, Symbol s) =>

  CFG s -> [(s,[s])] -> [(s,[s])]

  firstStepf grammar approx =

  (startSingle grammar)

  ‘combine‘ (compose (first1 grammar) approx)

  combine :: Ord s => [(s,[s])] -> [(s,[s])] -> [(s,[s])] combine xs = foldr insert xs

  where insert (a,bs) [] = [(a,bs)]

  insert (a,bs) ((c,ds):rest)

  | a == c

  = (a, union bs ds) : rest

  | otherwise = (c,ds) : (insert (a,bs) rest)

  compose :: Ord a => [(a,[a])] -> [(a,[a])] -> [(a,[a])] compose r1 r2 = [(a, unions (map (r2?) bs)) | (a,bs) <- r1]

  Finally, function first1 computes the direct first symbols of all productions, taking into account that some nonterminals can derive the empty string, and combines the results for the different nonterminals.

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

  map (\(nt,fs) -> (nt,unions fs))

  (group (map (\(nt,rhs) -> (nt,foldrRhs (isEmpty grammar)

  single [] rhs

  ) (prods grammar)

  where group groups elements with the same first element together

  group :: Eq a => [(a,b)] -> [(a,[b])] group = foldr insertPair []

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

  = [(a,[b])]

  insertPair (a,b) ((c,ds):rest) =

  if a==c then (c,(b:ds)):rest else (c,ds):(insertPair (a,b) rest) function single takes an element and returns the set with the element, and unions

  returns the union of a set of sets. Function lasts is defined using function firsts. Suppose we reverse the right-hand

  sides of all productions of a grammar. Then the first set of this reversed grammar is the last set of the original grammar. This idea is implemented in the following functions.

  10.2 LL Parsing: Implementation

  reverseGrammar :: Symbol s => CFG s -> CFG s reverseGrammar =

  \(s,al) -> (s,map (\(nt,rhs) -> (nt,reverse rhs)) al) lasts :: (Symbol s, Ord s) => CFG s -> [(s,[s])]

  lasts = firsts . reverseGrammar

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