Syntax Analysis Part III
|
|
- Mariah Osborne
- 6 years ago
- Views:
Transcription
1 Syntax Analysis Part III Chapter 4: Top-Down Parsing Slides adapted from : Robert van Engelen, Florida State University
2 Eliminating Ambiguity stmt if expr then stmt if expr then stmt else stmt other The above grammar is ambiguous since the following string has two parse trees if E 1 then if E 2 then S 1 else S 2
3 Eliminating Ambiguity stmt if exp r then stmt if expr then stmt else stmt stmt if expr then stmt if expr then stmt else stmt
4 Eliminating Ambiguity stmt matched_stmt open_stmt matched_stmt if expr then matched_stmt else matched_stmt other open_stmt if expr then stmt if expr then matched_stmt else open_stmt In practice, disambiguation is rarely implemented into the grammar; extra constraints are added to the parser for solving ambiguity
5 Exercise Choose the unambiguous version of the given ambiguous grammar: S SS a b S Sa Sb ε S S S S a b S S S S a b S Sa Sb S Sa Sb a b
6 Immediate Left/Right Recursion Productions of the form A A α β are immediate left recursive Productions of the form A α A β are immediate right recursive
7 Associativity of Operators Immediate left-recursive productions used to implement left-associative operators left left + term term String a+b+c has the same meaning as (a+b)+c
8 Associativity of Operators Immediate right-recursive productions used to implement right-associative operators right term = right term String a=b=c has the same meaning as a=(b=c)
9 Precedence of Operators Operators with higher precedence bind more tightly expr expr + term term term term * factor factor factor number ( expr ) String 2+3*5 has the same meaning as 2+(3*5) expr expr term term factor number term factor number factor number * 5
10 Immediate Left Recursion When one of the productions in a grammar is immediate left recursive then a top-down predictive parser loops forever on certain inputs S A Recursive call to A w A β
11 Immediate Left Recursion We can eliminate immediate left recursive productions by systematically rewriting the grammar using right recursive productions A A α β A β R R α R ε
12 Immediate Left Recursion A A A α β R A α α R β α R ε
13 Immediate Left-Recursion Elimination Method Rewrite every left-recursive production A A α A δ β γ into a right-recursive production A β A R γ A R A R α A R δ A R ε
14 Exercise Choose the grammar that correctly eliminates immediate left recursion from the given grammar: E E + T T T id ( E )
15 Exercise (cont d) E E + id E + ( E ) id ( E ) E T E E + T E ε T id ( E ) E E + T T E id ( E ) T id ( E ) E id + E E + T T T id ( E )
16 Left Recursion A CFG is left recursive if it allows derivations of the form A + A α This notion is more general than immediate left recursion
17 Recognition of Left Recursion Assume there are no epsilon rules Draw a graph where nodes are nonterminals arcs (A, B) represent the relation A expands into B α for some α In this graph, a cycle indicates left recursion
18 Example A B C a B C A A b C A B C C a A B C
19 Elimination of Left Recursion The basic operation used by the algorithm: expand first symbol in right-hand side with a rule A B α B β A β α
20 Elimination of Left Recursion The basic idea Process nonterminal symbols in some order A 1, A 2,, A n Replace rules of the form A i A j γ having j < i with rules having j > i
21 Example A < B < C A B C A B C
22 General Left Recursion Elimination Method Arrange the nonterminals in some order A 1, A 2,, A n for i = 1,, n { for j = 1,, i-1 { replace each A i A j γ with A i δ 1 γ δ 2 γ δ k γ where A j δ 1 δ 2 δ k } eliminate the immediate left recursion in A i }
23 Example A B C a B C A A b C A B C C a Choose arrangement: A, B, C i = 1: i = 2, j = 1: i = 3, j = 1: i = 3, j = 2: nothing to do B C A A b B C A B C b a b (imm) B C A B R a b B R B R C b B R ε C A B C C a C B C B a B C C a C B C B a B C C a C C A B R C B a b B R C B a B C C a (imm) C a b B R C B C R a B C R a C R C R A B R C B C R C C R ε
24 Left Factoring When a nonterminal has two or more productions whose right-hand sides start with the same grammar symbols, the grammar is not LL(1) and cannot be used for predictive parsing S A Symbol b cannot be used to predict expansion of B w Bβ Bδ b
25 Left Factoring Replace productions with A α β 1 α β 2 α β n γ A α A R γ A R β 1 β 2 β n
26 Top-Down Parsing Recursive-descent parsing Needs backtracking LL methods (Left-to-right, Leftmost derivation) Also called predictive parsing Specialization of recursive-descent where backtracking is not needed (correct production chosen using look-ahead)
27 Top-Down Parsing Both methods build leftmost derivations Grammar: E T + T T ( E ) T - E T id Leftmost derivation: E lm T + T lm id + T lm id + id E E E E T T T T T T + id + id + id
28 Recursive Descent Parsing Every nonterminal has one (recursive) procedure responsible for parsing the nonterminal s syntactic category of input tokens When a nonterminal has multiple productions, each production is implemented in a branch of a selection statement based on input look-ahead information
29 Recursive Descent Parsing void A() { choose an A-production A X 1 X 2 X k ; for ( i = 1 to k ) { if ( X i is a nonterminal ) } call procedure X i (); else if ( X i equals the current input symbol a ) advance the input to the next symbol; else error(); }
30 Recursive Descent Parsing To allow for backtracking previous code must be modified as follows Must try each A-production in some order In case of failure, input pointer should be retracted and next production should be checked at the latest point of choice If all productions have been checked, then we have an input error
31 Example Assume the grammar E E E + id E - E id ( E ) The following C code implements a recursive descent parser
32 Example (cont d) 1 bool term(token tok) { return *next++ == tok; } 2 bool E 1 () { return E (); } 3 bool E 2 () { return E () && term(plus) && term(id); } 4 bool E() { 5 TOKEN *save = next; 6 return (next = save, E1()) (next = save, E2()); } 7 bool E 1 () { return term(minus) && E (); } 8 bool E 2 () { return term(id); } 9 bool E 3 () { return term(open) && E() && term(close); } 10 bool E () { 11 TOKEN *save = next; 12 return (next = save, E 1 ()) 13 (next = save, E 2 ()) 14 (next = save, E 3 ()); }
33 Exercise Choose the derivation that is a valid recursive descent parse for the string id + id in the grammar E E E + E E - E id ( E ) Moves that are followed by backtracking are marked in red
34 Exercise (cont d) 1. E, E, E + E, id + E, id + E, id + id 2. E, E, - E, id, ( E ), E + E, - E + E, id + E, id + E, id + - E, id + id 3. E, E + E, id + E, id + E, id + id 4. E, E, id, E + E, id + E, id + E, id + id
35 Backtracking & Efficiency When failure occurs at higher level, all lower level subparses that were successful are deleted Later on, those subparses are recomputed from scratch Highly inefficient
36 Predictive Parsing Grammar must be non-ambiguous Eliminate left recursion from grammar Left factor the grammar Compute functions FIRST() and FOLLOW() Table-driven variant of descent parsing
37 FIRST() and FOLLOW(): Intuitive Idea Used to predict production A α for nontermial A S FIRST(α) w A FOLLOW(A) a b
38 FIRST() FIRST(α) = the set of terminals that begin all strings derived from α FIRST(a) = {a} if a T FIRST(ε) = {ε} FIRST(A) = A α FIRST(α) for A α P FIRST(X 1 X 2 X k ) : if ε FIRST(X j ) for all j = 1,, i-1 then add FIRST(X i )\{ε} to FIRST(X 1 X 2 X k ) if ε FIRST(X j ) for all j = 1,, k then add ε to FIRST(X 1 X 2 X k )
39 Example Grammar: E T X T ( E ) int Y X + E ε Y * T ε FIRST(+) = {+} FIRST(*) = {*} FIRST( ( ) = {(} FIRST( ) ) = {)} FIRST(int) = {int} FIRST(ε) = {ε}
40 Example FIRST(Y) = FIRST( * T ) FIRST(ε) = FIRST( * ) FIRST(ε) = { * } { ε } = { *, ε } FIRST(X) = FIRST(+ E) FIRST(ε) = FIRST(+) FIRST(ε) = { + } { ε } = { +, ε }
41 Example FIRST(T) = FIRST( ( E ) ) FIRST(int Y) = FIRST( ( ) FIRST(int) = { ( } { int } = { (, int } FIRST(E) = FIRST(T X) = FIRST(T) = { (, int }
42 FOLLOW() FOLLOW(A) = the set of terminals that can immediately follow nonterminal A FOLLOW(A) = for all (B α A β) P do add FIRST(β)\{ε} to FOLLOW(A) for all (B α A β) P and ε FIRST(β) do add FOLLOW(B) to FOLLOW(A) for all (B α A) P do add FOLLOW(B) to FOLLOW(A) if A is the start symbol then add $ to FOLLOW(A)
43 Example Grammar: E T X T ( E ) int Y X + E ε Y * T ε { $, ) } FOLLOW(E) FOLLOW(X) FOLLOW(E) FOLLOW(E) FOLLOW(X) FOLLOW(X) = FOLLOW(E) = { $, ) }
44 Example Grammar: E T X T ( E ) int Y X + E ε Y * T ε FIRST(X) \ {ε} = {+} FOLLOW(T) FOLLOW(E) = { $, ) } FOLLOW(T) FOLLOW(Y) FOLLOW(T) FOLLOW(T) FOLLOW(Y) FOLLOW(Y) = FOLLOW(T) = { +, $, ) }
45 LL(1) Grammar A grammar G is LL(1) if it is not left recursive and for each collection of productions A α 1 α 2 α n for nonterminal A the following holds: 1. FIRST(α i ) FIRST(α j ) = for all i j 2. if α i * ε then 2.a. α j * ε for all j i 2.b. FIRST(α j ) FOLLOW(A) = for all j i
46 Non-LL(1) Examples Grammar S S a a S a S a S a R ε R S ε S a R a R S ε Not LL(1) because: Left recursive FIRST(a S) FIRST(a) For R: S * ε and ε * ε For R: FIRST(S) FOLLOW(R)
47 Using FIRST and FOLLOW to Write a Recursive Descent Parser expr term rest rest + term rest - term rest ε term id procedure rest(); begin if lookahead in FIRST(+ term rest) then match( + ); term(); rest() else if lookahead in FIRST(- term rest) then match( - ); term(); rest() else if lookahead in FOLLOW(rest) then return else error() end; FIRST(+ term rest) = { + } FIRST(- term rest) = { - } FOLLOW(rest) = { $ }
48 Non-Recursive Predictive Parsing: Table-Driven Parsing Given an LL(1) grammar G = (N, T, P, S) construct a table M[A, a] for A N, a T and use a driver program with a stack input a + b $ stack X Y Z $ Predictive parsing program (driver) Parsing table M output
49 Constructing an LL(1) Predictive Parsing Table for each production A α do for each a FIRST(α) do add A α to M[A, a] enddo if ε FIRST(α) then for each b FOLLOW(A) do add A α to M[A, b] enddo endif enddo Mark each undefined entry in M as error
50 Example Table E T E R E R + T E R ε T F T R T R * F T R ε F ( E ) id A α FIRST(α) FOLLOW(A) E T E R ( id $ ) E R + T E R + $ ) E R ε ε $ ) T F T R ( id + $ ) T R * F T R * + $ ) T R ε ε + $ ) F ( E ) ( * + $ ) F id id * + $ ) id + * ( ) $ E E T E R E T E R E R E R + T E R E R ε E R ε T T F T R T F T R T R T R ε T R * F T R T R ε T R ε F F id F ( E )
51 Ambiguous grammar S i E t S S R a S R e S ε E b Error: duplicate table entry LL(1) Grammars are Unambiguous A α FIRST(α) FOLLOW(A) S i E t S S R i e $ S a a e $ S R e S e e $ S R ε ε e $ E b b t a b e i t $ S S a S i E t S S R S R E E b S R ε S R e S S R ε
52 Predictive Parsing Algorithm Input: string w and LL parsing table M for grammar G Output: leftmost derivation if w L(G) and error otherwise Start configuration stack is $S buffer is w$
53 Predictive Parsing Algorithm let a be the first symbol of w; let X be the top stack symbol; while ( X $ ) if ( X = a ) pop the stack and let a be the next symbol of w; else if ( X is a terminal ) error(); else if ( M[X, a] is an error entry ) error(); else if ( M[X, a] = X Y 1 Y 2 Y k ) { output the production X Y 1 Y 2 Y k ; pop the stack; push Y k, Y k-1,, Y 2, Y 1 onto the stack, with Y 1 } let X be the top stack symbol; } on top;
54 Example Stack $E $E R T $E R T R F $E R T R id $E R T R $E R $E R T+ $E R T $E R T R F $E R T R id $E R T R $E R T R F* $E R T R F $E R T R id $E R T R $E R $ Input id+id*id$ id+id*id$ id+id*id$ id+id*id$ +id*id$ +id*id$ +id*id$ id*id$ id*id$ id*id$ *id$ *id$ id$ id$ $ $ $ Production applied E T E R T F T R F id T R ε E R + T E R T F T R F id T R * F T R F id T R ε E R ε
55 Panic Mode Recovery Add synchronizing actions to undefined entries based on FOLLOW Pro: Can be automated Cons: Error messages are needed FOLLOW(E) = { ) $ } FOLLOW(E R ) = { ) $ } FOLLOW(T) = { + ) $ } FOLLOW(T R ) = { + ) $ } FOLLOW(F) = { + * ) $ } id + * ( ) $ E E T E R E T E R synch synch E R E R + T E R E R ε E R ε T T F T R synch T F T R synch synch T R T R ε T R * F T R T R ε T R ε F F id synch synch F ( E ) synch synch synch: the driver pops current nonterminal A and skips input till synch token
56 Phrase-Level Recovery Change input stream by inserting missing tokens For example: id id is changed into id * id Pro: Can be automated Cons: Recovery not always intuitive Can then continue here id + * ( ) $ E E T E R E T E R synch synch E R E R + T E R E R ε E R ε T T F T R synch T F T R synch synch T R insert * T R ε T R * F T R T R ε T R ε F F id synch synch F ( E ) synch synch insert *: driver inserts missing * and retries the production
57 Error Productions E T E R E R + T E R ε T F T R T R * F T R ε F ( E ) id Add error production : T R F T R to ignore missing *, e.g.: id id Pro: Powerful recovery method Cons: Cannot be automated id + * ( ) $ E E T E R E T E R synch synch E R E R + T E R E R ε E R ε T T F T R synch T F T R synch synch T R T R F T R T R ε T R * F T R T R ε T R ε F F id synch synch F ( E ) synch synch
Syntax 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 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 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 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 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 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 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 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 informationExercises. Exercise: Grammar Rewriting
Exercises Text adapted from : Alessandro Artale, Free University of Bolzano Exercise: Grammar Rewriting Consider the following grammar for Boolean expressions: Bexp Bexp or Bterm Bterm Bterm Bterm and
More informationCS415 Compilers Syntax Analysis Top-down Parsing
CS415 Compilers Syntax Analysis Top-down Parsing These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Announcements Midterm on Thursday, March 13
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 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 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 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 informationPredictive parsing as a specific subclass of recursive descent parsing complexity comparisons with general parsing
Plan for Today Recall Predictive Parsing when it works and when it doesn t necessary to remove left-recursion might have to left-factor Error recovery for predictive parsers Predictive parsing as a specific
More informationCS 314 Principles of Programming Languages
CS 314 Principles of Programming Languages Lecture 7: LL(1) Parsing Zheng (Eddy) Zhang Rutgers University February 7, 2018 Class Information Homework 3 will be posted this coming Monday. 2 Review: Top-Down
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 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 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 informationSyntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 3. Y.N. Srikant
Syntax Analysis: Context-free Grammars, Pushdown Automata and Part - 3 Department of Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler
More informationComputer Science 160 Translation of Programming Languages
Computer Science 160 Translation of Programming Languages Instructor: Christopher Kruegel Top-Down Parsing Top-down Parsing Algorithm Construct the root node of the parse tree, label it with the start
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 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 informationLL(1) Grammar and parser
Crafting a Compiler with C (XI) 資科系 林偉川 LL(1) Grammar and parser LL(1) grammar is the class of CFG and is suitable for RDP. Define LL(1) parsers which use an LL(1) parse table rather than recursive procedures
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 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 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 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 informationFollow sets. LL(1) Parsing Table
Follow sets. LL(1) Parsing Table Exercise Introducing Follow Sets Compute nullable, first for this grammar: stmtlist ::= ε stmt stmtlist stmt ::= assign block assign ::= ID = ID ; block ::= beginof ID
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 informationThe Parser. CISC 5920: Compiler Construction Chapter 3 Syntactic Analysis (I) Grammars (cont d) Grammars
The Parser CISC 5920: Compiler Construction Chapter 3 Syntactic Analysis (I) Arthur. Werschulz Fordham University Department of Computer and Information Sciences Copyright c Arthur. Werschulz, 2017. All
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 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 informationCS153: Compilers Lecture 5: LL Parsing
CS153: Compilers Lecture 5: LL Parsing Stephen Chong https://www.seas.harvard.edu/courses/cs153 Announcements Proj 1 out Due Thursday Sept 20 (2 days away) Proj 2 out Due Thursday Oct 4 (16 days away)
More informationAnnouncement: Midterm Prep
Announcement: Midterm Prep Midterm is Tuesday, 3/13 at 11 in three classrooms - Last name A-C Van Vleck B115 - Last name D-J Van Vleck B135 - Last name K-Z CS1240 List of topics Up to and including lecture
More informationAdministrivia. 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 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 informationCompiler Principles, PS7
Top-Down Parsing Compiler Principles, PS7 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 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 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 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 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 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 informationCompilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs.
Compilerconstructie najaar 2012 http://www.liacs.nl/home/rvvliet/coco/ Rudy van Vliet kamer 124 Snellius, tel. 071-527 5777 rvvliet(at)liacs.nl werkcollege 9, dinsdag 27 november 2012 SLR Parsing / Backpatching
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 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 informationCompila(on* **0368/3133*(Semester*A,*2013/14)*
Compila(on 0368/3133(SemesterA,2013/14) Admin Lecture4:SyntaxAnalysis (Top/DownParsing) ModernCompilerDesign:Chapter2.2 NoamRinetzky Nextweek:Trubowicz101(Lawschool) Mobiles... Slidescredit:RomanManevich,MoolySagiv,JeffUllman,EranYahav
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. 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 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 informationCompiler Design. Spring Syntactic Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz
Compiler Design Spring 2015 Syntactic Analysis Sample Exercises and Solutions Prof. Pedro C. Diniz USC / Information Sciences Institute 4676 Admiralty Way, Suite 1001 Marina del Rey, California 90292 pedro@isi.edu
More informationContext Free Grammars
Automata and Formal Languages Context Free Grammars Sipser pages 101-111 Lecture 11 Tim Sheard 1 Formal Languages 1. Context free languages provide a convenient notation for recursive description of languages.
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 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 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 informationINF5110 Compiler Construction
INF5110 Compiler Construction Spring 2017 1 / 330 Outline 1. Parsing First and follow sets Top-down parsing Bottom-up parsing References 2 / 330 INF5110 Compiler Construction Parsing Spring 2017 3 / 330
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 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 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 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 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 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 informationCompiler construction
Compiler construction Martin Steffen February 20, 2017 Contents 1 Abstract 1 1.1 Parsing.................................................. 1 1.1.1 First and follow sets.....................................
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 informationCourse Script INF 5110: Compiler construction
Course Script INF 5110: Compiler construction INF5110, spring 2018 Martin Steffen ii Contents Contents 4 Parsing 1 4.1 Introduction to parsing........................ 1 4.2 Top-down parsing...........................
More informationTheory of Computation - Module 3
Theory of Computation - Module 3 Syllabus Context Free Grammar Simplification of CFG- Normal forms-chomsky Normal form and Greibach Normal formpumping lemma for Context free languages- Applications of
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 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 informationParsing with Context-Free Grammars
Parsing with Context-Free Grammars Berlin Chen 2005 References: 1. Natural Language Understanding, chapter 3 (3.1~3.4, 3.6) 2. Speech and Language Processing, chapters 9, 10 NLP-Berlin Chen 1 Grammars
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 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 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 informationTop-Down Parsing, Part II
op-down Parsing, Part II Announcements Programming Project 1 due Friday, 11:59PM Office hours every day until then. ubmission instructions will be emailed out tonight. Where We Are ource Code Lexical Analysis
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 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 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 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 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 informationLecture Notes on Inductive Definitions
Lecture Notes on Inductive Definitions 15-312: Foundations of Programming Languages Frank Pfenning Lecture 2 August 28, 2003 These supplementary notes review the notion of an inductive definition and give
More informationCYK Algorithm for Parsing General Context-Free Grammars
CYK Algorithm for Parsing General Context-Free Grammars Why Parse General Grammars Can be difficult or impossible to make grammar unambiguous thus LL(k) and LR(k) methods cannot work, for such ambiguous
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 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 informationLR(1) Parsers Part II. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.
LR(1) Parsers Part II 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 IR grammar Parser
More informationComputational Models - Lecture 4
Computational Models - Lecture 4 Regular languages: The Myhill-Nerode Theorem Context-free Grammars Chomsky Normal Form Pumping Lemma for context free languages Non context-free languages: Examples Push
More informationParsing with CFGs L445 / L545 / B659. Dept. of Linguistics, Indiana University Spring Parsing with CFGs. Direction of processing
L445 / L545 / B659 Dept. of Linguistics, Indiana University Spring 2016 1 / 46 : Overview Input: a string Output: a (single) parse tree A useful step in the process of obtaining meaning We can view the
More informationParsing with CFGs. Direction of processing. Top-down. Bottom-up. Left-corner parsing. Chart parsing CYK. Earley 1 / 46.
: Overview L545 Dept. of Linguistics, Indiana University Spring 2013 Input: a string Output: a (single) parse tree A useful step in the process of obtaining meaning We can view the problem as searching
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 informationTHEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET
THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET Regular Languages and FA A language is a set of strings over a finite alphabet Σ. All languages are finite or countably infinite. The set of all languages
More informationBottom-up syntactic parsing. LR(k) grammars. LR(0) grammars. Bottom-up parser. Definition Properties
Course 8 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 informationTalen en Compilers. Johan Jeuring , period 2. October 29, Department of Information and Computing Sciences Utrecht University
Talen en Compilers 2015-2016, period 2 Johan Jeuring Department of Information and Computing Sciences Utrecht University October 29, 2015 12. LL parsing 12-1 This lecture LL parsing Motivation Stack-based
More informationBottom-up syntactic parsing. LR(k) grammars. LR(0) grammars. Bottom-up parser. Definition Properties
Course 8 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 informationFundamentele Informatica II
Fundamentele Informatica II Answer to selected exercises 5 John C Martin: Introduction to Languages and the Theory of Computation M.M. Bonsangue (and J. Kleijn) Fall 2011 5.1.a (q 0, ab, Z 0 ) (q 1, b,
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 informationEng. Maha Talaat Page 1
El-Shorouk Academy Acad. Year : 2013 / 2014 Higher Institute for Computer & Term : 2nd Information Technology Year : 4th Computer Science Department Supervisor: Prof. Dr. Ahmed Abbassy Section contents:
More informationLecture Notes on Inductive Definitions
Lecture Notes on Inductive Definitions 15-312: Foundations of Programming Languages Frank Pfenning Lecture 2 September 2, 2004 These supplementary notes review the notion of an inductive definition and
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 informationIntro to Theory of Computation
Intro to Theory of Computation LECTURE 7 Last time: Proving a language is not regular Pushdown automata (PDAs) Today: Context-free grammars (CFG) Equivalence of CFGs and PDAs Sofya Raskhodnikova 1/31/2016
More informationParsing. Left-Corner Parsing. Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 17
Parsing Left-Corner Parsing Laura Kallmeyer Heinrich-Heine-Universität Düsseldorf Winter 2017/18 1 / 17 Table of contents 1 Motivation 2 Algorithm 3 Look-ahead 4 Chart Parsing 2 / 17 Motivation Problems
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 information