Bottom-Up Syntax Analysis Mooly Sagiv html://www.math.tau.ac.il/~msagiv/courses/wcc03.html Textbook:Modern Compiler Design Chapter 2.2.5.

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

Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Compiler construction in4020 – lecture 4 Koen Langendoen Delft University of Technology The Netherlands.
Compilation (Semester A, 2013/14) Lecture 6a: Syntax (Bottom–up parsing) Noam Rinetzky 1 Slides credit: Roman Manevich, Mooly Sagiv, Eran Yahav.
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
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
Recap Roman Manevich Mooly Sagiv. Outline Subjects Studied Questions & Answers.
CSE 5317/4305 L4: Parsing #21 Parsing #2 Leonidas Fegaras.
Mooly Sagiv and Roman Manevich School of Computer Science
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.
Recap Mooly Sagiv. Outline Subjects Studied Questions & Answers.
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Bottom-Up Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter (modified)
CS Summer 2005 Top-down and Bottom-up Parsing - a whirlwind tour June 20, 2005 Slide acknowledgment: Radu Rugina, CS 412.
Context-Free Grammars Lecture 7
Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3.
Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter 2.2 (Partial) Hashlama 11:00-14:00.
Bottom-Up Syntax Analysis Mooly Sagiv & Greta Yorsh Textbook:Modern Compiler Design Chapter (modified)
Bottom Up Parsing.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter 2.2 (Partial)
Bottom-Up Syntax Analysis Mooly Sagiv & Greta Yorsh Textbook:Modern Compiler Design Chapter (modified)
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3.
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
Parser construction tools: YACC
Syntax and Semantics Structure of programming languages.
LR Parsing Compiler Baojian Hua
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
Syntax and Semantics Structure of programming languages.
Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter 2.2 (Partial) 1.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
Compiler Principles Fall Compiler Principles Lecture 6: Parsing part 5 Roman Manevich Ben-Gurion University.
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.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Yet Another Compiler-Compiler Stephen C. Johnson July 31, 1978 YACC.
Compiler Principles Fall Compiler Principles Lecture 5: Parsing part 4 Roman Manevich Ben-Gurion University.
4. Bottom-up Parsing Chih-Hung Wang
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.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 6: LR grammars and automatic parser generators.
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 
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
COMPILER CONSTRUCTION
Syntax and Semantics Structure of programming languages.
Programming Languages Translator
Compiler design Bottom-up parsing Concepts
50/50 rule You need to get 50% from tests, AND
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
Unit-3 Bottom-Up-Parsing.
Textbook:Modern Compiler Design
Table-driven parsing Parsing performed by a finite state machine.
Chapter 4 Syntax Analysis.
Context-free Languages
Fall Compiler Principles Lecture 4: Parsing part 3
Bottom-Up Syntax Analysis
Regular Grammar - Finite Automaton
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Kanat Bolazar February 16, 2010
Compiler Construction
Compiler design Bottom-up parsing: Canonical LR and LALR
Presentation transcript:

Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter 2.2.5

Efficient Parsers Pushdown automata Deterministic Report an error as soon as the input is not a prefix of a valid program Not usable for all context free grammars bison context free grammar tokens parser “Ambiguity errors” parse tree

Kinds of Parsers Top-Down (Predictive Parsing) LL –Construct parse tree in a top-down matter –Find the leftmost derivation –For every non-terminal and token predict the next production Bottom-Up LR –Construct parse tree in a bottom-up manner –Find the rightmost derivation in a reverse order – For every potential right hand side and token decide when a production is found

Bottom-Up Syntax Analysis Input –A context free grammar –A stream of tokens Output –A syntax tree or error Method –Construct parse tree in a bottom-up manner –Find the rightmost derivation in (reversed order) –For every potential right hand side and token decide when a production is found –Report an error as soon as the input is not a prefix of valid program

Plan Pushdown automata Bottom-up parsing (informal) Bottom-up parsing (given a parser table) Constructing the parser table Interesting non LR grammars

Pushdown Automaton control parser-table input stack $ $utw V

