CS 321 Programming Languages and Compilers Bottom Up Parsing.

Slides:



Advertisements
Similar presentations
Compiler Principles Fall Compiler Principles Lecture 4: Parsing part 3 Roman Manevich Ben-Gurion University.
Advertisements

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.
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.
1 May 22, May 22, 2015May 22, 2015May 22, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
Predictive Parsing l Find derivation for an input string, l Build a abstract syntax tree (AST) –a representation of the parsed program l Build a symbol.
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.
Lecture #8, Feb. 7, 2007 Shift-reduce parsing,
CS 536 Spring Bottom-Up Parsing: Algorithms, part 1 LR(0), SLR Lecture 12.
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.
CH4.1 CSE244 Sections 4.5,4.6 Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
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.
LR(1) Languages An Introduction Professor Yihjia Tsai Tamkang University.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
Bottom-up parsing Goal of parser : build a derivation
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.
Syntax and Semantics Structure of programming languages.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019.
LR(k) Parsing CPSC 388 Ellen Walker Hiram College.
Chap. 6, Bottom-Up Parsing J. H. Wang May 17, 2011.
1 Natural Language Processing Lecture 11 Efficient Parsing Reading: James Allen NLU (Chapter 6)
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
 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.
CSc 453 Syntax Analysis (Parsing)
CH4.1 CSE244 Sections 4.5,4.6 Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
CMSC 331, Some material © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Chapter 4 Bottom Up Parsing.
Chapter 3-3 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR 
Syntax and Semantics Structure of programming languages.
1 Bottom-Up Parsing  “Shift-Reduce” Parsing  Reduce a string to the start symbol of the grammar.  At every step a particular substring is matched (in.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
111 Chapter 6 LR Parsing Techniques Prof Chung. 1.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
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.
Bottom-Up Parsing Algorithms LR(k) parsing L: scan input Left to right R: produce Rightmost derivation k tokens of lookahead LR(0) zero tokens of look-ahead.
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.
COMPILER CONSTRUCTION
Syntax and Semantics Structure of programming languages.
Bottom-up Parsing.
Programming Languages Translator
Bottom-Up Parsing.
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
Bottom-up Parsing.
Table-driven parsing Parsing performed by a finite state machine.
Fall Compiler Principles Lecture 4: Parsing part 3
LALR Parsing Canonical sets of LR(1) items
Bottom-Up Syntax Analysis
Syntax Analysis Part II
Subject Name:COMPILER DESIGN Subject Code:10CS63
Syntax Analysis source program lexical analyzer tokens syntax analyzer
Parsing #2 Leonidas Fegaras.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Parsing #2 Leonidas Fegaras.
Bottom-Up Parsing “Shift-Reduce” Parsing
Announcements HW2 due on Tuesday Fall 18 CSCI 4430, A Milanova.
Chap. 3 BOTTOM-UP PARSING
Presentation transcript:

CS 321 Programming Languages and Compilers Bottom Up Parsing

Bottom-Up Parsing 2 Bottom-up Parsing: Shift-reduce parsing Grammar H: L  E ; L | E E  a | b Input: a;a;b has parse tree L L ; E L ; E b a a

Bottom-Up Parsing 3 Data for Shift-reduce Parser Input string: sequence of tokens being checked for grammatical correctness Stack: sentential form representing the input seen so far Trees Constructed: the parse trees that have been constructed so far at some point in the parsing

Bottom-Up Parsing 4 Operations of shift-reduce parser Shift: move input token to the set of trees as a singleton tree. Reduce: coalesce one or more trees into single tree (according to some production). Accept: terminate and accept the token stream as grammatically correct. Reject: Terminate and reject the token stream.

Bottom-Up Parsing 5 A parse for grammar H Input Stack Action Trees a;a;b Shift ;a;b a Reduce E  a ;a;b E Shift a;b E ; Shift a EaEa EaEa ;

Bottom-Up Parsing 6 A parse for grammar H Input Stack Action Trees ;b E ;a Reduce E  a ;b E;E Shift b E;E; Shift EaEa ;a EaEa ; EaEa EaEa ; EaEa ;

Bottom-Up Parsing 7 A parse for grammar H Input Stack Action Trees $ E;E; b Reduce E  b $ E;E;E Reduce L  E $ E;E;L Reduce L  E;L EaEa ; EaEa ;b EaEa ; EaEa ; EbEb EaEa ; EaEa ; EbEb L

Bottom-Up Parsing 8 A parse for grammar H Input Stack Action Trees $ E;L Reduce L  E;L $ L Acc EaEa ; EaEa ; EbEb L L EaEa ; EaEa ; EbEb L L L

Bottom-Up Parsing 9 Bottom-up Parsing Characteristic automata for an “LR” grammar: tells when to shift/reduce/accept or reject handles viable prefixes

Bottom-Up Parsing 10 A parse for grammar H Input Stack ActionStack+Input a;a;b Shift a;a;b ;a;b a Reduce E  a a;a;b ;a;b E ShiftE ;a;b a;b E ; Shift E ;a;b ;b E ;a Reduce E  a E ;a;b ;b E;E ShiftE ; E ;b b E;E; ShiftE ; E ;b $ E;E; b Reduce E  b E ; E ;b $ E;E;E Reduce L  EE ; E ; E $ E;E;L Reduce L  E;LE ; E;L $ E;L Reduce L  E;LE;L $ L AcceptL

Bottom-Up Parsing 11 “Bottom-up” parsing? Stack+Input L E;L E ; E;L E ; E ; E E ; E ;b E ;a;b a;a;b EaEa ; EaEa ; EbEb L L L EaEa ; EaEa ; EbEb L L L

Bottom-Up Parsing 12 Handles and viable prefixes Stack+remaining input = sentential form Handle = the part of the sentential form that is reduced in each step Viable prefix = the prefix of the sentential form in a right-most derivation that do not extend beyond the end of the handle E.g. viable prefixes for H: (E;)*(E | L | a | b) Viable prefixes form a regular set.

Bottom-Up Parsing 13 Characteristic Finite State Machine (CFSM) Viable prefixes of H are recognized by this CFSM: E L a b 56 E a b L ;

Bottom-Up Parsing 14 How a Bottom-Up Parser Works Run the CFSM on symbols in the stack If a transition possible on the incoming input symbol, then shift, else reduce. –Still need to decide which rule to use for the reduction.

Bottom-Up Parsing 15 Characteristic automaton E L a b 56 E a b L ; a;a;b leads to state 3 after a E ;a;b leads to state 3 after E ;a E;E; b leads to state 4 after E;E; b E;E;E leads to state 2 after E;E;E E;E;L leads to state 6 after E;E;L E;L leads to state 6 after E;L start Viable Prefixes

Bottom-Up Parsing 16 Characteristic automaton E L a b 56 E a b L ; start State Action 0,5shift (if possible) 1accept 2reduce L  E, if EOF shift otherwise 3reduce E  a 4reduce E  b 6reduce L  E;L

Bottom-Up Parsing 17 Example: expression grammar E  E + T | T T  T * P | P P  id id+id+id+id has parse tree: E T+E T+E T+E T P id P P P

Bottom-Up Parsing 18 A parse in this grammar id+id+id+id Shift +id+id+id id Reduce +id+id+id P Reduce +id+id+id T Reduce +id+id+id E Shift id+id+id E + Shift +id+id E +id Reduce +id+id E + P Reduce +id+id E + T Reduce +id+id E Shift id+id E + Shift

Bottom-Up Parsing 19 A parse in this grammar (cont.) +id E + id Reduce +id E + P Reduce +id E + T Reduce +id E Shift id E + Shift $ E +id Reduce $ E + P Reduce $ E + T Reduce $ E Reduce $ E Accept

Bottom-Up Parsing 20 Characteristic Finite State Machine The CFSM recognizes viable prefixes (strings of grammar symbols that can appear on the stack): id E T P 58 * T + 79 P * P

Bottom-Up Parsing 21 Definitions Rightmost derivation Right-sentential form Handle Viable prefix Characteristic automaton

Bottom-Up Parsing 22 Rightmost Derivation Definition: A rightmost derivation in G is a derivation: such that for each step i, the rightmost non-terminal in is replaced to obtain is right-most is not right-most

Bottom-Up Parsing 23 Right-sentential Forms Definition: A right-sentential form is any sentential form that occurs in a right-most derivation. E.g., any of these

Bottom-Up Parsing 24 Handles Definition: Assume the i-th step of a rightmost derivation is: w i =u i Av i  u i  v i = w i+1 Then, ( , | u i  |) is the handle of w i+1 In an unambiguous grammar, any sentence has a unique rightmost derivation, and so we can talk about “the” handle rather than “a” handle.

Bottom-Up Parsing 25 The Plan Construct a parser by first constructing the CFSM, then constructing GOTO and ACTION tables from it. Construction has two parts: –“LR(0)” construction of the CFSM –“SLR(1)” construction of tables

Bottom-Up Parsing 26 Constructing the CFSM: States The states in the CFSM are created by taking the “closure” of “LR(0) items” L  E ; L Given a production “L  E ; L”, these are all induced “LR(0) items”

Bottom-Up Parsing 27 What is “”? The “ ” in “L  E ; L” represents the state of the parse. L E ; L Only this part of the tree is fully developed

Bottom-Up Parsing 28 A State in the CFSM: Closure of LR(0) Item For set I of LR(0) items, calculate closure(I): 1. if “A   B  ” is in closure(I), then for every production “B   ”, “B   ” is in closure(I) 2. closure(I) is the smallest set with property (1)

Bottom-Up Parsing 29 Closure of LR(0) Item: Example H: L  E ; L | E E  a | b closure({ L  E ; L}) = {L  E ; L, L  E ; L, L  E, E  a, E  b }

Bottom-Up Parsing 30 LR(0) Machine Given grammar G with goal symbol S, augment the grammar by adding new goal symbol S’ and production S’  S. States = sets of LR(0) items Start state = closure({S’  S}) All states are considered to be final (set of viable prefixes closed under prefix) transition(I, X) = closure({A   X  | A   X  I}).

Bottom-Up Parsing 31 Example: LR(0) CFSM Construction H: L’  L L  E ; L | E E  a | b Augment the grammar: Initial State is closure of this “augmenting rule”: closure({ L’  L}) = L’  L L  E ; L L  E E  a E   b

Bottom-Up Parsing 32 Example: Transitions from I0 transition(, L) = {L’  L } = transition(, E) = { L  E ; L, L  E } = transition(, a ) = {E  a } = transition(, b ) = {E  b } = There are no other transitions from I 0. There are no transitions possible from I 1. Now consider the transitions from I 2.

Bottom-Up Parsing 33 Transitions from I 2 transition(, ; ) = L  E ; L L  E E  a E   b transition(, L) = { L  E ; L }= transition(, E) = transition(, a ) = transition(, b ) = New state:

Bottom-Up Parsing 34 The CFSM Transition Diagram for H L  E ; L L  E E  a E   b L’  L L  E ; L L  E E  a E  b L’  L L  E ; L L  E E  a E   b L  E ; L L E a b E a b ; L

Bottom-Up Parsing 35 Characteristic Finite State Machine for H E L a b 56 E a b L ;

Bottom-Up Parsing 36 How LR(1) parsers work GOTO table: transition function of characteristic automaton in tabular form ACTION table: State    Action Procedure: –Use the GOTO table to run the CFSM over the stack. Suppose state reached at top of stack is . –Take action given by ACTION( ,a), where a is incoming input symbol.

Bottom-Up Parsing 37 Action Table for H

Bottom-Up Parsing 38 SLR(1) Parser Construction GOTO table is the move function from the LR(0) CFSM. ACTION table is constructed from the CFSM as follows: –If state i contains A   a , then ACTION(i,a) = Shift. –If state i contains A  , then ACTION(i,a) = Reduce A   (But, if A is L’, action is Accept.) –Otherwise, reject. But,...

Bottom-Up Parsing 39 SLR(1) Parser Construction Rules for the ACTION table can involve shift/reduce conflicts. So the actual rule for reduce actions is: If state i contains A  , then ACTION(i,a) = Reduce A  , for all a  FOLLOW(A). E.g. state 2 for grammar H yields a shift/reduce conflict. Namely, should you shift the “;” or reduce by “L  E”. This is resolved by looking at the “follow set” for L. Follow(L) = {$}

Bottom-Up Parsing 40 FIRST and FOLLOW sets FIRST(  ) = {a   |    a  }  {  | a    } FOLLOW(A) = {a   | S    Aa  }

Bottom-Up Parsing 41 Calculating FIRST sets Create table Fi mapping N to   {  }; initially, Fi(A) =  for all A. Repeat until Fi does not change: –For each A   P, Fi(A) := Fi(A)  FIRST( , Fi) where FIRST( , Fi) is defined as follows: –FIRST( , Fi) = {  } –FIRST(a , Fi) = {a} –FIRST(B , Fi) = Fi(B)  FIRST(b,Fi), if  Fi(B) Fi(B), o.w.

Bottom-Up Parsing 42 Calculating FOLLOW sets Calculate FOLLOW sets Create table Fo : N    {$}; initially, Fo(A) =  for all A, except Fo(S) = {$} Repeat until Fo does not change: –For each production A   B , Fo(B) := Fo(B)  FIRST(  ) - {  } –For each production A   B   Fo(B) := Fo(B)  Fo(A) –For each production A   B  if  FIRST(  ), Fo(B) := Fo(B)  Fo(A)