You are on page 1of 7
Chapter 5 LL Parsing 5.1 LL(k) Grammars 5.1.1 Introduction © Subset of CEG’s ‘© Permit deterministic left-to-right top-down recognition with a look ahead of k symbols, * Building a tree top down «If the correct production can be deduced from the partially constructed tree and the next k symbols in the wascanned string, for every possible top-down parsing step, then the grammar is said to be LL(k) © Ifa parse table can be constructed for a grammar then it is LIL(k), if it can’t, its not LL(k) 5.1.2 Properties of LL(k) 1, Bach LL(k) grammar is unambiguous 2. An EL(k) grammar has no left-recursion © Why is this a problem? © Keep expanding top nonterminal on the stack without consuming any input © An erroneous string will cause this to happen 5.1.8 Problems in LL(k) parsing 1, Left recursion 2. Order of alternatives is important 3. Failure LL Parsing Page 56 [Algorithm 5.1 Elimination of Left Recursi { Restrictions: Grammar has no cycles (A =>* A) or e-productions. } Arrange non-torminals of G in some order At, A2, 5 An for i:= 1tondo for j:=1te i-1do Replace each production of the form A; -+ Aj7 by the production Aj—+ & 7 | 627 || =| 647%, where A; > 6 | 2 | | 6 are all the current A;-productions end for Eliminate the immediate left-recursion among the Aj-productions end for yo in LE grammars ‘igure 5.1: Algorithm for the elimination of left recu 5.1.4 Left Recursion ‘© Consider the problems with L1(k) parsers. Consider the grammar A> f| Aa and try to parse the string fi a. — Show the string fa — Show the erroncous string a - gets into an infinite loop ‘¢ If we removed the left recursion the grammar becomes As pal As ad’c which derives the same string but towards the sight instead of the left # Now parse a a. doosn’t match a, therefore try another alternative for A. There are none, so parse fails. With right recursion, we will be matching part of the input string as we go along « Left recursion indicates we are building the string from right-to-left © To climinate left recursion, wo turn it into right recursion and build the string left-to-right «© Algorithm to eliminate loft recursion is shown in Figure 5.1. « Eliminating left recursion at one level is done by As Aay | Aaz | | Aam | Bt | Be | mt Bn where no fj, 1 aj. Such a table is called an LL) seloctor table, LL Parsing Page 59 ‘Nonter— Input Symbol minal a + ical * C ) $ E ETE" ESTE" iz prortg”| perp" Bla e| Buse oe T3547 | T3-T Fs TFT" TFT" rf Te | Moe [Porro er" Those Fr Posk | PojF F_ | Fa Fae) Figure 5.5: Example selector table 5.2.2 Example Selection Table © Consider Figure 5.5 that shows a selector table for the grammar generating arithmetic ex- pressions Notice this grammar has been modified by the transformations talked about previously. For example, left recursion has been removed. 5.2.3 LL(1) Parsing Algorithm «© The algorithm for the LL(1) parse is shown in Figure 5.6, 5.2.4 Example © Let's parse the string a (a-+a). The parse is shown in Figure 5.7. 5.2.5 Selector ‘Table Construction © The algorithm for constructing the selector table is shown in Figure 5.8. © If any pair (A,2) maps to two or more different productions, then the grammar cannot be LL(1); we say that we have a conflict, © Consider the grammar shown in Figure 5.9(a).. © The selector table is shown in Figure 5.9(b). * Obviously the grammar is not LL(1) * Let's consider another example, ‘The grammar is shown in Figure 5.10(a), the First and Follow sets are shown in Figure 5.10(b), and the solector table is shown in Figure 5.10(c). LL Parsing Page 60 [Algorithm 5.2 LL(1) Parse Algorithm { Input: A string w and a parsing table M for grammar G. } { Output: Ifw is in L(G), @ leftmost derivation of w; otherwise, an error indication. Initially, the parser is in a configuration in which it has $S on the stack with S, the start symbol] of G on top, and wS in the input buffer. Set ip to point to the first symbol of w8. repeat Let X be the top stack symbol and a the symbol pointed to by ép. ifXeVor$ ifX=a | Pop X from the stack and advance ip. else error() end if else { Xisanonterminal } if M[X,a] =X > ¥s¥o--Ye Pop X from the stack Push Yi, Ye-a,-++,¥i onto the stack, with ¥; on top Output the production X + ¥1¥2---Ye else error() end if end if until X {stack is empty } Figure 5.6: LI(1) Parsing Algorithm LL Parsing Page 61 ‘Nonter— | Input Symbot a minal | a | 8 ss os Boal ae C00 eat. B | Boac @) c C00 Gram. mar (b) Selector table Figure 5.9: Example of selector table construction 1, SAB 8 Zox% be First = polo as ae a 3 AGZW 10. WowW | y |e” ny oer 4 xXoex Woe [|X| % wy 5. Xe 12. B4bB ly a te Your wae LHS ee ie Bld 3 (0) Grammar (b) First and Follow sets ‘Nonter— Taput Symbol minal_ [6 € a w z z z_I§ S| S>AB| SAB | S>AB| SAB | SAB | SAB | SAB A |asxy|asgw] Aoxy | Aszw | 4oxy | Aoxy | 44217 x | Kae Xe Xoax | Xe eee ose Yoe You z Bove aoe Bok Ww Woe Woww B_ | Bop Bod (©) Selector table Figure 5.10: Another example of selector table constrnetion LL Parsing Page 63

You might also like