Bottom-Up Syntax Analysis
|
|
- Emmeline Charles
- 5 years ago
- Views:
Transcription
1 Bottom-Up Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter (modified) 1
2 Pushdown automata Deterministic fficient Parsers Report an error as soon as the is not a prefix of a valid program Not usable for all context free grammars context free grammar cup Ambiguity errors 2 tokens parser parse tree
3 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 3
4 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 is not a prefix of valid program 4
5 Plan Pushdown automata Bottom-up parsing (informal) Non-deterministic bottom-up parsing Deterministic bottom-up parsing Interesting non LR grammars 5
6 Pushdown Automaton u t w V parser-table control 6
7 Informal xample(1) S T + T T i ( ) i tree tree i + i + i shift 7 i
8 Informal xample(2) S T + T T i ( ) tree i + i i reduce T i tree 8 T i T + i
9 Informal xample(3) S T + T T i ( ) tree T i T + i reduce T tree T + i 9 i
10 Informal xample(4) S T + T T i ( ) tree T + i i shift + tree T i 10 i +
11 Informal xample(5) S T + T T i ( ) tree + i T + i shift 11 i+ tree T i + i
12 Informal xample(6) 12 S T + T T i ( ) tree i+ T i + i reduce T i T + i tree T + i T
13 Informal xample(7) T + S T + T tree T i ( ) T T i + i reduce + T tree T + T 13 i i
14 Informal xample(8) S T + T T i ( ) tree T + T shift i i tree 14 i T + T i
15 Informal xample(9) i S T + T tree T i ( ) T + T i reduce S 15
16 Informal xample reduce S reduce + T reduce T i reduce T 16 reduce T i
17 The Problem Deciding between shift and reduce 17
18 Informal xample(7) T + S T + T T i ( ) tree T T i + i reduce + T tree T + T 18 i i
19 Informal xample(7 ) 19 T + + S T + T T i ( ) tree T T i + i reduce T tree T + i
20 Bottom-UP LR(0) Items already matched still need to be matched regular expression T 20 already matched expecting to match
21 LR(0) items ( ) i + T S T + T T i T ( ) 1: S 2 4, 6 2: S s3 3: S r 4: T 5 10, 12 5: T r 6: + T 7 4, 6 7: + T s8 8: + T 9 10, 12 9: + T r 10: T i s11 11: T i r 12: T () s13 13: T ( ) 14 4, 6 14: T ( ) s : T () r
22 Formal xample(1) S T + T T i ( ) 1: S i + i -move : +T 1: S i + i
23 Formal xample(2) S T + T T i ( ) 6: +T 1: S i + i -move : T 6: +T 1: S i + i
24 Formal xample(3) S T + T T i ( ) 4: T 6: +T 1: S i + i -move : T i 4: T 6: +T 1: S i + i
25 Formal xample(4) S T + T T i ( ) 25 10: T i 4: T 6: +T 1: S 11: T i 10: T i 4: T 6: +T 1: S i + i + i shift 11
26 Formal xample(5) S T + T T i ( ) 11: T i 10: T i + i 4: T 6: +T 1: S reduce T i 26 5: T 4: T 6: +T 1: S + i
27 Formal xample(6) S T + T T i ( ) 5: T 4: T 6: +T 1: S + i reduce T 27 7: +T 6: +T 1: S + i
28 Formal xample(7) S T + T T i ( ) 7: +T 6: +T 1: S + i shift : + T 7: +T 6: +T 1: S i
29 Formal xample(8) S T + T T i ( ) 8: + T 7: +T 6: +T 1: S i -move : T i 8: + T 7: +T 6: +T 1: S i
30 Formal xample(9) S T + T T i ( ) 10: T i 8: + T 7: +T 6: +T 1: S i shift : T i 10: T i 8: + T 7: +T 6: +T 1: S
31 Formal xample(10) S T + T T i ( ) 11: T i 10: T i 8: + T 7: +T 6: +T 1: S 9: + T 8: + T 7: +T 6: +T 1: S reduce T i 31
32 Formal xample(11) S T + T T i ( ) 9: + T 8: + T 7: +T 6: +T 1: S 2: S 1: S reduce + T 32
33 Formal xample(12) S T + T T i ( ) 2: S 1: S shift : S 2: S 1: S
34 Formal xample(13) S T + T T i ( ) 3: S 2: S 1: S reduce S 34
35 But how can this be done efficiently? Deterministic Pushdown Automaton 35
36 Handles Identify the leftmost node (nonterminal) that has not been constructed but all whose children have been constructed t 1 t 2 t 4 t 5 t 6 t 7 t 8
37 Identifying Handles Create a deteteministic finite state automaton over grammar symbols Sets of LR(0) items Accepting states identify handles Use automaton to build parser tables reduce For items A on every token shift For items A t on token t When conflicts occur the grammar is not LR(0) When no conflicts occur use a DPDA which pushes states on the 37
38 A Trivial xample S A B A a B a 38
39 ( ) i + T S T + T T i T ( ) 1: S 2 4, 6 2: S s3 3: S r 4: T 5 10, 12 5: T r 6: + T 7 4, 6 7: + T s8 8: + T 9 10, 12 9: + T r 10: T i s11 11: T i r 12: T () s13 13: T ( ) 14 4, 6 14: T ( ) s : T () r
40 : T 11: T i i T i i ( 0 7 1: S 4: T 6: + T 10: T i 12: T () ( 13: T ( ) 4: T 6: + T 10: T i 12: T () 2 ( 3 2: S 7: + T 3: S 8: + T 10: T i 12: T () T 15: T () ) 14: T ( ) 7: + T 4 9: + T 9 40
41 xample Control Table 41 i + ( ) T 0 s5 err s7 err err err s3 err err s2 2 acc 3 s5 err s7 err err 4 4 reduce +T 5 reduce T i 6 reduce T 7 s5 err s7 err err err s3 err s9 err 9 reduce T ()
42 i + ( ) T 0 s5 err s7 err err err s3 err err s2 2 acc 3 s5 err s7 err err 4 4 reduce +T 5 reduce T i 6 reduce T 7 s5 err s7 err err err s3 err s9 err 9 reduce T () 42 0() i + i shift 5
43 43 i + ( ) T 0 s5 err s7 err err err s3 err err s2 2 acc 3 s5 err s7 err err 4 4 reduce +T 5 reduce T i 6 reduce T 7 s5 err s7 err err err s3 err s9 err 9 reduce T () 5 (i) 0 () + i reduce T i
44 i + ( ) T 0 s5 err s7 err err err s3 err err s2 2 acc 3 s5 err s7 err err 4 4 reduce +T 5 reduce T i 6 reduce T 7 s5 err s7 err err err s3 err s9 err 9 reduce T () 6 (T) 0 () + i reduce T 44
45 i + ( ) T 0 s5 err s7 err err err s3 err err s2 2 acc 3 s5 err s7 err err 4 4 reduce +T 5 reduce T i 6 reduce T 7 s5 err s7 err err err s3 err s9 err 9 reduce T () 1() 45 0 () + i shift 3
46 i + ( ) T 0 s5 err s7 err err err s3 err err s2 2 acc 3 s5 err s7 err err 4 4 reduce +T 5 reduce T i 6 reduce T 7 s5 err s7 err err err s3 err s9 err 9 reduce T () 46 3 (+) 1() 0 () i shift 5
47 i + ( ) T 0 s5 err s7 err err err s3 err err s2 2 acc 3 s5 err s7 err err 4 4 reduce +T 5 reduce T i 6 reduce T 7 s5 err s7 err err err s3 err s9 err 9 reduce T () 47 5 (i) 3 (+) 1() 0() reduce T i
48 i + ( ) T 0 s5 err s7 err err err s3 err err s2 2 acc 3 s5 err s7 err err 4 4 reduce +T 5 reduce T i 6 reduce T 7 s5 err s7 err err err s3 err s9 err 48 9 reduce T () 4 (T) 3 (+) 1() 0() reduce + T
49 i + ( ) T 0 s5 err s7 err err err s3 err err s2 2 acc 3 s5 err s7 err err 4 4 reduce +T 5 reduce T i 6 reduce T 7 s5 err s7 err err err s3 err s9 err 9 reduce T () 49 1 () 0 () shift 2
50 i + ( ) T 0 s5 err s7 err err err s3 err err s2 2 acc 3 s5 err s7 err err 4 4 reduce +T 5 reduce T i 6 reduce T 7 s5 err s7 err err err s3 err s9 err 9 reduce T () 50 2 () 1 () 0 () accept
51 i + ( ) T 0 s5 err s7 err err err s3 err err s2 2 acc 3 s5 err s7 err err 4 4 reduce +T 5 reduce T i 6 reduce T 7 s5 err s7 err err err s3 err s9 err 9 reduce T () 51 0() ((i) shift 7
52 i + ( ) T 0 s5 err s7 err err err s3 err err s2 2 acc 3 s5 err s7 err err 4 4 reduce +T 5 reduce T i 6 reduce T 7 s5 err s7 err err err s3 err s9 err 9 reduce T () 52 7(() 0() (i) shift 7
53 i + ( ) T 0 s5 err s7 err err err s3 err err s2 2 acc 3 s5 err s7 err err 4 4 reduce +T 5 reduce T i 6 reduce T 7 s5 err s7 err err err s3 err s9 err 9 reduce T () 53 7 (() 7(() 0() i) shift 5
54 54 i + ( ) T 0 s5 err s7 err err err s3 err err s2 2 acc 3 s5 err s7 err err 4 4 reduce +T 5 reduce T i 6 reduce T 7 s5 err s7 err err err s3 err s9 err 9 reduce T () 5 (i) 7 (() 7(() 0() reduce T i )
55 55 i + ( ) T 0 s5 err s7 err err err s3 err err s2 2 acc 3 s5 err s7 err err 4 4 reduce +T 5 reduce T i 6 reduce T 7 s5 err s7 err err err s3 err s9 err 9 reduce T () 6 (T) 7 (() 7(() 0() reduce T )
56 56 i + ( ) T 0 s5 err s7 err err err s3 err err s2 2 acc 3 s5 err s7 err err 4 4 reduce +T 5 reduce T i 6 reduce T 7 s5 err s7 err err err s3 err s9 err 9 reduce T () 8 () 7 (() 7(() 0() shift 9 )
57 57 i + ( ) T 0 s5 err s7 err err err s3 err err s2 2 acc 3 s5 err s7 err err 4 4 reduce +T 5 reduce T i 6 reduce T 7 s5 err s7 err err err s3 err s9 err 9 reduce T () 9 ()) 8 () 7 (() 7(() 0() reduce T ( )
58 i + ( ) T 0 s5 err s7 err err err s3 err err s2 2 acc 3 s5 err s7 err err 4 4 reduce +T 5 reduce T i 6 reduce T 7 s5 err s7 err err err s3 err s9 err 9 reduce T () 58 6 (T) 7(() 0() reduce T
59 i + ( ) T 0 s5 err s7 err err err s3 err err s2 2 acc 3 s5 err s7 err err 4 4 reduce +T 5 reduce T i 6 reduce T 7 s5 err s7 err err err s3 err s9 err 9 reduce T () 59 8 () 7(() 0() err
60 : T 11: T i i T i i ( 0 7 1: S 4: T 6: + T 10: T i 12: T () ( 13: T ( ) 4: T 6: + T 10: T i 12: T () 2 3 2: S 7: + T 2: S 7: + T 10: T i 12: T () T 15: T () ) 14: T ( ) 7: + T 4 8: + T 9 60
61 Constructing LR(0) parsing table Add a production S S Construct a deterministic finite automaton accepting valid 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 61
62 Filling Parsing Table 62 A state s i reduce A A s i Shift on t 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)
63 xample Control Table 63 i + ( ) T 0 s5 err s7 err err err s3 err err s2 2 acc 3 s5 err s7 err err 4 4 reduce +T 5 reduce T i 6 reduce T 7 s5 err s7 err err err s3 err s9 err 9 reduce T ()
64 xample Non LR(0) Grammar LR(0) items i + S + i 1: S 2 4, 8 2: S s3 3: S r S 4: + 5 4, 8 5: + s6 6: + 7 7: + r + 8: i s9 9: i r i 64
65 xample Non LR(0) DFA S + i 0 S + i 2 S i i 1 i S i
66 0 S + i 2 S i i + 1 i 3 S i i + 0 s1 err err 2 1 red i 2 err s4 s3 3 accept 4 s red + s4 red + red +
67 Dangling lse S if cond s else s if cond s assign 67
68 Non-Ambiguous Non LR(0) Grammar S + T T T T * F F F i 0 S + T T T T * F T F F i T 1 2 T T T * F * T T * F F i i + * 0 1?? 2 68
69 Non-Ambiguous SLR(1) Grammar S + T T T T * F F F i 0 S + T T T T * F T F F i T 1 2 T T T * F * T T * F F i i + * 0 1 r T s2 2 69
70 LR(1) Parser LR(1) Items A, t is at the top of the and we are expecting t LR(1) State Sets of items LALR(1) State Merge items with the same look-ahead 70
71 Grammar Hierarchy Non-ambiguous CFG CLR(1) LALR(1) SLR(1) LL(1) LR(0) 71
72 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 := xp 72
73 A motivating example Create a desk calculator Challenges Non trivial syntax Recursive expressions (semantics) Operator precedence 73
74 Solution (lexical analysis) import java_cup.runtime.*; %% %cup %eofval{ return sym.of; %eofval} NUMBR=[0-9]+ %% + { return new Symbol(sym.PLUS); } - { return new Symbol(sym.MINUS); } * { return new Symbol(sym.MULT); } / { return new Symbol(sym.DIV); } ( { return new Symbol(sym.LPARN); } ) { return new Symbol(sym.RPARN); } {NUMBR} { return new Symbol(sym.NUMBR, new Integer(yytext())); } \n { }. { } 74 Parser gets terminals from the Lexer
75 75 terminal Integer NUMBR; terminal PLUS,MINUS,MULT,DIV; terminal LPARN, RPARN; terminal UMINUS; nonterminal Integer expr; precedence left PLUS, MINUS; precedence left DIV, MULT; Precedence left UMINUS; %% expr ::= expr:e1 PLUS expr:e2 {: RSULT = new Integer(e1.intValue() + e2.intvalue()); :} expr:e1 MINUS expr:e2 {: RSULT = new Integer(e1.intValue() - e2.intvalue()); :} expr:e1 MULT expr:e2 {: RSULT = new Integer(e1.intValue() * e2.intvalue()); :} expr:e1 DIV expr:e2 {: RSULT = new Integer(e1.intValue() / e2.intvalue()); :} MINUS expr:e1 %prec UMINUS {: RSULT = new Integer(0 - e1.intvalue(); :} LPARN expr:e1 RPARN {: RSULT = e1; :} NUMBR:n {: RSULT = n; :}
76 Summary 76 LR is a powerful technique Generates efficient parsers Generation tools exit LALR(1) Bison, yacc, CUP But some grammars need to be tuned Shift/Reduce conflicts Reduce/Reduce conflicts fficiency of the generated parser There exist more general methods GLR Arbitrary grammars in n 3 arly parsers CYK algorithms
Administrivia. Test I during class on 10 March. Bottom-Up Parsing. Lecture An Introductory Example
Administrivia Test I during class on 10 March. Bottom-Up Parsing Lecture 11-12 From slides by G. Necula & R. Bodik) 2/20/08 Prof. Hilfinger CS14 Lecture 11 1 2/20/08 Prof. Hilfinger CS14 Lecture 11 2 Bottom-Up
More informationTHEORY OF COMPILATION
Lecture 04 Syntax analysis: top-down and bottom-up parsing THEORY OF COMPILATION EranYahav 1 You are here Compiler txt Source Lexical Analysis Syntax Analysis Parsing Semantic Analysis Inter. Rep. (IR)
More informationLR2: LR(0) Parsing. LR Parsing. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class
LR2: LR(0) Parsing LR Parsing CMPT 379: Compilers Instructor: Anoop Sarkar anoopsarkar.github.io/compilers-class Parsing - Roadmap Parser: decision procedure: builds a parse tree Top-down vs. bottom-up
More informationCMSC 330: Organization of Programming Languages. Pushdown Automata Parsing
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing Chomsky Hierarchy Categorization of various languages and grammars Each is strictly more restrictive than the previous First described
More informationShift-Reduce parser E + (E + (E) E [a-z] In each stage, we shift a symbol from the input to the stack, or reduce according to one of the rules.
Bottom-up Parsing Bottom-up Parsing Until now we started with the starting nonterminal S and tried to derive the input from it. In a way, this isn t the natural thing to do. It s much more logical to start
More informationCA Compiler Construction
CA4003 - Compiler Construction Bottom Up Parsing David Sinclair Bottom Up Parsing LL(1) parsers have enjoyed a bit of a revival thanks to JavaCC. LL(k) parsers must predict which production rule to use
More informationParsing -3. A View During TD Parsing
Parsing -3 Deterministic table-driven parsing techniques Pictorial view of TD and BU parsing BU (shift-reduce) Parsing Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1) Problems with
More informationCS 406: Bottom-Up Parsing
CS 406: Bottom-Up Parsing Stefan D. Bruda Winter 2016 BOTTOM-UP PUSH-DOWN AUTOMATA A different way to construct a push-down automaton equivalent to a given grammar = shift-reduce parser: Given G = (N,
More informationCompiler Construction Lent Term 2015 Lectures (of 16)
Compiler Construction Lent Term 2015 Lectures 13 --- 16 (of 16) 1. Return to lexical analysis : application of Theory of Regular Languages and Finite Automata 2. Generating Recursive descent parsers 3.
More informationLecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS Berkley University 1
Lecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS 164 -- Berkley University 1 Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient
More informationCSC 4181Compiler Construction. Context-Free Grammars Using grammars in parsers. Parsing Process. Context-Free Grammar
CSC 4181Compiler Construction Context-ree Grammars Using grammars in parsers CG 1 Parsing Process Call the scanner to get tokens Build a parse tree from the stream of tokens A parse tree shows the syntactic
More informationCompiler Construction Lent Term 2015 Lectures (of 16)
Compiler Construction Lent Term 2015 Lectures 13 --- 16 (of 16) 1. Return to lexical analysis : application of Theory of Regular Languages and Finite Automata 2. Generating Recursive descent parsers 3.
More informationCompiler Design Spring 2017
Compiler Design Spring 2017 3.4 Bottom-up parsing Dr. Zoltán Majó Compiler Group Java HotSpot Virtual Machine Oracle Corporation 1 Bottom up parsing Goal: Obtain rightmost derivation in reverse w S Reduce
More informationSyntax Analysis Part I. Position of a Parser in the Compiler Model. The Parser. Chapter 4
1 Syntax Analysis Part I Chapter 4 COP5621 Compiler Construction Copyright Robert van ngelen, Flora State University, 2007 Position of a Parser in the Compiler Model 2 Source Program Lexical Analyzer Lexical
More informationCompiler Construction Lectures 13 16
Compiler Construction Lectures 13 16 Lent Term, 2013 Lecturer: Timothy G. Griffin 1 Generating Lexical Analyzers Source Program Lexical Analyzer tokens Parser Lexical specification Scanner Generator LEX
More informationSyntax Analysis Part I
1 Syntax Analysis Part I Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2013 2 Position of a Parser in the Compiler Model Source Program Lexical Analyzer
More informationSyntax Analysis Part I
1 Syntax Analysis Part I Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2013 2 Position of a Parser in the Compiler Model Source Program Lexical Analyzer
More informationBottom-Up Syntax Analysis
Bottom-Up Syntax Analysis Wilhelm/Maurer: Compiler Design, Chapter 8 Reinhard Wilhelm Universität des Saarlandes wilhelm@cs.uni-sb.de and Mooly Sagiv Tel Aviv University sagiv@math.tau.ac.il Subjects Functionality
More informationProf. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan
Language Processing Systems Prof. Mohamed Hamada Software Engineering La. The University of izu Japan Syntax nalysis (Parsing) 1. Uses Regular Expressions to define tokens 2. Uses Finite utomata to recognize
More informationCompiler Design 1. LR Parsing. Goutam Biswas. Lect 7
Compiler Design 1 LR Parsing Compiler Design 2 LR(0) Parsing An LR(0) parser can take shift-reduce decisions entirely on the basis of the states of LR(0) automaton a of the grammar. Consider the following
More informationSyntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis
Context-free grammars Derivations Parse Trees Left-recursive grammars Top-down parsing non-recursive predictive parsers construction of parse tables Bottom-up parsing shift/reduce parsers LR parsers GLR
More informationLecture 11 Sections 4.5, 4.7. Wed, Feb 18, 2009
The s s The s Lecture 11 Sections 4.5, 4.7 Hampden-Sydney College Wed, Feb 18, 2009 Outline The s s 1 s 2 3 4 5 6 The LR(0) Parsing s The s s There are two tables that we will construct. The action table
More informationIntroduction to Bottom-Up Parsing
Introduction to Bottom-Up Parsing Outline Review LL parsing Shift-reduce parsing The LR parsing algorithm Constructing LR parsing tables Compiler Design 1 (2011) 2 Top-Down Parsing: Review Top-down parsing
More informationBottom-Up Parsing. Ÿ rm E + F *idÿ rm E +id*idÿ rm T +id*id. Ÿ rm F +id*id Ÿ rm id + id * id
Bottom-Up Parsing Attempts to traverse a parse tree bottom up (post-order traversal) Reduces a sequence of tokens to the start symbol At each reduction step, the RHS of a production is replaced with LHS
More informationEXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully
EXAM Please read all instructions, including these, carefully There are 7 questions on the exam, with multiple parts. You have 3 hours to work on the exam. The exam is open book, open notes. Please write
More informationIntroduction to Bottom-Up Parsing
Outline Introduction to Bottom-Up Parsing Review LL parsing Shift-reduce parsing he LR parsing algorithm Constructing LR parsing tables Compiler Design 1 (2011) 2 op-down Parsing: Review op-down parsing
More informationBottom-Up Syntax Analysis
Bottom-Up Syntax Analysis Wilhelm/Seidl/Hack: Compiler Design Syntactic and Semantic Analysis, Chapter 3 Reinhard Wilhelm Universität des Saarlandes wilhelm@cs.uni-saarland.de and Mooly Sagiv Tel Aviv
More informationIntroduction to Bottom-Up Parsing
Introduction to Bottom-Up Parsing Outline Review LL parsing Shift-reduce parsing The LR parsing algorithm Constructing LR parsing tables 2 Top-Down Parsing: Review Top-down parsing expands a parse tree
More informationBottom-Up Syntax Analysis
Bottom-Up Syntax Analysis Wilhelm/Seidl/Hack: Compiler Design Syntactic and Semantic Analysis Reinhard Wilhelm Universität des Saarlandes wilhelm@cs.uni-saarland.de and Mooly Sagiv Tel Aviv University
More informationIntroduction to Bottom-Up Parsing
Outline Introduction to Bottom-Up Parsing Review LL parsing Shift-reduce parsing he LR parsing algorithm Constructing LR parsing tables 2 op-down Parsing: Review op-down parsing expands a parse tree from
More informationFoundations of Informatics: a Bridging Course
Foundations of Informatics: a Bridging Course Week 3: Formal Languages and Semantics Thomas Noll Lehrstuhl für Informatik 2 RWTH Aachen University noll@cs.rwth-aachen.de http://www.b-it-center.de/wob/en/view/class211_id948.html
More informationI 1 : {S S } I 2 : {S X ay, Y X } I 3 : {S Y } I 4 : {X b Y, Y X, X by, X c} I 5 : {X c } I 6 : {S Xa Y, Y X, X by, X c} I 7 : {X by } I 8 : {Y X }
Let s try building an SLR parsing table for another simple grammar: S XaY Y X by c Y X S XaY Y X by c Y X Canonical collection: I 0 : {S S, S XaY, S Y, X by, X c, Y X} I 1 : {S S } I 2 : {S X ay, Y X }
More informationComputer Science 160 Translation of Programming Languages
Computer Science 160 Translation of Programming Languages Instructor: Christopher Kruegel Building a Handle Recognizing Machine: [now, with a look-ahead token, which is LR(1) ] LR(k) items An LR(k) item
More informationCFLs and Regular Languages. CFLs and Regular Languages. CFLs and Regular Languages. Will show that all Regular Languages are CFLs. Union.
We can show that every RL is also a CFL Since a regular grammar is certainly context free. We can also show by only using Regular Expressions and Context Free Grammars That is what we will do in this half.
More informationSyntax Analysis (Part 2)
Syntax Analysis (Part 2) Martin Sulzmann Martin Sulzmann Syntax Analysis (Part 2) 1 / 42 Bottom-Up Parsing Idea Build right-most derivation. Scan input and seek for matching right hand sides. Terminology
More informationINF5110 Compiler Construction
INF5110 Compiler Construction Parsing Spring 2016 1 / 131 Outline 1. Parsing Bottom-up parsing Bibs 2 / 131 Outline 1. Parsing Bottom-up parsing Bibs 3 / 131 Bottom-up parsing: intro LR(0) SLR(1) LALR(1)
More informationChapter 4: Bottom-up Analysis 106 / 338
Syntactic Analysis Chapter 4: Bottom-up Analysis 106 / 338 Bottom-up Analysis Attention: Many grammars are not LL(k)! A reason for that is: Definition Grammar G is called left-recursive, if A + A β for
More informationSyntax Directed Transla1on
Syntax Directed Transla1on Syntax Directed Transla1on CMPT 379: Compilers Instructor: Anoop Sarkar anoopsarkar.github.io/compilers-class Syntax directed Translation Models for translation from parse trees
More informationCompiling Techniques
Lecture 6: 9 October 2015 Announcement New tutorial session: Friday 2pm check ct course webpage to find your allocated group Table of contents 1 2 Ambiguity s Bottom-Up Parser A bottom-up parser builds
More informationBottom up parsing. General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing);
Bottom up parsing General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing); 1 Top-down vs Bottom-up Bottom-up more powerful than top-down; Can process
More informationEXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully
EXAM Please read all instructions, including these, carefully There are 7 questions on the exam, with multiple parts. You have 3 hours to work on the exam. The exam is open book, open notes. Please write
More informationAnnouncements. H6 posted 2 days ago (due on Tue) Midterm went well: Very nice curve. Average 65% High score 74/75
Announcements H6 posted 2 days ago (due on Tue) Mterm went well: Average 65% High score 74/75 Very nice curve 80 70 60 50 40 30 20 10 0 1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96 101 106
More informationn Top-down parsing vs. bottom-up parsing n Top-down parsing n Introduction n A top-down depth-first parser (with backtracking)
Announcements n Quiz 1 n Hold on to paper, bring over at the end n HW1 due today n HW2 will be posted tonight n Due Tue, Sep 18 at 2pm in Submitty! n Team assignment. Form teams in Submitty! n Top-down
More informationTop-Down Parsing and Intro to Bottom-Up Parsing
Predictive Parsers op-down Parsing and Intro to Bottom-Up Parsing Lecture 7 Like recursive-descent but parser can predict which production to use By looking at the next few tokens No backtracking Predictive
More informationComputing if a token can follow
Computing if a token can follow first(b 1... B p ) = {a B 1...B p... aw } follow(x) = {a S......Xa... } There exists a derivation from the start symbol that produces a sequence of terminals and nonterminals
More informationCompiler Construction
Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-16/cc/ Recap: LR(0) Grammars LR(0) Grammars The case k = 0 is
More informationThis lecture covers Chapter 5 of HMU: Context-free Grammars
This lecture covers Chapter 5 of HMU: Context-free rammars (Context-free) rammars (Leftmost and Rightmost) Derivations Parse Trees An quivalence between Derivations and Parse Trees Ambiguity in rammars
More information1. For the following sub-problems, consider the following context-free grammar: S AA$ (1) A xa (2) A B (3) B yb (4)
ECE 468 & 573 Problem Set 2: Contet-free Grammars, Parsers 1. For the following sub-problems, consider the following contet-free grammar: S $ (1) (2) (3) (4) λ (5) (a) What are the terminals and non-terminals
More informationSYLLABUS. Introduction to Finite Automata, Central Concepts of Automata Theory. CHAPTER - 3 : REGULAR EXPRESSIONS AND LANGUAGES
Contents i SYLLABUS UNIT - I CHAPTER - 1 : AUT UTOMA OMATA Introduction to Finite Automata, Central Concepts of Automata Theory. CHAPTER - 2 : FINITE AUT UTOMA OMATA An Informal Picture of Finite Automata,
More informationINF5110 Compiler Construction
INF5110 Compiler Construction Parsing Spring 2016 1 / 84 Overview First and Follow set: general concepts for grammars textbook looks at one parsing technique (top-down) [Louden, 1997, Chap. 4] before studying
More information60-354, Theory of Computation Fall Asish Mukhopadhyay School of Computer Science University of Windsor
60-354, Theory of Computation Fall 2013 Asish Mukhopadhyay School of Computer Science University of Windsor Pushdown Automata (PDA) PDA = ε-nfa + stack Acceptance ε-nfa enters a final state or Stack is
More informationLecture 11 Context-Free Languages
Lecture 11 Context-Free Languages COT 4420 Theory of Computation Chapter 5 Context-Free Languages n { a b : n n { ww } 0} R Regular Languages a *b* ( a + b) * Example 1 G = ({S}, {a, b}, S, P) Derivations:
More informationCompiling Techniques
Lecture 3: Introduction to 22 September 2017 Reminder Action Create an account and subscribe to the course on piazza. Coursework Starts this afternoon (14.10-16.00) Coursework description is updated regularly;
More informationParsing VI LR(1) Parsers
Parsing VI LR(1) Parsers N.B.: This lecture uses a left-recursive version of the SheepNoise grammar. The book uses a rightrecursive version. The derivations (& the tables) are different. Copyright 2005,
More informationParsing Algorithms. CS 4447/CS Stephen Watt University of Western Ontario
Parsing Algorithms CS 4447/CS 9545 -- Stephen Watt University of Western Ontario The Big Picture Develop parsers based on grammars Figure out properties of the grammars Make tables that drive parsing engines
More informationCS20a: summary (Oct 24, 2002)
CS20a: summary (Oct 24, 2002) Context-free languages Grammars G = (V, T, P, S) Pushdown automata N-PDA = CFG D-PDA < CFG Today What languages are context-free? Pumping lemma (similar to pumping lemma for
More informationCreating a Recursive Descent Parse Table
Creating a Recursive Descent Parse Table Recursive descent parsing is sometimes called LL parsing (Left to right examination of input, Left derivation) Consider the following grammar E TE' E' +TE' T FT'
More informationAmbiguity, Precedence, Associativity & Top-Down Parsing. Lecture 9-10
Ambiguity, Precedence, Associativity & Top-Down Parsing Lecture 9-10 (From slides by G. Necula & R. Bodik) 2/13/2008 Prof. Hilfinger CS164 Lecture 9 1 Administrivia Team assignments this evening for all
More informationCONTEXT FREE GRAMMAR AND
CONTEXT FREE GRAMMAR AND PARSING STATIC ANALYSIS - PARSING Source language Scanner (lexical analysis) tokens Parser (syntax analysis) Syntatic structure Semantic Analysis (IC generator) Syntatic/sema ntic
More informationEXAM. Please read all instructions, including these, carefully NAME : Problem Max points Points 1 10 TOTAL 100
EXAM Please read all instructions, including these, carefully There are 7 questions on the exam, with multiple parts. You have 3 hours to work on the exam. The exam is open book, open notes. Please write
More informationSyntax Analysis - Part 1. Syntax Analysis
Syntax Analysis Outline Context-Free Grammars (CFGs) Parsing Top-Down Recursive Descent Table-Driven Bottom-Up LR Parsing Algorithm How to Build LR Tables Parser Generators Grammar Issues for Programming
More informationΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 7a Syntax Analysis Elias Athanasopoulos
ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών Lecture 7a Syntax Analysis Elias Athanasopoulos eliasathan@cs.ucy.ac.cy Operator-precedence Parsing A class of shiq-reduce parsers that can be writen by hand
More informationHarvard CS 121 and CSCI E-207 Lecture 10: Ambiguity, Pushdown Automata
Harvard CS 121 and CSCI E-207 Lecture 10: Ambiguity, Pushdown Automata Salil Vadhan October 4, 2012 Reading: Sipser, 2.2. Another example of a CFG (with proof) L = {x {a, b} : x has the same # of a s and
More informationWhy augment the grammar?
Why augment the grammar? Consider -> F + F FOLLOW F -> i ---------------- 0:->.F+ ->.F F->.i i F 2:F->i. Action Goto + i $ F ----------------------- 0 s2 1 1 s3? 2 r3 r3 3 s2 4 1 4? 1:->F.+ ->F. i 4:->F+.
More informationBottom-up syntactic parsing. LR(k) grammars. LR(0) grammars. Bottom-up parser. Definition Properties
Course 9-10 1 Bottom-up syntactic parsing Bottom-up parser LR(k) grammars Definition Properties LR(0) grammars Characterization theorem for LR(0) LR(0) automaton 2 a 1... a i... a n # X 1 X 1 Control Parsing
More informationTheory of Computation (IV) Yijia Chen Fudan University
Theory of Computation (IV) Yijia Chen Fudan University Review language regular context-free machine DFA/ NFA PDA syntax regular expression context-free grammar Pushdown automata Definition A pushdown automaton
More informationCS Pushdown Automata
Chap. 6 Pushdown Automata 6.1 Definition of Pushdown Automata Example 6.2 L ww R = {ww R w (0+1) * } Palindromes over {0, 1}. A cfg P 0 1 0P0 1P1. Consider a FA with a stack(= a Pushdown automaton; PDA).
More informationPushdown Automata (2015/11/23)
Chapter 6 Pushdown Automata (2015/11/23) Sagrada Familia, Barcelona, Spain Outline 6.0 Introduction 6.1 Definition of PDA 6.2 The Language of a PDA 6.3 Euivalence of PDA s and CFG s 6.4 Deterministic PDA
More informationContext free languages
Context free languages Syntatic parsers and parse trees E! E! *! E! (! E! )! E + E! id! id! id! 2 Context Free Grammars The CF grammar production rules have the following structure X α being X N and α
More informationSyntactic Analysis. Top-Down Parsing
Syntactic Analysis Top-Down Parsing Copyright 2015, Pedro C. Diniz, all rights reserved. Students enrolled in Compilers class at University of Southern California (USC) have explicit permission to make
More information1. For the following sub-problems, consider the following context-free grammar: S A$ (1) A xbc (2) A CB (3) B yb (4) C x (6)
ECE 468 & 573 Problem Set 2: Contet-free Grammars, Parsers (Solutions) 1. For the following sub-problems, consider the following contet-free grammar: S A$ (1) A C (2) A C (3) y (4) λ (5) C (6) (a) What
More informationTranslator Design Lecture 16 Constructing SLR Parsing Tables
SLR Simple LR An LR(0) item (item for short) of a grammar G is a production of G with a dot at some position of the right se. Thus, production A XYZ yields the four items AA XXXXXX AA XX YYYY AA XXXX ZZ
More informationPushdown Automata: Introduction (2)
Pushdown Automata: Introduction Pushdown automaton (PDA) M = (K, Σ, Γ,, s, A) where K is a set of states Σ is an input alphabet Γ is a set of stack symbols s K is the start state A K is a set of accepting
More informationBriefly on Bottom-up
Briefly on Bottom-up Paola Quaglia University of Trento November 11, 2017 Abstract These short notes provide descriptions and references relative to the construction of parsing tables for SLR(1), for LR(1),
More informationLanguages. Languages. An Example Grammar. Grammars. Suppose we have an alphabet V. Then we can write:
Languages A language is a set (usually infinite) of strings, also known as sentences Each string consists of a sequence of symbols taken from some alphabet An alphabet, V, is a finite set of symbols, e.g.
More informationCompiling Techniques
Lecture 7: Abstract Syntax 13 October 2015 Table of contents Syntax Tree 1 Syntax Tree Semantic Actions Examples Abstract Grammar 2 Internal Representation AST Builder 3 Visitor Processing Semantic Actions
More information5 Context-Free Languages
CA320: COMPUTABILITY AND COMPLEXITY 1 5 Context-Free Languages 5.1 Context-Free Grammars Context-Free Grammars Context-free languages are specified with a context-free grammar (CFG). Formally, a CFG G
More informationCISC4090: Theory of Computation
CISC4090: Theory of Computation Chapter 2 Context-Free Languages Courtesy of Prof. Arthur G. Werschulz Fordham University Department of Computer and Information Sciences Spring, 2014 Overview In Chapter
More information1. For the following sub-problems, consider the following context-free grammar: S AB$ (1) A xax (2) A B (3) B yby (5) B A (6)
ECE 468 & 573 Problem Set 2: Contet-free Grammars, Parsers 1. For the following sub-problems, consider the following contet-free grammar: S AB$ (1) A A (2) A B (3) A λ (4) B B (5) B A (6) B λ (7) (a) What
More informationSyntax Analysis, VII The Canonical LR(1) Table Construction. Comp 412
COMP 412 FALL 2018 Syntax Analysis, VII The Canonical LR(1) Table Construction Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights
More informationIntroduction and Motivation. Introduction and Motivation. Introduction to Computability. Introduction and Motivation. Theory. Lecture5: Context Free
ntroduction to Computability Theory Lecture5: Context Free Languages Prof. Amos sraeli 1 ntroduction and Motivation n our study of RL-s we Covered: 1. Motivation and definition of regular languages. 2.
More informationCSE302: Compiler Design
CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University February 27, 2007 Outline Recap
More informationCompiler Principles, PS4
Top-Down Parsing Compiler Principles, PS4 Parsing problem definition: The general parsing problem is - given set of rules and input stream (in our case scheme token input stream), how to find the parse
More informationContext-Free Grammars (and Languages) Lecture 7
Context-Free Grammars (and Languages) Lecture 7 1 Today Beyond regular expressions: Context-Free Grammars (CFGs) What is a CFG? What is the language associated with a CFG? Creating CFGs. Reasoning about
More informationTAFL 1 (ECS-403) Unit- III. 3.1 Definition of CFG (Context Free Grammar) and problems. 3.2 Derivation. 3.3 Ambiguity in Grammar
TAFL 1 (ECS-403) Unit- III 3.1 Definition of CFG (Context Free Grammar) and problems 3.2 Derivation 3.3 Ambiguity in Grammar 3.3.1 Inherent Ambiguity 3.3.2 Ambiguous to Unambiguous CFG 3.4 Simplification
More informationEinführung in die Computerlinguistik
Einführung in die Computerlinguistik Context-Free Grammars (CFG) Laura Kallmeyer Heinrich-Heine-Universität Düsseldorf Summer 2016 1 / 22 CFG (1) Example: Grammar G telescope : Productions: S NP VP NP
More informationType 3 languages. Type 2 languages. Regular grammars Finite automata. Regular expressions. Type 2 grammars. Deterministic Nondeterministic.
Course 7 1 Type 3 languages Regular grammars Finite automata Deterministic Nondeterministic Regular expressions a, a, ε, E 1.E 2, E 1 E 2, E 1*, (E 1 ) Type 2 languages Type 2 grammars 2 Brief history
More informationParsing. Context-Free Grammars (CFG) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 26
Parsing Context-Free Grammars (CFG) Laura Kallmeyer Heinrich-Heine-Universität Düsseldorf Winter 2017/18 1 / 26 Table of contents 1 Context-Free Grammars 2 Simplifying CFGs Removing useless symbols Eliminating
More informationCompiling Techniques
Lecture 5: Top-Down Parsing 6 October 2015 The Parser Context-Free Grammar (CFG) Lexer Source code Scanner char Tokeniser token Parser AST Semantic Analyser AST IR Generator IR Errors Checks the stream
More informationCS5371 Theory of Computation. Lecture 7: Automata Theory V (CFG, CFL, CNF)
CS5371 Theory of Computation Lecture 7: Automata Theory V (CFG, CFL, CNF) Announcement Homework 2 will be given soon (before Tue) Due date: Oct 31 (Tue), before class Midterm: Nov 3, (Fri), first hour
More informationLR(1) Parsers Part III Last Parsing Lecture. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.
LR(1) Parsers Part III Last Parsing Lecture Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. LR(1) Parsers A table-driven LR(1) parser looks like source code Scanner Table-driven Parser
More informationContext-free Grammars and Languages
Context-free Grammars and Languages COMP 455 002, Spring 2019 Jim Anderson (modified by Nathan Otterness) 1 Context-free Grammars Context-free grammars provide another way to specify languages. Example:
More informationIntroduction to Theory of Computing
CSCI 2670, Fall 2012 Introduction to Theory of Computing Department of Computer Science University of Georgia Athens, GA 30602 Instructor: Liming Cai www.cs.uga.edu/ cai 0 Lecture Note 3 Context-Free Languages
More informationCPS 220 Theory of Computation Pushdown Automata (PDA)
CPS 220 Theory of Computation Pushdown Automata (PDA) Nondeterministic Finite Automaton with some extra memory Memory is called the stack, accessed in a very restricted way: in a First-In First-Out fashion
More informationCompiling Techniques
Lecture 5: Top-Down Parsing 26 September 2017 The Parser Context-Free Grammar (CFG) Lexer Source code Scanner char Tokeniser token Parser AST Semantic Analyser AST IR Generator IR Errors Checks the stream
More informationGrammars and Context Free Languages
Grammars and Context Free Languages H. Geuvers and A. Kissinger Institute for Computing and Information Sciences Version: fall 2015 H. Geuvers & A. Kissinger Version: fall 2015 Talen en Automaten 1 / 23
More informationPushdown Automata (Pre Lecture)
Pushdown Automata (Pre Lecture) Dr. Neil T. Dantam CSCI-561, Colorado School of Mines Fall 2017 Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 1 / 41 Outline Pushdown Automata Pushdown
More informationSyntax Analysis Part III
Syntax Analysis Part III Chapter 4: Top-Down Parsing Slides adapted from : Robert van Engelen, Florida State University Eliminating Ambiguity stmt if expr then stmt if expr then stmt else stmt other The
More informationCPS 220 Theory of Computation
CPS 22 Theory of Computation Review - Regular Languages RL - a simple class of languages that can be represented in two ways: 1 Machine description: Finite Automata are machines with a finite number of
More informationSuppose h maps number and variables to ɛ, and opening parenthesis to 0 and closing parenthesis
1 Introduction Parenthesis Matching Problem Describe the set of arithmetic expressions with correctly matched parenthesis. Arithmetic expressions with correctly matched parenthesis cannot be described
More information