Cse 321 Programming Languages and Compilers Lecture 10

  • Slides: 24
Download presentation
Cse 321, Programming Languages and Compilers Lecture #10, Feb. 14, 2007 • Modified sets

Cse 321, Programming Languages and Compilers Lecture #10, Feb. 14, 2007 • Modified sets of item construction • Rules for building LR parse tables • The Action rules • The GOTO rules • Conflicts and ambiguity • Shift-reduce and reduce-reduce conflicts • Parser generators and ambiguity • Ambiguous expression grammar • Ambiguous if-then-else grammar • Ml-yacc 1/23/2022 1

Cse 321, Programming Languages and Compilers Assignments • Homework – Assignment 7 will be

Cse 321, Programming Languages and Compilers Assignments • Homework – Assignment 7 will be accepted til the end of the week » good review for exam! – Assignment 8 (paper and pencil) is posted & due Mon. Feb 19 » good review for exam! – Assignment 9 (programming) is posted & due Wed. Feb 21 » in case your interested • Project 1 is Due today. – Email me the code – Name files with your last name as discussed in the Project 1 description. • Midterm Exam will be Monday, Feb 19, 2007 – Exam will be closed book – Exam will take 60 minutes – We will have a short lecture after the exam. • Project 2 will be assigned next Monday Feb 19, 2006 1/23/2022 2

Cse 321, Programming Languages and Compilers To facilitate Table building • To facilitate Table

Cse 321, Programming Languages and Compilers To facilitate Table building • To facilitate Table building we modify the sets of items construction slightly • Each item now has three components. – A production – A location for the dot – A terminal symbol that indicates a valid terminal that could follow the production. This is similar to, but not quite like the Nonterminals that are in the Follow set. Start → E • Examples: • [ Start →. Exp, EOF] • [ F → T. * F, +] E → E + T | T T → T * F | F F → ( E ) 1/23/2022 | id 3

Cse 321, Programming Languages and Compilers Modified Closure • • • Let I be

Cse 321, Programming Languages and Compilers Modified Closure • • • Let I be a set of it modified items Then Closure(I) = For each i I, where i = [A → . B β, x] For each p Productions where p = B → For each t Terminals where t First(βx) Add [B →. , t] to Closure(I) if its not already there 1/23/2022 4

Cse 321, Programming Languages and Compilers Modified GOTO • GOTO(I, X) = • For

Cse 321, Programming Languages and Compilers Modified GOTO • GOTO(I, X) = • For each item in I of the form [A → . X β, a] » i. e. the dot comes just before the X • Let J be the set of items [A → X. β, a] » i. e. move the dot after the X • Return the Closure(J) 1/23/2022 5

Cse 321, Programming Languages and Compilers Modified Sets of items Construction • Start with

Cse 321, Programming Languages and Compilers Modified Sets of items Construction • Start with a grammar with a Start symbol with only 1 production. Start → E – If the grammar isn’t of that form create a new grammar that is of that form with a new start symbol that accepts the same set of strings. • • C : = Closure( { [ Start →. E, EOF] }) For each set of items I C For Each X Non. Terminal union Terminal Compute new : = GOTO(I, X) – If new is not empty, and new is not already in C, add it to C • Until no new sets of items can be added to C 1/23/2022 6

Cse 321, Programming Languages and Compilers Building Tables for LR parsers • Once the

Cse 321, Programming Languages and Compilers Building Tables for LR parsers • Once the sets of items have been constructed, then the tables can be constructed by using – The set of items – The GOTO construction – The grammar • Each set of items corresponds to a state. • States and Terminals index the ACTION table • States and Non-Terminals index the GOTO table 1/23/2022 7

Cse 321, Programming Languages and Compilers Construction of ACTION table • Let C be

Cse 321, Programming Languages and Compilers Construction of ACTION table • Let C be the sets of items constructed for a grammar. There is one state “i” for each set ci C 1. If [A → . a β, b] ci , and GOTO(ci , a) = cj Then set ACTION[ i, a ] to shift j note “a” is a terminal symbol 2. If [A → . , a] ci Then set ACTION[ i, a ] to reduce( A → ) 3. If [Start → S. , EOF ] ci Then set ACTION[ i, EOF ] to accept Any conflict in these rules means the grammar is ambiguous. 1/23/2022 8

