Lecture 9 SLR Parse Table Construction

Slides:



Advertisements
Similar presentations
Compiler Theory – 08/10(ppt) By Anindhya Sankhla 11CS30004 Group : G 29.
Advertisements

Compiler Principles Fall Compiler Principles Lecture 4: Parsing part 3 Roman Manevich Ben-Gurion University.
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,
Review: LR(k) parsers a1 … a2 … an $ LR parsing program Action goto Sm xm … s1 x1 s0 output input stack Parsing table.
Compiler Construction Sohail Aslam Lecture Finite Automaton of Items Then for every item A →  X  we must add an  -transition for every production.
Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)
Lecture 10 YACC – Yet Another Compiler Compiler Introduction to YACC and Bison Topics Yacc/Bison IntroductionReadings: February 13, 2006 CSCE 531 Compiler.
– 1 – CSCE 531 Spring 2006 Lecture 9 SLR Parse Table Construction Topics SLR Parse Table Construction Sets of Items Closure(I) Readings: 4.7 Homework:
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.
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
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.
Chapter 4-2 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR Other.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
– 1 – CSCE 531 Spring 2006 Lecture 8 Bottom Up Parsing Topics Overview Bottom-Up Parsing Handles Shift-reduce parsing Operator precedence parsing Readings:
LALR Parsing Canonical sets of LR(1) items
 an efficient Bottom-up parser for a large and useful class of context-free grammars.  the “ L ” stands for left-to-right scan of the input; the “ R.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019.
CS 321 Programming Languages and Compilers Bottom Up Parsing.
 an efficient Bottom-up parser for a large and useful class of context-free grammars.  the “ L ” stands for left-to-right scan of the input; the “ R.
Chapter 3-3 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR 
Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Parsing V LR(1) Parsers. LR(1) Parsers LR(1) parsers are table-driven, shift-reduce parsers that use a limited right context (1 token) for handle recognition.
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.
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.
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 
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.
COMPILER CONSTRUCTION
Announcements/Reading
Lec04-bottomupparser 4/13/2018 LR Parsing.
Programming Languages Translator
Compiler design Bottom-up parsing Concepts
Bottom-Up Parsing.
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
Table-driven parsing Parsing performed by a finite state machine.
Syntactic Analysis and Parsing
CS 404 Introduction to Compiler Design
Compiler Construction
Fall Compiler Principles Lecture 4: Parsing part 3
LALR Parsing Canonical sets of LR(1) items
UNIT 2 - SYNTAX ANALYSIS Role of the parser Writing grammars
Bottom-Up Syntax Analysis
Simple, efficient;limitated
Canonical LR Parsing Tables
Syntax Analysis Part II
Subject Name:COMPILER DESIGN Subject Code:10CS63
Top-Down Parsing CS 671 January 29, 2008.
Lecture 7 Predictive Parsing
Syntax Analysis — Parser
Lecture 8 Bottom Up Parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler SLR Parser.
Lecture 7 Predictive Parsing
Syntax Analysis - 3 Chapter 4.
Kanat Bolazar February 16, 2010
Announcements HW2 due on Tuesday Fall 18 CSCI 4430, A Milanova.
Compiler Construction
Chap. 3 BOTTOM-UP PARSING
Lecture 11 LR Parse Table Construction
Presentation transcript:

Lecture 9 SLR Parse Table Construction CSCE 531 Compiler Construction Lecture 9 SLR Parse Table Construction Topics SLR Parse Table Construction Sets of Items Closure(I) Readings: 4.7 Homework: Test 1 – Feb 27 February 15, 2018

Last Time Panic mode error recovery in Predictive parsing Overview Bottom-Up Parsing Handles Shift-reduce parsing Today’s Lecture Sets of Items / Closure / GOTO (J, X) LR(0) sets of items construction SLR parser table construction Homework: LL(1) table for core (pdf email handout) grammar Test 1 Feb 27 !!! Reference: ξ is the greek letter Xi http://www.mathacademy.com/pr/prime/articles/greek/index.asp