Informal Example S  E$ E  T | E + T T  i | ( E ) i + i $ input $ stack tree input + i $ i$i$ stack tree i shift

Informal Example S  E$ E  T | E + T T  i | ( E ) input + i $ i$i$ stack tree i reduce T  i input + i $ T$T$ stack tree i T

Informal Example S  E$ E  T | E + T T  i | ( E ) input + i $ T$T$ stack tree i reduce E  T T input + i $ E$E$ stack tree i T E

Informal Example S  E$ E  T | E + T T  i | ( E ) shift input + i $ E$E$ stack tree i T E input i $ +E$+E$ stack tree i T E +

Informal Example S  E$ E  T | E + T T  i | ( E ) shift input i $ +E$+E$ stack tree i T E + input $ i+ E $ stack tree i T E + i

Informal Example S  E$ E  T | E + T T  i | ( E ) reduce T  i input $ i+ E $ stack tree i T E + i input $ T+E$T+E$ stack tree i T E + i T

Informal Example S  E$ E  T | E + T T  i | ( E ) reduce E  E + T input $ T+E$T+E$ stack tree i T E + i T input $ E$E$ stack tree i T E + T E

Informal Example S  E$ E  T | E + T T  i | ( E ) input $ E$E$ stack tree i T E + T E shift input $E$$E$ stack tree i T E + T E

Informal Example S  E$ E  T | E + T T  i | ( E ) input $ $E$$E$ stack tree i T E + T E reduce Z  E $

Informal Example reduce E  E + T reduce T  i reduce E  T reduce T  i reduce Z  E $

Handles Identify the leftmost node (nonterminal) that has not been constructed but all whose children have been constructed –A  

Identifying Handles Create a finite state automaton over grammar symbols –Accepting states identify handles Report if the grammar is inadequate Push automaton states onto parser stack Use the automaton states to decide –Shift/Reduce

Example Finite State Automaton

Example Control Table

i+()$ET 0s5errs7err 16 1 s3err s2 2acc 3s5errs7err 4 4 reduce E  E+T 5 reduce T  i 6 reduce E  T 7s5errs7err 86 8 s3errs9err 9 reduce T  (E) Example Control Table (2)

i + i $ input s0($) stack shift 5 stack i+()$ET 0s5errs7err 16 1 s3err s2 2acc 3s5errs7err 4 4 reduce E  E+T 5 reduce T  i 6 reduce E  T 7s5errs7err 86 8 s3errs9err 9 reduce T  (E)

+ i $ input s5 (i) s0 ($) reduce T  i stack i+()$ET 0s5errs7err 16 1 s3err s2 2acc 3s5errs7err 4 4 reduce E  E+T 5 reduce T  i 6 reduce E  T 7s5errs7err 86 8 s3errs9err 9 reduce T  (E)

+ i $ input s6 (T) s0 ($) reduce E  T stack i+()$ET 0s5errs7err 16 1 s3err s2 2acc 3s5errs7err 4 4 reduce E  E+T 5 reduce T  i 6 reduce E  T 7s5errs7err 86 8 s3errs9err 9 reduce T  (E)

+ i $ input s1(E) s0 ($) shift 3 stack i+()$ET 0s5errs7err 16 1 s3err s2 2acc 3s5errs7err 4 4 reduce E  E+T 5 reduce T  i 6 reduce E  T 7s5errs7err 86 8 s3errs9err 9 reduce T  (E)

i $ input s3 (+) s1(E) s0 ($) shift 5 stack i+()$ET 0s5errs7err 16 1 s3err s2 2acc 3s5errs7err 4 4 reduce E  E+T 5 reduce T  i 6 reduce E  T 7s5errs7err 86 8 s3errs9err 9 reduce T  (E)

$ input s5 (i) s3 (+) s1(E) s0($) reduce T  i i+()$ET 0s5errs7err 16 1 s3err s2 2acc 3s5errs7err 4 4 reduce E  E+T 5 reduce T  i 6 reduce E  T 7s5errs7err 86 8 s3errs9err 9 reduce T  (E)