Cse 321, Programming Languages and Compilers Construction of GOTO table If GOTO(ci , A)

Cse 321, Programming Languages and Compilers Construction of GOTO table If GOTO(ci , A) = cj Then set GOTO(I, A) to j Note that “A” is a Non-Terminal symbol • • All other entries are error entries The Start state of the parser is the state derived from Closure( { [ Start →. E, EOF] }) 1/23/2022 9

Cse 321, Programming Languages and Compilers Parser generators • Programs that analyze grammars to

Cse 321, Programming Languages and Compilers Parser generators • Programs that analyze grammars to produce efficient winning strategies • ml-yacc uses a LALR(1) table-driven parser – Look-Ahead 1 symbol – Left to right processing of input – Right-most derivation • ml-yacc reads a grammar, produces a table • ml-yacc attaches semantic actions to reduce moves 1/23/2022 10

Cse 321, Programming Languages and Compilers ml-yacc makes a virtue out of Ambiguity •

Cse 321, Programming Languages and Compilers ml-yacc makes a virtue out of Ambiguity • Why not: expr -> expr + expr | expr * expr | Number expr Number 17 1/23/2022 + * Number expr 2 3 11

Cse 321, Programming Languages and Compilers Factoring - A hard solution • Fix the

Cse 321, Programming Languages and Compilers Factoring - A hard solution • Fix the grammar E: E+T |T ; T: T*F |F ; F: (E) | Number ; E E T F Number + T T Number 3 * F Number 2 17 1/23/2022 12

Cse 321, Programming Languages and Compilers Using Ambiguity • Ambiguity means the parser can’t

Cse 321, Programming Languages and Compilers Using Ambiguity • Ambiguity means the parser can’t decide between – Shifting a terminal, or reducing a handle to a Non-Terminal – Reducing a handle to one or more Non-terminals » T → rhs and S → rhs are both in the grammar and rhs is the handle. • This choice means we can’t construct a unique parse tree for any string. • But what if we could direct the parser to always prefer one choice over the other. • Then – The parse tree would always be unique – The grammar might even be smaller 1/23/2022 13

Cse 321, Programming Languages and Compilers Ambiguous Expression Grammar • Contrast the two grammars.

Cse 321, Programming Languages and Compilers Ambiguous Expression Grammar • Contrast the two grammars. Start → E E → E + E • Convince yourself they both accept the same set of strings. | E * E | ( E ) | • Which one is ambiguous? • Which one is simpler? id Start → E E → E + T | T T → T * F • Which one is smaller? | F F → ( E ) | 1/23/2022 id 14

Cse 321, Programming Languages and Compilers An LR parser for the ambiguous EXP grammar

Cse 321, Programming Languages and Compilers An LR parser for the ambiguous EXP grammar • The sets of item construction has 11 states • It has 4 shift-reduce ambiguities Start → E E → E + E | E * E | ( E ) | 1/23/2022 id 15