Slides in Lecture Review Model of an LR parser LR Parse table (Expressions) Constructing SLR Parse Tables Sets of Items / Closure Example Goto Operation and example Canonical LR(0) sets-of-items (fig 4.35) Valid Items/Viable prefixes SLR table construction Example Example 4.38 Example 4.39 Bison/Flex Overview picture Bison specification files

Chomsky Normal form CNF .

Recall - Model of an LR Parser input a1 … ai an$ output Stack sm Xm sm-1 Xm-1 … s0 LR Parsing Program Parsing Table Action goto

Fig 4.36 LR Parsing Algorithm

Expression LR-Parsing Table fig 4.31 State Action goto Id + * ( ) $ E T F S5 S4 1 2 3 S6 accept R2 S7 R4 4 8 5 R6 6 9 7 10 S11 R1 R3 11 R5

Constructing SLR Parse Tables As with LL(1) or Predictive Parsing table construction we will use FIRST and FOLLOW. We will construct an automata for recognizing handles. This will be similar to subset construction, NFA DFA We use “items” to represent partially matched productions. The sets of items are sort of the collection of productions we are working on matching. Grammars For top-down we avoid left recursion and left factor For LR we avoid right recursion (in some cases we will ignore)

Items An item is a production with a “dot” somewhere on the right hand side. Examples: A  B C D E Yields the following items A  . B C D E A  B . C D E A  B C . D E A  B C D . E A  B C D E . Also N  ε generates the item N  .

Sets of Items: Closure fig 4.33 If J is a set of items for a grammar G, then closure(J) is the set of items constructed from J by the two rules: Initially, every item in J is added to closure(J) If A  α . Bβ is in closure(J) and B  η is a production then add B  . η Apply this rule until no new items can be added to the set. Example: Assume D xyF | Bc and BaB | Dd are the productions then Closure(ABC.DE) = {ABC.DE, D.xyF, D . Bc, B.aB, B.Dd }

