html5-img
1 / 8

Winter 2007-2008 LR(0) Parsing Summary

Winter 2007-2008 LR(0) Parsing Summary. Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University. LR(0) parsing. Construct transition relation between states Use algorithms Initial item set and Next item set States are set of LR(0) items

Download Presentation

Winter 2007-2008 LR(0) Parsing Summary

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Winter 2007-2008LR(0) Parsing Summary Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University

  2. LR(0) parsing • Construct transition relation between states • Use algorithms Initial item set and Next item set • States are set of LR(0) items • Shift items of the form PαSβ • Reduce items of the form Pα • Construct parsing table • If every state contains no conflicts use LR(0) parsing algorithm • If states contain conflict • Rewrite grammar or • Resolve conflict or • Use stronger parsing technique

  3. ε closure algorithm for LR(0) item sets for a grammar G Data definitions: A set S of LR(0) items.Initializations: S is prefilled externally with one or more LR(0) items.Inference rules: If S holds an item of the form PαNβ, then for each production rule Nγ in G, S must also contain the item Nγ. p. 155 FUNCTION Initial item set RETURNING an item set: SET New item set TO Empty; // Initial contents – obtain from the start symbol: FOR EACH production rule Sα for the start symbol S: SET New item set TO New item set + item Sα; RETURN ε closure (New item set); p. 158 FUNCTION Next item set (Item set, Symbol) RETURNING an item set: SET New item set TO Empty; // Initial contents – obtain from token moves: FOR EACH item NαSβ IN item set; IF S = Symbol; SET New item set TO New item set + NαSβRETURN ε closure (New item set); p. 158

  4. Example: LR(0) for grammar G Grammar G: Z  E$ E  T E  E+ T T i T  (E) Convention:non-terminals denoted by upper-case lettersterminals denoted by lower-case letters

  5. S6 E  T T T S7 S0 T  (E)E TE E+TT iT  (E) Z E$E TE E+TT iT  (E) ( ( S5 i i T  i E E S8 ( S1 T  (E)E  E+T Z  E$E  E+T i + + ) $ E  E+TT iT  (E) S3 S9 S2 T  (E) Z  E$ T S4 E  E+T

  6. GOTO tablesymbol ACTION table

  7. LR(0) Parsing with a push-down automaton IMPORT input token [1..]; // from the lexical analyzer SET Input token index TO 1;SET Reduction stack TO Empty stack;PUSH Start State ON Reduction stack; While Reduction stack ≠ {Start state, Start symbol, End state} SET State TO Top of Reduction stack; SET Action TO Action table [State]; IF Action = “shift”; // Do a shift move; SET shifted token TO Input token [Input token index]; SET Input token index TO Input token index + 1; // shifted PUSH Shifted token ON Reduction stack; Set New State TO Goto table [State, Shifted token .class]; PUSH New state ON Reduction stack; // can be empty ELSE IF Action = (“reduce”, Nα) : // Do a reduction move: Pop the symbols of α from the Reduction stack; SET State TO Top of Reduction stack; // update state PUSH N ON Reduction stack; SET New state TO Goto table [State, N] PUSH New State ON Reduction stack; // cannot be empty ELSE Action = Empty: ERROR “Error at token “, Input token [Input token index]; p. 162

  8. LR(0) parsing of i+i StackInputAction S0 i + i $ shift S0 i S5 + i $ reduce by Ti S0 T S6 + i $ reduce by ET S0 E S1 + i $ shift S0 E S1 + S3 i $ shift S0 E S1 + S3 i S5 $ reduce by Ti S0 E S1 + S3 T S4 $ reduce by EE+T S0 E S1 $ shift S0 E S1 $ S2 reduce by ZE$ S0 Z stop

More Related