Presentation is loading. Please wait.

Presentation is loading. Please wait.

Discussion #61/13 Discussion #6 Parsing Recursive Grammars.

Similar presentations


Presentation on theme: "Discussion #61/13 Discussion #6 Parsing Recursive Grammars."— Presentation transcript:

1 Discussion #61/13 Discussion #6 Parsing Recursive Grammars

2 Discussion #62/13 Topics Tail recursion LL(1) with  Table driven LL(1) with  Lexical Analyzers

3 Discussion #63/13 Motivating Example Let’s use integers instead of digits in our prefix language. What’s the problem with *2+72100? –Syntax error? Or is it simply ambiguous? –* 2 + 7 2100 = 2 * (7 + 2100) = 4214? –* 2 + 72 100 = 2 * (72+100) = 344? Solution? –Let n mark the beginning of a number e.g. * n2 + n72 n100 = 2 * (72 + 100) = 344 –Strange: but you’ll soon see where we are headed and why.

4 Discussion #64/13 E  (1) N | (2) OEE O  (3) + | (4) * N  (5) nI I  (6) D | (7) ID D  (8) 0 | (9) 1 | … | (17) 9 E I D n 2 OEENN+In DI DI D 1 0 0 In DI D 7 2 NOEE* Consider: * n2 + n72 n100

5 Discussion #65/13 E  (1) N | (2) OEE O  (3) + | (4) * N  (5) nI I  (6) D | (7) ID D  (8) 0 | (9) 1 | … | (17) 9 E I D n 2 OEENN+ In ?? NOEE* * n2 + n72 n100 Question… Which rule do we choose? I  (6) D orI  (7) ID We don’t know without looking further ahead. Should we look further ahead, or find another way?

6 Discussion #66/13 LL(1) with  There is another way. Consider the following replacement: – (6) I  D by (6) I  D T – (7) I  ID T  (7) I | (8)  Now, if I is on the top of the stack and we see a digit, we choose I  DT. If T is on top, if we see a digit, we choose T  I, otherwise we choose T  . DT I DT I1 DT 00I  Note: The  does not “consume” the “+” which is still on top. Example: the 100 in …n100+n21n…

7 Discussion #67/13 Tails We use tails for things that go on forever. –Numbers, eg. 12, 123456, … –Parameter lists, eg. (parm 1, parm 2,…,parm n ) –Variable names, eg. dog, doggone, … Note that T(ail) rules have special constructions: –FIRST(I) = FIRST(T) = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} –FIRST(  for T ) = { {V T  {#}} – FIRST(T) } = {+, *, n, #} –Note: FIRST(T)  FIRST(  for T ) =  –Note also: FIRST(I)  FIRST(  for T ) = V T  {#} –Thus, our tail construction simply iterates until it reaches the end. Further, it leaves the character that is one beyond the end on top of the stack.

8 Discussion #68/13 E  (1) N | (2) OEE O  (3) + | (4) * N  (5) nI I  (6) DT T  (7) I | (8)  D  (9) 0 | (10) 1 | … | (18) 9 +*012…n# E(OEE,2) (N,1) O(+,3)(*,4) N(nI,5) I(DT,6) T ( ,8) (I,7) ( ,8) D(0,9)(1,10)(2,11…) +pop * 0 1 2…pop n #accept

9 Discussion #69/13 +*012…n# E(OEE,2) (N,1) O(+,3)(*,4) N(nI,5) I(DT,6) T ( ,8) (I,7) ( ,8) D(0,9)(1,10)(2,11) +,*,0-9,npop #accept ActionStackInputOutput InitializeE#E#  +n10n1# ACTION(E,+) = Replace [E,OEE], Out 2OEE#  +n10n1#2 ACTION(+,+) = pop(+,+)EE#+  n10n1#23 ACTION(E,n) = Replace [E,N], Out 1NE#+  n10n1#231 ACTION(O,+) = Replace [O,+], Out 3+EE#  +n10n1#23 ACTION(N,n) = Replace [N,nI], Out 5nIE#+  n10n1#2315 ACTION(I,1) = Replace [I,DT], Out 6DTE#+n  10n1#23156 ACTION(n,n) = pop(n,n)IE#+n  10n1#2315 ACTION(D,1) = Replace [D,1], Out 101TE#+n  10n1#23156 10 ACTION(1,1) = pop(1,1)TE#+n1  0n1#23156 10 ACTION(T,0) = Replace [T,I], Out 7IE#+n1  0n1#23156 10 7 ACTION(I,0) = Replace [I,DT], Out 6DTE#+n1  0n1#23156 10 76 ACTION(D,0) = Replace [D,0], Out 90TE#+n1  0n1#23156 10 769

10 Discussion #610/13 +*012…n# E(OEE,2) (N,1) O(+,3)(*,4) N(nI,5) I(DT,6) T ( ,8) (I,7) ( ,8) D(0,9)(1,10)(2,11) +,*,0-9,npop #accept ActionStackInputOutput Continued…0TE#+n1  0n1#23156 10 769 ACTION(0,0) = pop(0,0)TE#+n10  n1#23156 10 769 ACTION(T,n) = Replace [T,  ], Out 8 E#E# +n10  n1#23156 10 7698 ACTION(E,n) = Replace [E,N], Out 1N#N#+n10  n1#23156 10 76981 ACTION(N,n) = Replace [N,nI], Out 5nI#+n10  n1#23156 10 769815 ACTION(n,n) = pop(n,n)I#I#+n10n  1#23156 10 769815 ACTION(I,1) = Replace [I,DT], Out 6DT#+n10n  1#23156 10 7698156 ACTION(D,1) = Replace [D,1], Out 101T#+n10n  1#23156 10 7698156 10 ACTION(1,1) = pop(1,1)T#T#+n10n1  #23156 10 7698156 10 ACTION(T,#) = Replace [T,  ], Out 8 ## +n10n1  #23156 10 7698156 10 8 ACTION(#,#) = Accept! # +n10n1  # 23156 10 7698156 10 8 ACTION( ,n) = pop  E# +n10  n1# 23156 10 7698 ACTION( ,#) = pop  # +n10n1  # 23156 10 7698156 10 8

11 Discussion #611/13 E  (1) N | (2) OEE O  (3) + | (4) * N  (5) nI I  (6) DT T  (7) I | (8)  D  (9) 0 | (10) 1 | … | (18) 9 2 3 1 5 6 10 7 6 9 8 1 5 6 10 8 E 2 3 1 5 6 10 7 6 9 8 1 5 6 10 8 is the parse for + n 1 0 n 1 OEE 2 + 3 N 1 N 1 In 5 TD 6 1 10 I 7 TD 6 0 9  8 In 5 TD 6 1  8

12 Discussion #612/13 E  (1) N | (2) OEE O  (3) + | (4) * N  (5) nI I  (6) DT T  (7) I | (8)  D  (9) 0 | (10) 1 | … | (18) 9 2 3 1 5 6 10 7 6 9 8 1 5 6 10 8 E OEE 2 + 3 N 1 N 1 In 5 TD 6 1 10 I 7 TD 6 0 9  8 In 5 TD 6 1  8 Lexical Analyzer Motivation

13 Discussion #613/13 E  (1) N | (2) OEE O  (3) + | (4) * N  (5) E 2 3 1 5 1 5 becomes the parse for +n10n1 where the tokens are +, n10, and n1 OEE 2 + 3 N 1 N 1 n10 5 n1 5 Tokenization Simplifies Grammars


Download ppt "Discussion #61/13 Discussion #6 Parsing Recursive Grammars."

Similar presentations


Ads by Google