Implementation of follow

10.2.8 Implementation of follow

  Finally, the last function we have to implement is the function follow, which takes

  a grammar, and returns an association list in which nonterminals are associated to symbols that can follow upon the nonterminal in a derivation. A nonterminal n is associated to a list containing terminal t in follow if n and t follow each other in some sequence of symbols occurring in some leftmost derivation. We can compute pairs of such adjacent symbols by splitting up the right-hand sides with length at least 2 and, using lasts and firsts, compute the symbols which appear at the end resp. at the beginning of strings which can be derived from the left resp. right part of the split alternative. Our grammar exGrammar has three alternatives with length at least 2: "AaS", "CB" and "SC". Function follow uses the functions firsts and lasts and the predicate isEmpty for intermediate results. The previous subsections explain how to compute these functions.

  Let’s see what happens with the alternative "AaS". The lists of all nonterminal symbols that can appear at the end of sequences of symbols derived from "A" and "Aa" are "ADC" and "" respectively. The lists of all terminal symbols which can appear at the beginning of sequences of symbols derived from "aS" and "S" are "a" and "dba" respectively. Zipping together those lists shows that an ’A’, a ’D’ and

  a ’C’ can be be followed by an ’a’. Splitting the alternative "CB" in the middle produces first and last sets "CD" and "dba". Splitting the alternative "SC" in the middle produces first and last sets "SDCAB" and "d". From the first pair we can see that a ’C’ and a ’D’ can be followed by a ’d’, a ’b’ and an ’a’ From the second pair we see that an ’S’, a ’D’, a ’C’, an ’A’, and a ’B’ can be followed by a ’d’. Combining all these results gives:

  [(’S’,"d"),(’A’,"ad"),(’B’,"d"),(’C’,"adb"),(’D’,"adb")] The function follow uses the functions scanrAlt and scanlAlt. The lists produced

  by these functions are exactly the ones we need: using the function zip from the standard prelude we can combine the lists. For example: for the alternative "AaS" the functions scanlAlt and scanrAlt produce the following lists:

  [[], "ADC", \es, "SDCAB"] ["dba", "a", "dba", []]

  Only the two middle elements of both lists are important (they correspond to the nontrivial splittings of "AaS"). Thus, we only have to consider alternatives of length at least 2. We start the computation of follow with assigning the empty follow set to each symbol:

  follow :: (Symbol s, Ord s) => CFG s -> [(s,[s])] follow grammar = combine (followNE grammar) (startEmpty grammar)

  LL Parsing

  startEmpty grammar = map (\x -> (x,[])) (symbols grammar) The real work is done by functions followNE and function splitProds. Func-

  tion followNE passes the right arguments on to function splitProds, and removes all nonterminals from the first set, and all terminals from the last set. Function splitProds splits the productions of length at least 2, and pairs the last nontermi- nals with the first terminals.

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

  where isTfirsts = map (\(x,xs) -> (x,filter isT xs)) . firsts

  isNlasts = map (\(x,xs) -> (x,filter isN xs)) . lasts splitProds :: (Symbol s, Ord s) =>

  [(s,[s])]

  -- productions

  (s -> Bool)

  -- isEmpty

  [(s,[s])]

  -- terminal firsts

  [(s,[s])]

  -- nonterminal lasts

  [(s,[s])]

  splitProds prods p fset lset =

  map (\(nt,rhs) -> (nt,nub rhs)) (group pairs) where pairs = [(l, f)

  | rhs <- map snd prods , length rhs >= 2 , (fs, ls) <- zip (rightscan rhs) (leftscan rhs) , l <- ls , f <- fs ]

  leftscan = scanlRhs p (lset?) [] rightscan = scanrRhs p (fset?) []

  Exercise 10.7 . Give the Rose tree representation of the parse tree corresponding to the deriva-

  tion of the sentence ccccba using grammar gramm1.

  Exercise 10.8 . Give the Rose tree representation of the parse tree corresponding to the deriva-

  tion of the sentence abbb using grammar gramm2’ defined in the exercises of the previous section .

  Exercise 10.9 . Give the Rose tree representation of the parse tree corresponding to the deriva-

  tion of the sentence acbab using grammar gramm3.

  Exercise 10.10 . In this exercise we will take a closer look at the functions foldrRhs and

  scanrRhs which are the essential ingredients of the implementation of the grammar analysis algorithms. From the definitions it is clear that grammar analysis is easily expressed via a calculus for (finite) sets. A calculus for finite sets is implicit in the programs for LL(1) parsing. Since the code in this module is obscured by several

  10.2 LL Parsing: Implementation

  implementation details we will derive the functions foldrRhs and scanrRhs in a stepwise fashion. In this derivation we will use the following:

  A (finite) set is implemented by a list with no duplicates. In order to construct a set, the following operations may be used:

  the empty set of a-elements

  union ::

  [a] → [a] → [a]

  the union of two sets

  unions ::

  [[a]] → [a]

  the generalised union

  single ::

  a → [a]

  the singleton function

  These operations satisfy the well-known laws for set operations.

  1. Define a function list2Set :: [a] → [a] which returns the set of elements occurring in the argument.

  2. Define list2Set as a foldr.

  3. Define a function pref p :: [a] → [a] which given a list xs returns the set of elements corresponding to the longest prefix of xs all of whose elements satisfy p.

  4. Define a function prefplus p :: [a] → [a] which given a list xs returns the set of elements in the longest prefix of xs all of whose elements satisfy p together with the first element of xs that does not satisfy p (if this element exists at all).

  5. Define prefplus p as a foldr.

  6. Show that prefplus p = foldrRhs p single [].

  7. It can be shown that

  foldrRhs p f [] = unions . map f . prefplus p for all set-valued functions f. Give an informal description of the functions

  foldrRhs p f [] and foldrRhs p f start.

  8. The standard function scanr is defined by

  scanr f q0 = map (foldr f q0) . tails where tails is a function which takes a list xs and returns a list with all

  tailsegments (postfixes) of xs in decreasing length. The function scanrRhs is defined is a similar way

  scanrRhs p f start = map (foldrRhs p f start) . tails Give an informal description of the function scanrRhs.

  LL Parsing

  Exercise 10.11 . The computation of the functions empty and firsts is not restricted to

  nonterminals only. For terminal symbols s these functions are defined by empty s = False

  firsts s = {s} Using the definitions in the previous exercise, compute the following.

  1. For the example grammar gramm1 and two of its productions A → bSA and

  B → Cb. (a) foldrRhs empty firsts [] bSA

  (b) foldrRhs empty firsts [] Cb

  2. For the example grammar gramm3 and its production S → AaS

  (a) foldrRhs empty firsts [] AaS (b) scanrRhs empty firsts [] AaS

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