LR Parsers An LR parser reads input from

  • Slides: 9
Download presentation
LR Parsers • An LR parser reads input from Left to right and constructs

LR Parsers • An LR parser reads input from Left to right and constructs a Rightmost derivation in reverse. • The parsing tables are constructed differently from LL parsers and they depend on different concepts. • Consider this grammar: S→a. Ac. Be A→Ab|b B→d and string abbcde. We want to reduce this to S. • Scan the string looking for right hand sides of productions: abbcde a. Acde a. Ac. Be S • At each point, we found a handle, that is, a sentential form , a production A→ , and a position in where the string can be found and replaced by A to produce the previous right sentential form in a rightmost derivation of . Lecture # PLP Spring 2004, UF CISE 1

LR Parser Structure a 1 ··· ai ··· an $ Stack sm ··· s

LR Parser Structure a 1 ··· ai ··· an $ Stack sm ··· s 1 $ Input Program Output Parsing Table Lecture # PLP Spring 2004, UF CISE 2

Whence Cometh an LR Parser? Grammar Table Generator Parsing Table I will leave the

Whence Cometh an LR Parser? Grammar Table Generator Parsing Table I will leave the Table Generation Algorithm for LR Parser a mystery. The LR parsing algorithm and table construction scheme was invented by Knuth (1965) and made practical by De. Remer (1969, 1971). Lecture # PLP Spring 2004, UF CISE 3

Driver Program of an LR Parser • A configuration of an LR parser is

Driver Program of an LR Parser • A configuration of an LR parser is a pair whose first component is the stack contents and whose second component is the unexpended input: (s 0 X 1 s 1 X 2 s 2 ··· Xmsm, aiai+1 ··· an$) 1. If ACTION[sm, a] = shift s, the parser executes a shift move, entering configuration (s 0 X 1 s 1 X 2 s 2 ··· Xmsmais, ai+1 ··· an$) 2. If ACTION[sm, a] = reduce A→ , then the parser executes a reduce move, entering configuration (s 0 X 1 s 1 X 2 s 2 ··· Xm-rsm-r. As, aiai+1 ··· an$) where s=GOTO[sm-r, A], and r is the length of . 3. If ACTION[sm, a]=accept, parsing is completed. 4. If ACTION[sm, a]=error, the parser has discovered an error and calls the error recovery routine. Lecture # PLP Spring 2004, UF CISE 4

Sample Grammar 1. 2. 3. 4. 5. 6. E→E+T E→T T→T+F T→F F→(E) F

Sample Grammar 1. 2. 3. 4. 5. 6. E→E+T E→T T→T+F T→F F→(E) F → id 7. Action codes in the table are as follows: 1. 2. 3. 4. si means shift and push state i on the stack rj means reduce by production number j acc means accept blank means error Lecture # PLP Spring 2004, UF CISE 5

LR Parsing Table Stack Action id 0 + * s 5 ( Goto )

LR Parsing Table Stack Action id 0 + * s 5 ( Goto ) $ s 4 1 s 6 2 r 2 s 7 r 2 3 r 4 r 4 4 s 4 r 6 r 6 6 s 5 s 4 7 s 5 s 4 F 1 2 3 8 2 3 9 3 r 6 10 8 s 6 9 r 1 s 7 r 1 10 r 3 r 3 11 r 5 r 5 Lecture # T acc s 5 5 E s 11 PLP Spring 2004, UF CISE 6

Sample Parse State Stack 1. 0 2. 0 id 5 (1) 0 F 3

Sample Parse State Stack 1. 0 2. 0 id 5 (1) 0 F 3 (2) 0 T 2 (3) 0 T 2 * 7 (4) 0 T 2 * 7 id 5 (5) 0 T 2 * 7 F 10 (6) 0 T 2 (7) 0 E 1 (8) 0 E 1 + 6 (9) 0 E 1 + 6 id 5 (10) 0 E 1 + 6 F 3 (11) 0 E 1 + 6 T 9 (12) 0 E 1 Lecture # Input id * id + id $ id + id $ + id $ $ $ PLP Spring 2004, UF CISE Table (action, goto) (s 5) (r 6 F→id, g 3) (r 4 T→F, g 2) (s 7) (s 5) (r 6, g 10) (r 3 T→T*F, g 2) (r 2 E→T, g 1) (s 6) (s 5) (r 6, g 3) (r 4, g 9) (r 1 E→E + T, g 1) (acc) 7

Building a Parse Tree While Parsing • Each grammar symbol on the stack can

Building a Parse Tree While Parsing • Each grammar symbol on the stack can be associated with a tree object. • When a shift is performed, a tree consisting of a single node (containing the symbol shifted) is pushed on the stack. • When a reduce is performed, the trees associated with the elements being popped from the stack become children of a new tree associated with the element being pushed onto the stack. • When parsing finished, the root node is associated with the parse tree. Lecture # PLP Spring 2004, UF CISE 8

How to Build a Parse Tree While Parsing (1) (2) (3) (4) (5) (6)

How to Build a Parse Tree While Parsing (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) $ id 1+ id 2 * id 3$ $id 1 + id 2 * id 3$ $E + E * id 3$ $E + E * id 3 $ $E + E * E $ $E + E $ $E $ E E E id 1 Lecture # PLP Spring 2004, UF CISE + E id 2 * E id 3 9