Bottom-up Parsing. Task Parsing (of course); but do it this way: Bottom-up Easy and algorithmic Efficiently –Knowing (input) as little as possible –Marking.

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 Sohail Aslam Lecture StackInput ¤0¤0 id – id  id $ s4 ¤0 id 4 – id  id $ r6 F → id ¤0F3¤0F3 – id  id $ r5 T → F ¤0T2¤0T2.
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
 CS /11/12 Matthew Rodgers.  What are LL and LR parsers?  What grammars do they parse?  What is the difference between LL and LR?  Why do.
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
Mooly Sagiv and Roman Manevich School of Computer Science
LR Parsing Table Costruction
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.
Top-Down Parsing. Task Parsing (of course); but do it: Top-Down Easy and algorithmic Efficiently –Knowing (input) as little as possible –Marking errors.
More SLR /LR(1) Professor Yihjia Tsai Tamkang University.
Bottom-Up Syntax Analysis Mooly Sagiv & Greta Yorsh Textbook:Modern Compiler Design Chapter (modified)
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.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
1 LR parsing techniques SLR (not in the book) –Simple LR parsing –Easy to implement, not strong enough –Uses LR(0) items Canonical LR –Larger parser but.
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3.
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.
LR Parsing Compiler Baojian Hua
LR(k) Parsing CPSC 388 Ellen Walker Hiram College.
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.
1 Compiler Construction Syntax Analysis Top-down parsing.
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.
1 LR Parsers  The most powerful shift-reduce parsing (yet efficient) is: LR(k) parsing. LR(k) parsing. left to right right-most k lookhead scanning derivation.
Chapter 3-3 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR 
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)
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
March 1, 2009 Dr. Muhammed Al-Mulhem 1 ICS 482 Natural Language Processing LR Parsing Muhammed Al-Mulhem March 1, 2009.
Announcements/Reading
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The.
4. Bottom-up Parsing Chih-Hung Wang
Errors in Top-Down Parsing Teoría de Autómatas y Lenguajes Formales M. Luisa González Díaz Universidad de Valladolid, 2005.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
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.
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.
CPSC46001 Bottom-up Parsing Reading Sections 4.5 and 4.7 from ASU.
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 error handling –Errors can occur at many levels lexical: unknown operator syntactic: unbalanced parentheses semantic: variable never declared runtime:
Bottom-up parsing Goal of parser : build a derivation
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
Unit-3 Bottom-Up-Parsing.
LALR Parsing Canonical sets of LR(1) items
Bottom-Up Syntax Analysis
Canonical LR Parsing Tables
LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005.
Bison Marcin Zubrowski.
4d Bottom Up Parsing.
Parsing #2 Leonidas Fegaras.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Parsing #2 Leonidas Fegaras.
4d Bottom Up Parsing.
Kanat Bolazar February 16, 2010
4d Bottom Up Parsing.
4d Bottom Up Parsing.
4d Bottom Up Parsing.
4d Bottom Up Parsing.
Lecture 11 LR Parse Table Construction
Lecture 11 LR Parse Table Construction
Presentation transcript:

Bottom-up Parsing