Sets-of-items GOTO(J, X) If J is a set of items and X is a grammar symbol then GOTO(J, X) is the closure of the set of all items of the form [AαX .β] such that [Aα . Xβ] is an item in J. GOTO(J, X) = closure ({AαX .β | Aα . Xβ is in J} ) Example given E  E + E | E * E | id If J = { [E  E . + E ], [E  . id] } then GOTO (J, +) = closure({[E  E + . E ] } ) = {[E  E + . E ], …

Augmentation and Kernel Items To facilitate recognition of the sucessful end of a parse we will “augment the grammar” adding a new start symbol S’, and a new production S’  S Items with the dot not at the left end and the initial item S’.S are called kernel items. All others will be referred to as non-kernel. Note non-kernel items will have the dot on the left.

LR(0) Sets-of-Items Construction Figure 4.34 Procedure items(G’) Begin C = { closure({ S’ . S} ) } repeat for each set of items J in C and each grammar symbol X such that GOT(J, X) is nonempty and not in C add GOT(J, X) to C until no more sets of items can be added to C end

Examples LR(0) sets-of-items Grammar 4.35 generates LR(0) items in fig 4.35 (p 225). Example 4.39 (p229) Example D  T ; | T R ; D T  int | float | char R  id | id [ const ] | * id First augment the grammar with S’  D Then …

Valid Items/Viable prefixes The set of prefixes of right sentential forms that can appear on the stack are called viable prefixes. Alternately it is one that is capable of being extended to a handle. Alternately it is a prefix of a right sentential form that does not extend beyond the right end of the rightmost handle. An item [ Aβ1 . β2 ] is valid for a viable prefix αβ if there is a derivation S’  α A w  α β1 β2 w An item may be valid for many viable prefixes

Valid Items  Parsing Actions Valid items indicate possible parsing actions If A  β . Is valid  the action is reduce by A β If [ Aβ1 . β2 ] is valid  the action is shift. Contradictory actions yield conflicts.

SLR table construction Alg 4.8 Augment G to obtain G’. Construct C = {I0, I1, …In} the LR(0) items for G’. State i corresponds to Ii. The parsing actions for state i are determined as follows: If [ Aα . a β ] is in Ii and GOTO(Ii, a) = Ij then set action[i, a] = “shift and goto state j” or just “shift j” If [ Aα . ] is in Ii then set action[i, a] = “reduce Aα” for all a in FOLLOW(A). (here A !=S’) If [ S’S . ] is in Ii then set action[i, $] = “accept.”

Examples of SLR Parse Table Construction Grammar Sets-of-Items Parse Table Example 4.39, pp 229 Fig 4.37, pp 229 Conflict bottom pp229 Example 4.38, pp 228 Fig 4.35, pp 225 Fig 4.31, pp 219 Example Grammar 4.42 S’  S S  C C C  c C | d

LR(1) Parsers A table-driven LR(1) parser looks like Tables can be built by hand However, this is a perfect task to automate Scanner Table-driven Parser ACTION & GOTO Tables Generator source code grammar IR

YACC/Bison Yet Another Compiler Compiler Stephen Johnson 1976 Takes grammar specification and generates the Action and GOTO tables

YACC Format and Usage First Bison = new and improved YACC YACC Format Definitions section %% productions / semantic actions section routines

Example (grammar & sets) Simplified, right recursive expression grammar Is this what we want? Goal  Expr Expr  Term – Expr Expr  Term Term  Factor * Term Term  Factor Factor  ident

Simple0.y in web/Examples/SimpleYacc %token DIGIT %% line : expr '\n' ; expr : expr '+' term | term term : term '*' factor | factor factor : '(' expr ')' | DIGIT

bison simple0.y deneb> deneb> ls -lrt … -rw-r--r-- 1 matthews faculty 28499 Jun 30 12:04 simple0.tab.c deneb> wc simple0.tab.c 1084 4111 28499 simple0.tab.c

gcc simple0.tab.c -ly Undefined first referenced symbol in file yylex /var/tmp//ccW88jE5.o ld: fatal: Symbol referencing errors. No output written to a.out collect2: ld returned 1 exit status

deneb> more simple1.y %token DIGIT %% expr : expr '+' expr | expr '*' expr | '(' expr ')' | DIGIT ;

deneb> bison simple1.y simple1.y contains 4 shift/reduce conflicts. bison -v simple1.y deneb> ls -lrt … -rw-r--r-- 1 matthews faculty 2311 Jun 30 12:10 simple1.output

.output deneb> more simple1.output State 8 contains 2 shift/reduce conflicts. State 9 contains 2 shift/reduce conflicts. Grammar Number, Line, Rule 1 5 expr -> expr '+' expr 2 6 expr -> expr '*' expr 3 7 expr -> '(' expr ')' 4 8 expr -> DIGIT

Terminals, with rules where they appear $ (-1) '(' (40) 3 ')' (41) 3 '*' (42) 2 '+' (43) 1 error (256) DIGIT (257) 4 Nonterminals, with rules where they appear expr (8) on left: 1 2 3 4, on right: 1 2 3

state 0 DIGIT shift, and go to state 1 '(' shift, and go to state 2 expr go to state 3 state 1 expr -> DIGIT . (rule 4) $default reduce using rule 4 (expr) 4

state 2 expr -> '(' . expr ')' (rule 3) DIGIT shift, and go to state 1 '(' shift, and go to state 2 expr go to state

state 3 expr -> expr . '+' expr (rule 1) expr -> expr . '*' expr (rule 2) $ go to state 10 '+' shift, and go to state 5 '*' shift, and go to state 6 … for states 4 through 7

Conflicts state 8 expr -> expr . '+' expr (rule 1) '+' shift, and go to state 5 '*' shift, and go to state 6 '+' [reduce using rule 1 (expr)] '*' [reduce using rule 1 (expr)] $default reduce using rule 1 (expr)

state 9 expr -> expr . '+' expr (rule 1) expr -> expr . '*' expr (rule 2) expr -> expr '*' expr . (rule 2) '+' shift, and go to state 5 '*' shift, and go to state 6 '+' [reduce using rule 2 (expr)] '*' [reduce using rule 2 (expr)] $default reduce using rule 2 (expr)

state 10 $ go to state 11 state 11 $default accept