$ input s4 (T) s3 (+) s1(E) s0($) reduce E  E + T stack i+()$ET 0s5errs7err 16 1 s3err s2 2acc 3s5errs7err 4 4 reduce E  E+T 5 reduce T  i 6 reduce E  T 7s5errs7err 86 8 s3errs9err 9 reduce T  (E)

$ input s1 (E) s0 ($) shift 2 stack i+()$ET 0s5errs7err 16 1 s3err s2 2acc 3s5errs7err 4 4 reduce E  E+T 5 reduce T  i 6 reduce E  T 7s5errs7err 86 8 s3errs9err 9 reduce T  (E)

input s2 ($) s1 (E) s0 ($) reduce Z  E $ stack i+()$ET 0s5errs7err 16 1 s3err s2 2acc 3s5errs7err 4 4 reduce E  E+T 5 reduce T  i 6 reduce E  T 7s5errs7err 86 8 s3errs9err 9 reduce T  (E)

((i) $ input s0($) shift 7 stack i+()$ET 0s5errs7err 16 1 s3err s2 2acc 3s5errs7err 4 4 reduce E  E+T 5 reduce T  i 6 reduce E  T 7s5errs7err 86 8 s3errs9err 9 reduce T  (E)

(i) $ input s7(() s0($) shift 7 stack i+()$ET 0s5errs7err 16 1 s3err s2 2acc 3s5errs7err 4 4 reduce E  E+T 5 reduce T  i 6 reduce E  T 7s5errs7err 86 8 s3errs9err 9 reduce T  (E)

i) $ input s7 (() s0($) shift 5 stack i+()$ET 0s5errs7err 16 1 s3err s2 2acc 3s5errs7err 4 4 reduce E  E+T 5 reduce T  i 6 reduce E  T 7s5errs7err 86 8 s3errs9err 9 reduce T  (E)

) $ input s5 (i) s7 (() s0($) reduce T  i stack i+()$ET 0s5errs7err 16 1 s3err s2 2acc 3s5errs7err 4 4 reduce E  E+T 5 reduce T  i 6 reduce E  T 7s5errs7err 86 8 s3errs9err 9 reduce T  (E)

) $ input s6 (T) s7 (() s0($) reduce E  T stack i+()$ET 0s5errs7err 16 1 s3err s2 2acc 3s5errs7err 4 4 reduce E  E+T 5 reduce T  i 6 reduce E  T 7s5errs7err 86 8 s3errs9err 9 reduce T  (E)

) $ input s8 (E) s7 (() s0($) shift 9 stack i+()$ET 0s5errs7err 16 1 s3err s2 2acc 3s5errs7err 4 4 reduce E  E+T 5 reduce T  i 6 reduce E  T 7s5errs7err 86 8 s3errs9err 9 reduce T  (E)

$ input s9 ()) s8 (E) s7 (() s0($) reduce T  ( E ) stack i+()$ET 0s5errs7err 16 1 s3err s2 2acc 3s5errs7err 4 4 reduce E  E+T 5 reduce T  i 6 reduce E  T 7s5errs7err 86 8 s3errs9err 9 reduce T  (E)

$ input s6 (T) s7(() s0($) reduce E  T stack i+()$ET 0s5errs7err 16 1 s3err s2 2acc 3s5errs7err 4 4 reduce E  E+T 5 reduce T  i 6 reduce E  T 7s5errs7err 86 8 s3errs9err 9 reduce T  (E)

$ input s8 (E) s7(() s0($) err stack i+()$ET 0s5errs7err 16 1 s3err s2 2acc 3s5errs7err 4 4 reduce E  E+T 5 reduce T  i 6 reduce E  T 7s5errs7err 86 8 s3errs9err 9 reduce T  (E)

Grammar Hierarchy Non-ambiguous CFG CLR(1) LALR(1) SLR(1) LL(1) LR(0)

Constructing LR(0) parsing table Add a production S’  S$ Construct a finite automaton accepting “valid stack symbols” States are set of items A   –The states of the automaton becomes the states of parsing-table –Determine shift operations –Determine goto operations –Determine reduce operations

Example Finite State Automaton

Constructing a Finite Automaton NFA –For X  X 1 X 2 … X n X  X 1 X 2 …X i  X i+1 … X n –“prefixes of rhs (handles)” –X 1 X 2 … X i is at the top of the stack and we expect X i+1 … X n The initial state S’   S$ –  (X  X 1 …X i  X i+1 … X n, X i+1 ) = X  X 1 …X i X i+1  … X n –For every production X i+1    (X  X 1 X 2 …X  X i+1 … X n,  ) = X i+1   Convert into DFA

S  E$ E  T | E + T T  i | ( E ) S   E $ E   T E   E + T T   i T   ( E ) E  T  T i T  i  T  (  E ) ( T  (E )  ) E  E  + T E E  E +  T + E  E + T  T S  E  $ E S  E $  $ T  (E  ) E

Example Finite State Automaton

Filling Parsing Table A state s i reduce A  –A    s i Shift –A   t   s i Goto(s i, X) = s j –A   X   s i –  (s i, X) = s j When conflicts occurs the grammar is not LR(0)

Example Finite State Automaton

Example Control Table i+()$ET 0s5errs7err 16 1 s3err s2 2acc 3s5errs7err 4 4 reduce E  E+T 5 reduce T  i 6 reduce E  T 7s5errs7err 86 8 s3errs9err 9 reduce T  (E)

Example S  E $ E  E + E | i S  E$ E  E+E E   i 0 S  E  $ E  E  +E 2 E E  E+  E E  E+E E   i 4 + E  E+ E  E  E  +E 5 E + E  i  i 1 S  E $  $ 3 i

S  E$ E  E+E E   i 0 S  E  $ E  E  +E 2 E E  E+  E E  E+E E   i 4 + E  E+ E  E  E  +E 5 E + E  i  i 1 S  E $  $ 3 i i+$E 0s1err 2 1 reduce E  i 2errs4s3 3accept 4s15 5reds4/redred

Interesting Non LR(0) Grammar S’  S$ S  L = R | R L  *R | id R  L S’  S$ S   L=R S   R L   *R L  id R   L S  L  =R R  L  L = Partial DFA S  L=  R R   L L   *R L  id

LR(1) Parser Item A , t –  is at the top of the stack and we are expecting  t LR(1) State –Sets of items LALR(1) State –Merge items with the same look-ahead

Interesting Non LR(1) Grammars Ambiguous –Arithmetic expressions –Dangling-else Common derived prefix –A  B 1 a b | B 2 a c –B 1   –B 2   Optional non-terminals –St  OptLab Ass –OptLab  id : |  –Ass  id := Exp

A motivating example Create a desk calculator Challenges –Non trivial syntax –Recursive expressions (semantics) Operator precedence

Solution (lexical analysis) /* desk.l */ % [0-9]+ { yylval = atoi(yytext); return NUM ;} “+” { return PLUS ;} “-” { return MINUS ;} “/” { return DIV ;} “*” { return MUL ;} “(“ { return LPAR ;} “)” { return RPAR ;} “//”.*[\n] ; /* comment */ [\t\n ] ; /* whitespace */. { error (“illegal symbol”, yytext[0]); }

Solution (syntax analysis) /* desk.y */ %token NUM %left PLUS, MINUS %left MUL, DIV %token LPAR, RPAR %start P % P : E {printf(“%d\n”, $1) ;} ; E : NUM { $$ = $1 ;} | LPAR e RPAR { $$ = $2; } | e PLUS e { $$ = $1 + $3 ; } | e MINUS e { $$ = $1 - $3 ; } | e MUL e { $$ = $1 * $3; } | e DIV e {$$ = $1 / $3; } ; % #include “lex.yy.c” flex desk.l bison desk.y cc y.tab.c –ll -ly

Summary LR is a powerful technique Generates efficient parsers Generation tools exit –Bison, yacc, CUP But some grammars need to be tuned –Shift/Reduce conflicts –Reduce/Reduce conflicts –Efficiency of the generated parser