Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #10, Feb. 14, 2007 Modified sets of item construction Rules for building LR parse tables.

Slides:



Advertisements
Similar presentations
A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
Advertisements

Compiler construction in4020 – lecture 4 Koen Langendoen Delft University of Technology The Netherlands.
Compiler Principles Fall Compiler Principles Lecture 4: Parsing part 3 Roman Manevich Ben-Gurion University.
Bottom up Parsing Bottom up parsing trys to transform the input string into the start symbol. Moves through a sequence of sentential forms (sequence of.
Chapter 3 Syntax Analysis
CH4.1 CSE244 SLR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
CSE 5317/4305 L4: Parsing #21 Parsing #2 Leonidas Fegaras.
Mooly Sagiv and Roman Manevich School of Computer Science
LR Parsing Table Costruction
Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
1 Chapter 5: Bottom-Up Parsing (Shift-Reduce). 2 - attempts to construct a parse tree for an input string beginning at the leaves (the bottom) and working.
Pertemuan 12, 13, 14 Bottom-Up Parsing
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
ML-YACC David Walker COS 320. Outline Last Week –Introduction to Lexing, CFGs, and Parsing Today: –More parsing: automatic parser generation via ML-Yacc.
Parsing VI The LR(1) Table Construction. LR(k) items The LR(1) table construction algorithm uses LR(1) items to represent valid configurations of an LR(1)
Lecture #8, Feb. 7, 2007 Shift-reduce parsing,
Parsing V Introduction to LR(1) Parsers. from Cooper & Torczon2 LR(1) Parsers LR(1) parsers are table-driven, shift-reduce parsers that use a limited.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
1 Bottom-up parsing Goal of parser : build a derivation –top-down parser : build a derivation by working from the start symbol towards the input. builds.
Bottom-up parsing Goal of parser : build a derivation
LALR Parsing Canonical sets of LR(1) items
LESSON 24.
Syntax and Semantics Structure of programming languages.
LEX and YACC work as a team
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
LR Parsing Compiler Baojian Hua
10/25/20151 Programming Languages and Compilers (CS 421) Grigore Rosu 2110 SC, UIUC Slides by Elsa Gunter, based.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
CMSC 331, Some material © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Chapter 4 Bottom Up Parsing.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
11 Outline  6.0 Introduction  6.1 Shift-Reduce Parsers  6.2 LR Parsers  6.3 LR(1) Parsing  6.4 SLR(1)Parsing  6.5 LALR(1)  6.6 Calling Semantic.
Syntax and Semantics Structure of programming languages.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
Syntax Analysis - LR(0) Parsing Compiler Design Lecture (02/04/98) Computer Science Rensselaer Polytechnic.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
Compiler Principles Fall Compiler Principles Lecture 5: Parsing part 4 Roman Manevich Ben-Gurion University.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
Three kinds of bottom-up LR parser SLR “Simple LR” –most restrictions on eligible grammars –built quite directly from items as just shown LR “Canonical.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 6: LR grammars and automatic parser generators.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Lecture 5: LR Parsing CS 540 George Mason University.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
Bottom-up parsing. Bottom-up parsing builds a parse tree from the leaves (terminals) to the start symbol int E T * TE+ T (4) (2) (3) (5) (1) int*+ E 
1 Chapter 6 Bottom-Up Parsing. 2 Bottom-up Parsing A bottom-up parsing corresponds to the construction of a parse tree for an input tokens beginning at.
Conflicts in Simple LR parsers A SLR Parser does not use any lookahead The SLR parsing method fails if knowing the stack’s top state and next input token.
Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
Eliminating Left-Recursion Where some of a nonterminal’s productions are left-recursive, top-down parsing is not possible “Immediate” left-recursion can.
Announcements/Reading
Programming Languages Translator
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
Table-driven parsing Parsing performed by a finite state machine.
Chapter 4 Syntax Analysis.
Fall Compiler Principles Lecture 4: Parsing part 3
LALR Parsing Canonical sets of LR(1) items
Bottom-Up Syntax Analysis
Syntax Analysis Part II
Syntax Analysis - LR(1) and LALR(1) Parsing
LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005.
Parsing #2 Leonidas Fegaras.
Chapter 4. Syntax Analysis (2)
Compiler SLR Parser.
Parsing #2 Leonidas Fegaras.
Kanat Bolazar February 16, 2010
Chapter 4. Syntax Analysis (2)
Chap. 3 BOTTOM-UP PARSING
Presentation transcript:

Cse321, Programming Languages and Compilers 1 6/12/2015 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

Cse321, Programming Languages and Compilers 2 6/12/2015 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. – 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

Cse321, Programming Languages and Compilers 3 6/12/2015 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 Non- terminals that are in the Follow set. Examples: [ Start →. Exp, EOF] [ F → T. * F, +] Start → E E → E + T | T T → T * F | F F → ( E ) | id

Cse321, Programming Languages and Compilers 4 6/12/2015 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

Cse321, Programming Languages and Compilers 5 6/12/2015 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)

Cse321, Programming Languages and Compilers 6 6/12/2015 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  NonTerminal 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

Cse321, Programming Languages and Compilers 7 6/12/2015 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

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

Cse321, Programming Languages and Compilers 9 6/12/2015 Construction of GOTO table If GOTO(c i,A) = c j 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] })

Cse321, Programming Languages and Compilers 10 6/12/2015 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

Cse321, Programming Languages and Compilers 11 6/12/2015 ml-yacc makes a virtue out of Ambiguity Why not: expr -> expr + expr | expr * expr | Number Ambiguity ! ! ! expr Number + 17 Number 3 expr * 2

Cse321, Programming Languages and Compilers 12 6/12/2015 Factoring - A hard solution Fix the grammar E : E + T | T ; T : T * F | F ; F : ( E ) | Number ; Problems –Grammar is harder to understand –Grammar is bigger E E +T T T*F F Number

Cse321, Programming Languages and Compilers 13 6/12/2015 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

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

Cse321, Programming Languages and Compilers 15 6/12/2015 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 ) | id

Cse321, Programming Languages and Compilers 16 6/12/ 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

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

Cse321, Programming Languages and Compilers 18 6/12/2015 State of the stack 2 Stack input... Exp * Exp * 3 EOF Choices Action(8,*) shift 4 Action(8,*) reduce by 2 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) { [E → E. + E, _ ], [E → E. * E, _ ], [E → E * E., _ ] }

Cse321, Programming Languages and Compilers 19 6/12/2015 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

Cse321, Programming Languages and Compilers 20 6/12/2015 Partial Ml-yacc file %left TIMES %left PLUS % Start: E EOF ( E ) E : E PLUS E ( Add(E1,E2) ) | E TIMES E ( Mult(E1,E2) ) | LP E RP ( E ) | id ( Id id ) Much more about ml-yacc next time!

Cse321, Programming Languages and Compilers 21 6/12/2015 If-then-else example Goal → Stmt 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

Cse321, Programming Languages and Compilers 22 6/12/2015 State of machine Stack input... IF Exp THEN Stmt 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

Cse321, Programming Languages and Compilers 23 6/12/2015 Ml-yacc file %nonassoc THEN %nonassoc ELSE % Goal: Stmt EOF ( Stmt ) Stmt: IF EXP THEN Stmt ( IfThen(E,Stmt) ) | IF EXP THEN Stmt ELSE Stmt ( IfThenElse(E,Stmt1,Stmt2) ) | ID ASSIGNOP EXP ( Assign(ID,E) )

Cse321, Programming Languages and Compilers 24 6/12/2015 Some sample ambiguous grammars These examples can be found in the directory