Cse 321, Programming Languages and Compilers 4 shift reduce ambiguities State 8 { [E

Cse 321, Programming Languages and Compilers 4 shift reduce ambiguities State 8 { [E → E. + E , _ ] , [E → E. * E, _ ] , [E → E * E. , _ ] } Action(8, +) shift 5 Action(8, +) reduce by 2 State 8 { [E → E. + E , _ ] , [E → E. * E, _ ] , [E → E * E. , _ ] } Action(8, *) shift 4 Action(8, *) reduce by 2 State 9 { [E → E. PLUS E , _ ] , [E → E PLUS E. , _ ] , [E → E. TIMES E, _ ] } Action(9, *) shift 4 Action(9, *) reduce by 1 State 9 { [E → E. PLUS E , _ ] , [E → E PLUS E. , _ ] , [E → E. TIMES E, _ ] } Action(9, +) shift 5 Action(9, +) reduce by 1 1/23/2022 16

Cse 321, Programming Languages and Compilers State of the stack 1 Stack. . .

Cse 321, Programming Languages and Compilers State of the stack 1 Stack. . . Exp * Exp • Choices Action(8, +) input + 3 EOF shift 5 reduce by 2 { [E → E. + E , _ ] , [E → E. * E, _ ] , [E → E * E. , _ ] } • Reducing by 2 means (E * E) has higher precedence than (E + E) • Generally this is what we want. 1/23/2022 17

Cse 321, Programming Languages and Compilers State of the stack 2 Stack. . .

Cse 321, Programming Languages and Compilers State of the stack 2 Stack. . . Exp * Exp • Choices Action(8, *) input * 3 EOF shift 4 reduce by 2 { [E → E. + E , _ ] , [E → E. * E, _ ] , [E → E * E. , _ ] } • Reducing by 2 means that (E * E) is left associative • Shifting * means (E * E) is right associative • The other 2 shift reduce errors are similar but talk about the precedence and associativity of (E + E) 1/23/2022 18

Cse 321, Programming Languages and Compilers ml-yacc, a Better Solution • ml-yacc allows ambiguous

Cse 321, Programming Languages and Compilers ml-yacc, a Better Solution • ml-yacc allows ambiguous grammars to be disambiguated via declarations of precedence and associativity • For example: – %left ‘+’ – %left ‘*’ • Declares that * has higher precedence than + and that both are left associative • If ambiguity remains the following rules are used – always shift on a shift/reduce conflict – do the first reduction listed in the grammar on a reduce/reduce conflict 1/23/2022 19

Cse 321, Programming Languages and Compilers Partial Ml-yacc file %left TIMES %left PLUS Much

Cse 321, Programming Languages and Compilers Partial Ml-yacc file %left TIMES %left PLUS Much more about ml-yacc next time! %% Start: E EOF ( E ) E ( ( 1/23/2022 : | | | E PLUS E E TIMES E LP E RP id Add(E 1, E 2) ) Mult(E 1, E 2) ) E ) Id id ) 20

Cse 321, Programming Languages and Compilers If-then-else example Goal → Stmt → IF Exp

Cse 321, Programming Languages and Compilers If-then-else example Goal → Stmt → IF Exp THEN Stmt | IF Exp THEN Stmt ELSE Stmt | ID : = Exp State 9 {[Stmt → IF EXP THEN Stmt. , _] , [Stmt → IF EXP THEN Stmt. ELSE Stmt, _ ] } Action(9, ELSE) = Shift 10 Action(9, ELSE) = Reduce by 1 1/23/2022 21

Cse 321, Programming Languages and Compilers State of machine Stack. . . IF Exp

Cse 321, Programming Languages and Compilers State of machine Stack. . . IF Exp THEN Stmt input ELSE x : = 3 … EOF State 9 {[Stmt → IF EXP THEN Stmt. , _] , [Stmt → IF EXP THEN Stmt. ELSE Stmt, _ ] } Action(9, ELSE) = Shift 10 Else associated with closest IF on stack Action(9, ELSE) = Reduce by 1 Else associated with further away IF 1/23/2022 22

Cse 321, Programming Languages and Compilers Ml-yacc file %nonassoc THEN %nonassoc ELSE %% Goal:

Cse 321, Programming Languages and Compilers Ml-yacc file %nonassoc THEN %nonassoc ELSE %% Goal: Stmt EOF ( Stmt ) Stmt: IF EXP THEN Stmt ( If. Then(E, Stmt) ) | IF EXP THEN Stmt ELSE Stmt ( If. Then. Else(E, Stmt 1, Stmt 2) ) | ID ASSIGNOP EXP ( Assign(ID, E) ) 1/23/2022 23

Cse 321, Programming Languages and Compilers Some sample ambiguous grammars • These examples can

Cse 321, Programming Languages and Compilers Some sample ambiguous grammars • These examples can be found in the directory • http: //www. cs. pdx. edu/~sheard/course/Cs 321/Lex. Yacc/Ambiguous. Exa mples/ 1/23/2022 24