Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 5 + 6 Ahmed Ezzat.

Similar presentations


Presentation on theme: "CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 5 + 6 Ahmed Ezzat."— Presentation transcript:

1 CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 5 + 6 Ahmed Ezzat

2 CS 404Ahmed Ezzat 2 Review: Bottom-up Parsing Start with strings of terminals Builds up from leaves of parse tree Apply production backwards (reduction) When reach start symbol and exhausted input, done Shift-reduce is one common type of bottom- up parsing

3 CS 404Ahmed Ezzat 3 Review: Shift-reduce Parsing Use context-free grammar (may not be LL1) Use stack to keep track of tokens seen so far Hard to do manually, but best with Yacc Basic idea – Shift next symbol onto stack – When stack top contains a good right-hand-side of a production, reduce by a rule

4 CS 404Ahmed Ezzat 4 Construct LR Parsing Table Construct the set of states s0, s1, …, sn Fill Action Table: Action[S,a], a is terminal Fill Goto Table: Goto[S,X], X is non-terminal

5 CS 404Ahmed Ezzat 5 LR(0) Item Sets An LR(0) item (or item for short) of a grammar G is a production of G with a dot at some position of the right side. For example, A->.XYZ, or A->X.YZ Dot represents how parser has gotten in recognizing the production Items are grouped into sets that give the states of the SLR parser

6 CS 404Ahmed Ezzat 6 Transition Between Item Sets Parser goes from one state to another based on symbols processed A->X.YZ ->Y->A->XY.Z When a state has a dot at an end of an item, we can reduce by a production rule

7 CS 404Ahmed Ezzat 7 Function: Closure Find all items that we expect to see at some stage of parsing For a set of items I – Every item in I is in closure(I) – If A->α.Bβ is in closure(I), and B->γ is a production, then add B->. γ to closure(I) – Repeat until no more new items can be added (called a fixed point)

8 CS 404Ahmed Ezzat 8 Function Goto Goto(I,X), I is a set of items, X is a symbol, terminal or non-terminal Goto(I,X) is the closure of the set of all items [A->αX.β] such that [A->α.Xβ] is in I. (move dot forward over X) Goto(I,X) tells the parser which state to shift (for terminals) or goto (for non-terminals).

9 CS 404Ahmed Ezzat 9 Construct the SLR Parsing Table Augment the grammar and ass S’->S, S’ is not the new start symbol Let C = { I0=closure(S’->.S)} Repeat until no more items generated – Add each non-empty goto(I,X) to C Each Ii is a state Si in the parsing table

10 CS 404Ahmed Ezzat 10 Example LR(0) Item Sets StateItem Set XGoto(I,X) I0:E'  EEI1 E  E+TEI1 E  TTI2 T  (E)(I3 T  ididI4 I1: E'  EAccept (dot at end of E’ rule) E  E+T+I5 I2: E  TReduce 2 (dot at end) I3: T  (E)EI6 E  E+TEI6 E  TTI2 T  (E)(I3 T  ididI4 (0) E’  E (1) E  E + T (2) E  T (3) T  (E) (4) T  id

11 CS 404Ahmed Ezzat 11 Example LR(0) Item Sets State(Item Set) XGoto(I,X) I4:T  idReduce 4 (dot at end) I5: E  E+TTI7 T  (E)(I3 T  ididI4 I6: T  (E))I8 E  E + T+I5 I7: E  E+TReduce 1 (dot at end) I8: T  (E)Reduce 3 (dot at end)

12 CS 404Ahmed Ezzat 12 Example SLR Parsing Table State ActionGoto id+()$ET 0S4 S3 12 1 S5 accept 2R2R2R2R2R2 3S4 S3 62 4R4R4R4R4R4 5S4 S3 7 6 S5 S8 7R1R1R1R1R1 8R3R3R3R3R3 (1) E  E + T (2) E  T (3) T  (E) (4) T  id

13 CS 404Ahmed Ezzat 13 More on Grammars SLR(1) – we just learned LR(1) – Split items to resolve shift/reduce error – Table too large LALR – Merge similar LR(1) states, smaller table – Handles more language than SLR(1)

14 CS 404Ahmed Ezzat 14 Error Repair and Recovery In the presence of an error, a compiler can (crash) Detect Report Repair (guess) Recover (continue)

15 CS 404Ahmed Ezzat 15 Error Handling Goals Detect errors as soon as possible Report errors clearly and accurately Recover soon enough to continue processing (find more than one error) Don’t significantly delay compilation of correct programs

16 CS 404Ahmed Ezzat 16 Error Detection In lexical analysis – Invalid characters, incomplete strings, file ending without EOF In syntax analysis – Input violates grammar, cannot continue parsing – But when did the error begin? – Rule: can only report when error occur (when the parsing table entry is empty or error)

17 CS 404Ahmed Ezzat 17 Error Reporting Include context information of an error – Source file location – Tokens before and after – Name of current routine or module – Report what was found and what was expected

18 CS 404Ahmed Ezzat 18 Error Repair Fix code – Example: add “end of comment” – Example: add “;” Replace Keyword – Example: duoble -> double (expecting a keyword, got an id)

19 CS 404Ahmed Ezzat 19 Error Recovery Global correction – If X not in L(G), find a similar X’ in L(G) Panic mode – Throw away incoming token until a “good” one is found. For example, until }, ; or EOL – May miss some errors

20 CS 404Ahmed Ezzat 20 Error Recovery (cont.) Phrase level recovery – Replace incoming token with a “correct” one Use error productions – Have grammar rules that match common error patterns, then have corresponding actions


Download ppt "CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 5 + 6 Ahmed Ezzat."

Similar presentations


Ads by Google