Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP4620 – Programming Language Translators Dr. Manuel E. Bermudez

Similar presentations


Presentation on theme: "COP4620 – Programming Language Translators Dr. Manuel E. Bermudez"— Presentation transcript:

1 COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
LR(0) parsing COP4620 – Programming Language Translators Dr. Manuel E. Bermudez

2 topics Recursive Descent Parsing with “items” NFA connecting items
DFA connecting items: LR(0) parser LR Parser Operations LR Driver Algorithm Direct LR(0) Construction

3 LR(0) parsing Recursive descent code: “annotate” code “items”, e.g. A  . . Use items to describe operation of RD parser. An FSA describes all possible calling sequences in the RD parser.

4 Example: proc E(); T(); while Next_Token = T_+ do Read(T_+); od end;
LR(0) parsing Each “item” is a dotted production We’ll use items to describe all possible situations in the parser. Transitions between items form an NFA. Example: proc E(); T(); while Next_Token = T_+ do Read(T_+); od end; {E → .E + T} { E →.T} {E → E. + T} {E → T.} {E → E. + T} {E → E + .T } {E → E + T.} {E → E + T.} {E → T.} T T + T

5 NFA Connecting Items The FSA is: M = (DP, V, , S’ → .S, {S’ → S.}) where DP is the set of all possible items (DP: dotted productions), and  is defined such that simulate a call to B simulate the execution of statement X, if X is a nonterminal, or Read(X), if X is a terminal. B → . ω 1. A → α.Bβ 2. A → α.Xβ A→X.β X

6 FSA Connecting Items Example: S → E  E → E + T → T T → (E) → i E  ε

7 FSA Connecting Items Run this NFA using a stack, i.e. keep track of the recursive calling sequence. To “return” from A → ω., back up |ω| + 1 states, then advance on A. Problem with this machine: it is nondeterministic. No problem. Be happy . Transform it to a DFA !

8 DFA Connecting Items  i + THIS IS AN LR(0) AUTOMATON E → E + T.
S → . E S → E. S → E. E → . T E → T . T → . i T → i . T → . (E) T → (E) . T → (E.) T → (.E) E → .E + T E → E. + T E → E +. T E T ( i T → .i E → .T T → .(E) + ) THIS IS AN LR(0) AUTOMATON

9 LR(0) Parsing LR means “Left-to-Right, Right-most Derivation”.
Need a stack of states to operate the parser. No look-ahead required, thus LR(0). DFA describes all possible positions in the RD parser’s code. Once the automaton is built, items can be discarded.

10 LR(0) Parsing Operation of an LR parser Two moves: shift and reduce.
Shift: Advance from current state on Next_Token, push new state on stack. Reduce: (on A → ω). Pop |ω| states from stack. Advance from new top state on A.

11 LR(0) Parsing Stack Input Derivation Tree
i + (i + i)  i + ( i + i ) (i + i)  (i + i)  (i + i)  (i + i)  i + i)  i)  i)  i)  i)  )  )  )  LR(0) Parsing E T T T E T 1 3 2 4 5 6 7 8 9 10 E i ( ) + T E → E+T T → (E) E → T T→i E E

12 LR(0) PARSE TABLEs Two Tables:
Action Table: indexed by state, and by terminal symbol. Contains all shift and reduce moves. GOTO Table: indexed by state, and by nonterminal symbol. Contains all transitions on nonterminals symbols.

13 LR(0) PARSE TABLES 1 3 2 4 5 6 7 8 9 10 i + ┴ ACTION GOTO i + ( ) ┴
E → E+T T → (E) E → T T→i ACTION GOTO i ( ) ┴ E T 1 S/4 S/5 2 3 S/7 S/6 R/E→T 4 R/T→ i 5 8 6 Accept 7 9 S/10 R/E→E+T 10 R/T →(E) R/T → (E)

14 LR(0) Driver algorithm Push(Start_State, S);
while ACTION (Top(S), ) ≠ Accept do case ACTION (Top(S), Next_Token) of s/r: Read(Next_Token); Push(r, S) R/A → ω: Pop(S) |ω| times; Push(GOTO (Top(S), A), S); empty: Error; end end;

15 Direct LR(0) construction
PT(G) = Closure({S’ → .S  }) U {Closure(P) | P  Successors(P’), P’  PT(G)} Closure(P) = P U {A → .w | B → α.Aβ  Closure(P)} Successors(P) = {Nucleus(P, X) | X  V} Nucleus(P, X) = {A → αX .β | A → α.Xβ  P}

16 Direct LR(0) construction
2 S → . E  E → .E + T E → .T T → .i T → . ( E ) T → (.E) E → .E + T E → .T T → .i T → .( E ) 8 T → (E.) E → E. + T 1 10 E 5 E 8 + 7 T 3 T 3 i 4 i 4 9 E → E + T. ( ( 5 5 10 T → (E). 6 S → E .  E → E. + T 2 6 S → E . + Grammar: S → E  E → E + T → T T → (E) → i 7 T E → E + .T T → .i T → .(E) 9 7 i 4 3 E → T. ( 5 4 T → i.

17 Direct LR(0) construction
Notes: Two states are “equal” if their Nuclei are identical. This grammar is LR(0) because there are no “conflicts”. A conflict occurs when a state contains i – Both a final (dot-at-the-end) item and non-final item (shift-reduce), or ii – Two or more final items (reduce-reduce). We’ll deal with conflicts soon.

18 summary Recursive Descent Parsing with “items” NFA connecting items
DFA connecting items: LR(0) parser LR Parser Operations LR Driver Algorithm Direct LR(0) Construction


Download ppt "COP4620 – Programming Language Translators Dr. Manuel E. Bermudez"

Similar presentations


Ads by Google