Shift-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.
|
|
- Constance Dickerson
- 5 years ago
- Views:
Transcription
1 Bottom-up Parsing
2 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 from the input and try to understand what s written there. Bottom-up parsing does exactly that, we start from the input string and try to reduce it to the starting non-terminal. 2
3 LRk LLk LRk No problem with left recursion Today we ll talk about LR0 One of the more popular ways to parse LR0 gramers is using the Shift-Reduce parser. 3
4 Intuition We gather the input token by token until we find a right-hand side of a rule and then we replace it with the non-terminal on the left side Going over a token and remembering it in the stack is a shift Each shift moves to a state that remembers what we ve seen so far A reduce replaces a string in the stack with the variable that derives it 4
5 Shift-Reduce parser Stack a E E Input a b c b c b c b c action Shift Reduce Shift shift 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 E b c shift E b c reduce E E c shift E E c shift E E c Shift E E c Reduce E E E Shift E E E Reduce E E shift E E Reduce E accept 5
6 6 Parsing idid$ 1 S E $ 2 E T 3 E E T 4 T id 5 T E Stack Input Action 0 id id $? S 0 id action $ goto E T g1 g6 1 acc 2 3 g4 Initialize with state g8 g6 8 s9 9
7 Parsing idid$ 1 S E $ 2 E T 3 E E T 4 T id 5 T E Stack Input Action 0 id id $ S 0 id action $ goto E T g1 g6 1 acc 2 3 g g8 g6 8 s9 9 7
8 Parsing idid$ 1 S E $ 2 E T 3 E E T 4 T id 5 T E Stack Input Action 0 id id $ 0 id 5 id $ S 0 1 id action $ acc goto E T g1 g6 2 3 g g8 g6 8 s9 9 8
9 Parsing idid$ 1 S E $ 2 E T 3 E E T 4 T id 5 T E Stack Input Action 0 id id $ 0 id 5 id $ S 0 1 id action $ acc goto E T g1 g6 2 3 g4 pop id g8 g6 8 s9 9 9
10 Parsing idid$ 1 S E $ 2 E T 3 E E T 4 T id 5 T E Stack Input Action 0 id id $ 0 id 5 id $ S 0 1 id action $ acc goto E T g1 g6 2 3 g4 push T g8 g6 8 s9 9 10
11 Parsing idid$ 1 S E $ 2 E T 3 E E T 4 T id 5 T E Stack Input Action 0 id id $ 0 id 5 id $ 0 T 6 id $ S id action $ acc goto E T g1 g6 3 g g8 g6 8 s9 9 11
12 Parsing idid$ 1 S E $ 2 E T 3 E E T 4 T id 5 T E Stack Input Action 0 id id $ 0 id 5 id $ 0 T 6 id $ 0 E 1 id $ S id action $ acc goto E T g1 g6 g g8 g6 8 s9 9 12
13 Parsing idid$ 1 S E $ 2 E T 3 E E T 4 T id 5 T E Stack Input Action 0 id id $ 0 id 5 id $ 0 T 6 id $ 0 E 1 id $ 0 E 1 3 id $ S id action $ acc goto E T g1 g6 g g8 g6 8 s9 9 13
14 Parsing idid$ 1 S E $ 2 E T 3 E E T 4 T id 5 T E Stack Input Action 0 id id $ 0 id 5 id $ 0 T 6 id $ 0 E 1 id $ 0 E 1 3 id $ 0 E 1 3 id 5 $ S id action $ acc goto E T g1 g6 g4 6 7 g8 g6 8 s9 9 14
15 Parsing idid$ 1 S E $ 2 E T 3 E E T 4 T id 5 T E Stack Input Action 0 id id $ 0 id 5 id $ 0 T 6 id $ 0 E 1 id $ 0 E 1 3 id $ 0 E 1 3 id 5 $ 0 E 1 3 T 4 $ S id action $ acc goto E T g1 g6 g4 7 g8 g6 8 s9 9 15
16 Parsing idid$ 1 S E $ 2 E T 3 E E T 4 T id 5 T E Stack Input Action 0 id id $ 0 id 5 id $ 0 T 6 id $ 0 E 1 id $ 0 E 1 3 id $ 0 E 1 3 id 5 $ 0 E 1 3 T 4 $ 0 E 1 $ acc S id action $ acc goto E T g1 g6 g4 g8 g6 8 s9 9 * notice that we doesn t look at the input when reducing 16
17 Constructing an LR0 parsing table 17
18 LR0 item Already matched To be matched Input N α β Hypothesis about αβ being a possible handle, so far we ve matched α, expecting to see β 18
19 Operations for transition diagram construction Initial = {S S$} For an item set I ClosureI = ClosureI {X µ is in grammar N α Xβ in I} GotoI, X = { N αx β N α Xβ in I} 19
20 Initial example Initial = {S E $} Grammar 1 S E $ 2 E T 3 E E T 4 T id 5 T E 20
21 Closure example Initial = {S E $} Closure{S E $} = S E $ E T E E T T id T E Grammar 1 S E $ 2 E T 3 E E T 4 T id 5 T E 21
22 Goto example Initial = {S E $} Closure{S E $} = S E $ E T E E T T id T E Grammar 1 S E $ 2 E T 3 E E T 4 T id 5 T E Goto{S E $, E E T, T id}, E = {S E $, E E T} 22
23 Constructing the transition diagram 1. Start with state 0 containing item Closure{S E $} 2. Repeat until no new states are discovered For every state p containing item set Ip, and symbol N, compute state q containing item set Iq = ClosuregotoIp, N 23
24 Automaton construction example q 0 T q 6 E T T q7 1 S E $ 2 E T 3 E E T 4 T id 5 T E S E$ E T E E T T i T E i q 5 T i i T E E T E E T T i T E q 2 q 1 S E $ E E T $ E q 3 i E E T T i T E T E E E T E q 8 q 9 S E$ q 4 T T E E E T 24
25 Automaton construction example q 0 S E$ 1 S E $ 2 E T 3 E E T 4 T id 5 T E Initialize 25
26 Automaton construction example q 0 S E$ E T E E T T i T E apply Closure 1 S E $ 2 E T 3 E E T 4 T id 5 T E 26
27 Automaton construction example q 0 S E$ E T E E T T i T E T i q 6 q 5 E T T i T E E T E E T T i T E 1 S E $ 2 E T 3 E E T 4 T id 5 T E q 1 E S E $ E E T 27
28 Automaton construction example q 2 q 0 S E$ E T E E T T i T E q 1 E Z E $ E E T T terminal transition corresponds to shift action in parse table $ i q 6 q 5 q 3 E T non-terminal transition corresponds to goto action in parse table T i i E E T T i T E i T T E E E T E q7 T E E T E E T T i T E q 8 q 9 1 S E $ 2 E T 3 E E T 4 T id 5 T E S E$ q 4 T T E E E T a single reduce item corresponds to reduce action 28
29 Automaton construction example q 0 T q 6 E T T q7 1 S E $ 2 E T 3 E E T 4 T id 5 T E S E$ E T E E T T i T E i q 5 T i i T E E T E E T T i T E q 2 q 1 S E $ E E T $ E q 3 i E E T T i T E T E E E T E q 8 q 9 S E$ q 4 T T E E E T 29
30 Parsing Table S id action $ goto E T 1 S E $ 2 E T 3 E E T 4 T id 5 T E 0 g1 g6 1 acc 2 3 g g8 g6 8 s9 9 30
31 LR0 conflicts 31
32 Conflicts Can construct a diagram for every grammar but some may introduce conflicts shift-reduce conflict: an item set contains at least one shift item and one reduce item reduce-reduce conflict: an item set contains two reduce items 32
33 LR0 conflicts q 0 T S E$ E T E E T T i T E T i[e] E i q 5 T i T i [E] Shift/reduce conflict S E $ E T E E T T i T E T i[e] 33
34 LR0 conflicts q 0 T S E$ E T E E T T i T E T i[e] E i q 5 T i V i reduce/reduce conflict S E $ E T E E T T i V i T E 34
35 Parsing Table 35 goto action S T E $ id g6 g1 0 acc 1 2 g g6 g8 7 s9 8 9 reduce/reduce conflict Shift/reduce conflict
36 LR0 conflicts Any grammar with an -rule cannot be LR0 Inherent shift/reduce conflict A reduce item P α Aβ shift item A can always be predicted from P α Aβ 36
37 SLR SLR0 is a tiny extension to LR0 and with it, it might be able to solve some shift-reduce conflicts. Let s say at some time, we can either use shift or reduce. When do we know for sure that reducing the input is bad? 37
38 Example S A$ A id id[number] Number [0-9] [ followa Input: a[5] This means that if we choose to reduce a to A, we ll leave [ out there, that can t be parsed. 38
39 Parsing Table 39 goto action S T E $ id g6 g1 0 acc 1 2 g4 3 s g6 g8 7 s9 8 9 Shift/reduce conflict 3 G something
40 Parsing Table S id action $ goto E T 3 G something 0 1 acc g1 g6 Assuming followg s10 g4 Shift/reduce conflict g8 g6 8 s9 9 40
41 Parsing Table S id action $ goto E T 3 G something 0 1 acc g1 g6 Assuming followg s10 g4 Shift/reduce conflict, Solved! g8 g6 8 s9 9 * now we do look at the input while reducing 41
Lecture 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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
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 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 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 informationBottom-Up Syntax Analysis
Bottom-Up Syntax Analysis Mooly Sagiv http://www.cs.tau.ac.il/~msagiv/courses/wcc13.html Textbook:Modern Compiler Design Chapter 2.2.5 (modified) 1 Pushdown automata Deterministic fficient Parsers Report
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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 Analysis. Theorem: Proof: Let a grammar G be reduced and left-recursive, then G is not LL(k) for any k.
Bottom-up Analysis Theorem: Let a grammar G be reduced and left-recursive, then G is not LL(k) for any k. Proof: Let A Aβ α P and A be reachable from S Assumption: G is LL(k) A n A S First k (αβ n γ) First
More informationSyntax Analysis, VII The Canonical LR(1) Table Construction. Comp 412 COMP 412 FALL Chapter 3 in EaC2e. source code. IR IR target.
COMP 412 FALL 2017 Syntax Analysis, VII The Canonical LR(1) Table Construction Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights
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 informationCurs 8. LR(k) parsing. S.Motogna - FL&CD
Curs 8 LR(k) parsing Terms Reminder: rhp = right handside of production lhp = left handside of production Prediction see LL(1) Handle = symbols from the head of the working stack that form (in order) a
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 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 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 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 informationConflict Removal. Less Than, Equals ( <= ) Conflict
Conflict Removal As you have observed in a recent example, not all context free grammars are simple precedence grammars. You have also seen that a context free grammar that is not a simple precedence grammar
More informationSyntax Analysis, VI Examples from LR Parsing. Comp 412
COMP 412 FALL 2017 Syntax Analysis, VI Examples from LR Parsing Comp 412 source code IR IR target code Front End Optimizer Back End Copyright 2017, Keith D. Cooper & Linda Torczon, all rights reserved.
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 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 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 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 informationEverything You Always Wanted to Know About Parsing
Everything You Always Wanted to Know About Parsing Part V : LR Parsing University of Padua, Italy ESSLLI, August 2013 Introduction Parsing strategies classified by the time the associated PDA commits to
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 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 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 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 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 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 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 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 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 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. Building LR(1) Tables : ACTION and GOTO How do we build the parse tables for an LR(1) grammar? Use grammar to
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 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 informationCS415 Compilers Syntax Analysis Bottom-up Parsing
CS415 Compilers Syntax Analysis Bottom-up Parsing These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Review: LR(k) Shift-reduce Parsing Shift reduce
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 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 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 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 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 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 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 informationMA/CSSE 474 Theory of Computation
MA/CSSE 474 Theory of Computation CFL Hierarchy CFL Decision Problems Your Questions? Previous class days' material Reading Assignments HW 12 or 13 problems Anything else I have included some slides online
More informationCS Rewriting System - grammars, fa, and PDA
Restricted version of PDA If (p, γ) δ(q, a, X), a Σ {ε}, p. q Q, X Γ. restrict γ Γ in three ways: i) if γ = YX, (q, ay, Xβ) (p, y, YXβ) push Y Γ, ii) if γ = X, (q, ay, Xβ) (p, y, Xβ) no change on stack,
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 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 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 informationEverything You Always Wanted to Know About Parsing
Everything You Always Wanted to Know About Parsing Part IV : Parsing University of Padua, Italy ESSLLI, August 2013 Introduction First published in 1968 by Jay in his PhD dissertation, Carnegie Mellon
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 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 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 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 informationHarvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs
Harvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs Harry Lewis October 8, 2013 Reading: Sipser, pp. 119-128. Pushdown Automata (review) Pushdown Automata = Finite automaton
More informationMA/CSSE 474 Theory of Computation
MA/CSSE 474 Theory of Computation Bottom-up parsing Pumping Theorem for CFLs Recap: Going One Way Lemma: Each context-free language is accepted by some PDA. Proof (by construction): The idea: Let the stack
More informationProblem Session 5 (CFGs) Talk about the building blocks of CFGs: S 0S 1S ε - everything. S 0S0 1S1 A - waw R. S 0S0 0S1 1S0 1S1 A - xay, where x = y.
CSE2001, Fall 2006 1 Problem Session 5 (CFGs) Talk about the building blocks of CFGs: S 0S 1S ε - everything. S 0S0 1S1 A - waw R. S 0S0 0S1 1S0 1S1 A - xay, where x = y. S 00S1 A - xay, where x = 2 y.
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 informationCSE 105 THEORY OF COMPUTATION
CSE 105 THEORY OF COMPUTATION Spring 2016 http://cseweb.ucsd.edu/classes/sp16/cse105-ab/ Today's learning goals Sipser Ch 2 Define push down automata Trace the computation of a push down automaton Design
More information