Task Parsing (of course); but do it this way: Bottom-up Easy and algorithmic Efficiently –Knowing (input) as little as possible –Marking errors as soon as possible Using a stack explicitly

Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num Lex. An. array[numptptnum

Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple array[numptptnum Lex. An.

Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum array[simple] Lex. An. ofchar Reduction r6 r6

Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum simple array[simple]ofchar Lex. An. r6

Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum char simple array[simple]of simple Lex. An. Reduction r5 r5 r6

Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum char simple array[simple]ofsimple type Lex. An. r5 r6

Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum char simple array[simple]of type Lex. An. type Reduction r1 r1 r5 r6

Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum char simple array[simple]oftype Lex. An. r1 r5 r6

Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum char simple type ]ofarray[ type $ (no more) Lex. An. $ type r1 r5 r6 Reduction r3 r3

array [ num ptpt num ] of char $$ $ array $ array [ $ array [ num $ array [ num ptpt $ array [ num ptpt num $ array [ simple $ array [ simple ] $ array [ simple ] of $ array [ simple ] of char $ array [ simple ] of simple $ array [ simple ] of type $ type stack [ num ptpt num ] of char $ num ptpt num ] of char $ ptpt num ] of char $ num ] of char $ ] of char $ of char $ char $ $ $ $ $ input shift reduce 6 shift reduce 5 reduce 1 reduce 3 OK action

simple numptptnum array [ num ptpt num ] of char Lex. An. array[numptptnum

simple numptptnum char simple array [ num ptpt num ] of char array [ simple ] of char Lex. An. array[simple]ofchar

simple numptptnum char simple type array [ num ptpt num ] of char array [ simple ] of char array [ simple ] of simple Lex. An. array[simple of]

simple numptptnum char simple type array [ num ptpt num ] of char array [ simple ] of char array [ simple ] of simple Lex. An. array[typesimpleof] array [ simple ] of type ]ofarray[ type

simple numptptnum char simple type array [ num ptpt num ] of char array [ simple ] of char array [ simple ] of simple Lex. An. array [ simple ] of type ]ofarray[ type

simple numptptnum char simple type array [ num ptpt num ] of char array [ simple ] of char array [ simple ] of simple Lex. An. array [ simple ] of type ]ofarray[ type ═> rm ═> rm ═> rm ═> rm terminals

Wrong example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num Lex. An. array[numptptnum

Wrong Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple array[numptptnum Lex. An.

Wrong Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum array[simple Lex. An.

Wrong Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum array[simple Lex. An. type

Wrong Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum array[ type Lex. An. type ]ofchar

Wrong Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum simple array[type]ofchar Lex. An. type

Wrong Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum char simple array[type]of simple Lex. An. type

Wrong Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum char simple array[type]ofsimple type Lex. An. type

Wrong Example type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num simple numptptnum char simple array[type]of type Lex. An. type $ ?

array [ num ptpt num ] of char array [ simple ] of char array [ simple ] of simple array [ simple ] of type simple numptptnum char simple type ]ofarray[ type ═> 6 ═> 5 ═> 1 ═> 3 handle

array [ num ptpt num ] of char $$ $ array $ array [ $ array [ num $ array [ num ptpt $ array [ num ptpt num $ array [ simple $ array [ simple ] $ array [ simple ] of $ array [ simple ] of char $ array [ simple ] of simple $ array [ simple ] of type $ type stack [ num ptpt num ] of char $ num ptpt num ] of char $ ptpt num ] of char $ num ] of char $ ] of char $ of char $ char $ $ $ $ $ input shift reduce 6 shift reduce 5 reduce 1 reduce 3 OK action handle

What is the handle? Shift or Reduce? If Reduce, which rule? = Knowing read input (and actions done), and (at most) the next input symbol

Some tips type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num reduce type → simple Don’t reduce type → simple after [ When there is simple at the top of the stack : after of when the next input symbol is $ or mark error if the next input symbol is not ] nor $ Marking errors as soon as possible Knowing (input) as little as possible

How to decide shift-reduce type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num At the beginning (state 0): –Shift on integer, char, num. (state 1) –Shift on array (state2) –otherwise Mark error State1 (integer, char, num on top): –If num on top, shift on ptpt (state 3), mark error otherwise –Else, reduce by 4 or 5,depending on the symbol at the top State 2 (array on top) : –Shift on [ (state 4) –Mark error otherwise State 3 (num ptpt on top) –Shift on num (state 5) –Mark error otherwise … Let Yacc do it

S → c S A d | d A → a B | a B → a | b S SAdc da S SAdc da Adc S B a a c d a d c c d a a d a d c S a d c S A d S S → d A → a S → cSAd

S SAdc da c d a d $$ $ c $ c d $ c S $ c S a $ c S A $ c S A d $ S stack d a d $ a d $ d $ $ $ input shift reduce 2: S → d shift reduce 4: A → a shift reduce 1: S → cSAd OK action c d a d c S a d c S A d S S → d A → a S → cSAd dc da S SA ═> rm ═> rm ═> rm

c d a d $$ $ c $ c d $ c S $ c S a $ c S A $ c S A d $ S stack d a d $ a d $ d $ $ $ input shift reduce 2: S → d shift reduce 4: A → a shift reduce 1: S → cSAd OK action LR (1)LALR(1) ∩ YACC →

S SAdc da c d a d $$ 0 $ 0 c 1 $ 0 c 1 d 2 $ 0 c 1 S 4 $ 0 c 1 S 4 a 5 $ 0 c 1 S 4 A 6 $ 0 c 1 S 5 A 6 d 10 $ 0 S 3 stack d a d $ a d $ d $ $ $ input shift 1 shift 2 reduce 2: S → d; goto [1, S] = 4 shift 5 reduce 4: A → a; goto [4, A] = 6 shift 10 reduce 1: S → cSAd; goto [0, S] = 3 OK action dc da S SA ActionGoTo abcd$SAB 0s1s23 1s1s24 2r2 3OK 4s56 5s7s8r49 6s10 7r5 8r6 9r3 10r1

S SAdc da c d a d $$ 0 $ 0 c 1 $ 0 c 1 d 2 $ 0 c 1 S 4 $ 0 c 1 S 4 a 5 $ 0 c 1 S 4 A 6 $ 0 c 1 S 5 A 6 d 10 $ 0 S 3 stack d a d $ a d $ d $ $ $ input shift 1 shift 2 reduce 2: S → d; goto [1, S] = 4 shift 5 reduce 4: A → a; goto [4, A] = 6 shift 10 reduce 1: S → cSAd; goto [0, S] = 3 OK action dc da S SA ActionGoTo abcd$SAB 0s1s23 1s1s24 2r2 3OK 4s56 5s7s8r49 6s10 7r5 8r6 9r3 10r1

ActionGoTo abcd$SAB 0s1s23 1s1s24 2r2 3OK 4s56 5s7s8r49 6s10 7r5 8r6 9r3 10r1 0: $accept :. S $end (0) 1: S : c. S A d (1) 2: S : d. (2) 4: S : c S. A d (1) 3: $accept : S. $end (0) 5: A : a. B (3) A : a. (4) 6: S : c S A. d (1) 7: B : a. (5) 8: B : b. (6) 9: A : a B. (3) 10: S : c S A d. (1) (Rule number) State number Meaning

S SAdc da c d a d $$ 0 $ 0 c 1 $ 0 c 1 d 2 $ 0 c 1 S 4 $ 0 c 1 S 4 a 5 $ 0 c 1 S 4 A 6 $ 0 c 1 S 5 A 6 d 10 $ 0 S 3 stack d a d $ a d $ d $ $ $ input shift 1 shift 2 reduce 2: S → d; goto [1, S] = 4 shift 5 reduce 4: A → a; goto [4, A] = 6 shift 10 reduce 1: S → cSAd; goto [0, S] = 3 OK action dc da S SA ActionGoTo abcd$SAB 0s1s23 1s1s24 2r2 3OK 4s56 5s7s8r49 6s10 7r5 8r6 9r3 10r1 0: $accept :. S $end (0) 1: S : c. S A d (1) 2: S : d. (2)

S SAdc da c d a d $ stack d a d $ a d $ d $ $ $ input shift 1 shift 2 reduce 2: S → d; goto [1, S] = 4 shift 5 reduce 4: A → a; goto [4, A] = 6 shift 10 reduce 1: S → cSAd; goto [0, S] = 3 OK action dc da S SA ActionGoTo abcd$SAB 0s1s23 1s1s24 2r2 3OK 4s56 5s7s8r49 6s10 7r5 8r6 9r3 10r1

S → cAba | cbbb | bA A → b S cbaA b S cbbb S bA b S═>cAbacbba═>S cbbbS═>bAbb═> b$bbb $ Lex. An. Conflicts

$ S cbaA b S cbbb S bA b S═>cAbacbba═>S cbbbS═>bAbb═> Lex. An. $b $ A S → cAba | cbbb | bA A → b

$ Conflicts S cbaA b S cbbb S bA b S═>cAbacbba═>S cbbbS═>bAbb═> Lex. An. $S $ S → cAba | cbbb | bA A → b

Conflicts S cbaA b S cbbb S bA b S═>cAbacbba═>S cbbbS═>bAbb═> Lex. An. c$c b b b shift/reduce conflict S → cAba | cbbb | bA A → b

Conflicts S cbaA b S cbbb S═>cAbacbba═>S cbbb Lex. An. $c b b shift/reduce conflict 4: shift/reduce conflict (shift 8, reduce 4) on ‘b’ state 4 S :’c’ ‘b’. ’b’ ‘b’ (2) A : ‘b’. (4) S → cAba | cbbb | bA A → b

Conflicts S cbaA b S═>cAbacbba═> S → cAba | cBbb | bB A → b B → ε c Lex. An. $ c b shift/reduce conflict S cbbB ε S═>cBbbc bb═>

Conflicts S cbaA b S═>cAbacbba═> S → cAba | cBbb | bB A → b B → ε S cbbB ε S═>cBbbc bb═> c Lex. An. $ c b shift/reduce conflict 1: shift/reduce conflict (shift 4, reduce 5) on ‘b’ state 1 S :’c’. A ’b’ ‘a’ (1) S :’c’. B ’b’ ‘b’ (2) B :. (5)

Conflicts S cbaA b S═>cAbacbba═> S cbbB b S cBbbcbbb═> S → cAba | cBbb | bB A → b | a B → b c Lex. An. $ c b b b reduce/reduce conflict

Conflicts S cbaA b S═>cAbacbba═> S cbbB b S cBbbcbbb═> S → cAba | cBbb | bB A → b | a B → b Lex. An. $ cb b reduce/reduce conflict 4: reduce/reduce conflict (reduce 4, reduce 6) on ‘b’ state 4 A : ‘b’. (4) B : ‘b’. (6)