Implementation of empty

10.2.6 Implementation of empty

  Many functions defined in this chapter make use of a predicate isEmpty, which tests whether or not the empty sequence can be derived from a nonterminal. This subsection defines this function. Consider the grammar exGrammar. We are now only interested in deriving sequences which contain only nonterminal symbols (since it is impossible to derive the empty string if a terminal occurs). Therefore we only have to consider the productions in which no terminal symbols appear in the right-hand sides.

  C →D One can immediately see form those productions that the nonterminal A derives the

  empty string in one step. To know whether there are any nonterminals which derive

  LL Parsing

  the empty string in more than one step we eliminate the productions for A and we eliminate all occurrences of A in the right hand sides of the remaining productions

  S → B | CB

  B →

  C →D One can now conclude that the nonterminal B derives the empty string in two steps.

  Doing the same with B as we did with A gives us the following productions

  S → |C

  C →D One can now conclude that the nonterminal S derives the empty string in three steps.

  Doing the same with S as we did with A and B gives us the following productions

  C →D At this stage we can conclude that there are no more new nonterminals which derive

  the empty string. We now give the Haskell implementation of the algorithm described above. The

  algorithm is iterative: it does the same steps over and over again until some desired condition is met. For this purpose we use function fixedPoint, which takes a function and a set, and repeatedly applies the function to the set, until the set does not change anymore.

  fixedPoint

  :: Ord a => ([a] -> [a]) -> [a] -> [a]

  fixedPoint f xs | xs == nexts = xs

  | otherwise

  = fixedPoint f nexts

  where nexts = f xs fixedPoint f is sometimes called the fixed-point of f. Function isEmpty determines

  whether or not a nonterminal can derive the empty string. A nonterminal can derive the empty string if it is a member of the emptySet of a grammar.

  isEmpty

  :: (Symbol s, Ord s) => CFG s -> s -> Bool

  isEmpty grammar = (‘elem‘ emptySet grammar) The emptySet of a grammar is obtained by the iterative process described in the

  example above. We start with the empty set of nonterminals, and at each step n of the computation of the emptySet as a fixedPoint, we add the nonterminals that can derive the empty string in n steps. Function emptyStepf adds a nonterminal if there exists a production for the nonterminal of which all elements can derive the empty string.

  emptySet :: (Symbol s, Ord s) => CFG s -> [s] emptySet grammar = fixedPoint (emptyStepf grammar) []

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

  nub (map fst (filter (\(nt,rhs) -> all (‘elem‘ set) rhs)

  (prods grammar)

  10.2 LL Parsing: Implementation

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