A bottom-up parser traces a rightmost derivation in reverse. Bottom-Up Parsing. Bottom-up parsing is more general than topdown.

Size: px
Start display at page:

Download "A bottom-up parser traces a rightmost derivation in reverse. Bottom-Up Parsing. Bottom-up parsing is more general than topdown."

Transcription

1 Bottom-Up Parsing Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient Builds on ideas in top-down parsing Bottom-up is the preferred method Originated from Prof. Aiken CS 13 Modified by Yu Zhang 1 2 An Introductory xample Bottom-up parsers don t need left-factored grammars he Idea Bottom-up parsing reduces a string to the start symbol by inverting productions: Revert to the natural grammar for our example: + ) Consider the string: Observation Read the productions in reverse from bottom to top) his is a rightmost derivation! Important Fact #1 Important Fact #1 about bottom-up parsing: A bottom-up parser traces a rightmost derivation in reverse 5 6

2 A Bottom-up Parse A Bottom-up Parse in Detail 1) A Bottom-up Parse in Detail 2) A Bottom-up Parse in Detail 3) A Bottom-up Parse in Detail ) A Bottom-up Parse in Detail 5)

3 A Bottom-up Parse in Detail 6) A rivial Bottom-Up Parsing Algorithm Let I = input string repeat pick a non-empty substring of I where X is a production if no such, backtrack replace one by X in I until I = S the start symbol) or all possibilities are exhausted 13 1 Questions Does this algorithm terminate? How fast is the algorithm? Does the algorithm handle all cases? How do we choose the substring to reduce at each step? Where Do Reductions Happen? Important Fact #1 has an eresting consequence: Let be a step of a bottom-up parse Assume the next reduction is by X hen is a string of terminals Why? Because X is a step in a rightmost derivation Notation Idea: Split string o two substrings Right substring is as yet unexamined by parsing a string of terminals) Left substring has terminals and non-terminals he dividing po is marked by a he is not part of the string Shift-Reduce Parsing Bottom-up parsing uses only two kinds of actions: Shift Reduce Initially, all input is unexamined x 1 x 2... x n 1 18

4 Shift Shift: Move one place to the right Shifts a terminal to the left string ABC xyz ABCx yz Reduce Apply an inverse production at the right end of the left string If A xy is a production, then Cbxy ijk CbA ijk he xample with Reductions Only he xample with Shift-Reduce Parsing + shift + shift + shift + reduce + reduce + reduce + reduce + shift + shift + reduce + reduce + reduce + reduce + reduce + + reduce A Shift-Reduce Parse in Detail 1) + A Shift-Reduce Parse in Detail 2)

5 A Shift-Reduce Parse in Detail 3) A Shift-Reduce Parse in Detail ) A Shift-Reduce Parse in Detail 5) A Shift-Reduce Parse in Detail 6) A Shift-Reduce Parse in Detail ) A Shift-Reduce Parse in Detail 8)

6 A Shift-Reduce Parse in Detail 9) A Shift-Reduce Parse in Detail 10) A Shift-Reduce Parse in Detail ) he Stack Left string can be implemented by a stack op of the stack is the Shift pushes a terminal on the stack Reduce pops 0 or more symbols off of the stack production rhs) and pushes a nonterminal on the stack production lhs) 3 Conflicts Key Issue In a given state, more than one action shift or reduce) may lead to a valid parse If it is legal to shift or reduce, there is a shiftreduce conflict If it is legal to reduce by two different productions, there is a reduce-reduce conflict 35 How do we decide when to shift or reduce? xample grammar: + ) Consider step + We could shift: + We could reduce by giving + A fatal mistake! No way to reduce to the start symbol 36

7 移进 归约分析的冲突 1 移进 归约冲突 shift/reduce conflict) 例 stmt if expr then stmt ifexpr then stmt else stmt other 如果移进 归约分析器处于格局 configuration) 栈 输入 if expr then stmt else $ 3 移进 归约分析的冲突 2 归约 归约冲突 reduce/reduce conflict) stmt id parameter_list) expr = expr parameter_list parameter_list, parameter parameter parameter id expr id expr_list) id id ) 是数组元素的引用 expr_list expr_list, expr expr 由 AI, J) 开始的语句 栈 idid 归约成 expr 还是 parameter? 输入,id) 38 移进 归约分析的冲突 2 归约 归约冲突 stmt id parameter_list) expr = expr parameter_list parameter_list, parameter parameter parameter id expr id expr_list) id expr_list expr_list, expr expr 由 AI, J) 开始的语句 词法分析查符号表, 区分第一个 id) 自下而上分析方法和 LL 分析方法的比较 在下面的推导中, 最后一步用的是 A l S rm rm A b w rm l b w 栈 procid id 需要修改上面的文法 输入,id) 39 LL1) 决定用该产生式的位置 0 自下而上分析方法和 LL 分析方法的比较 在下面的推导中, 最后一步用的是 A l S rm rm A b w rm l b w LR1) 决定用该产生式的位置 LL1) 决定用该产生式的位置 1 Handles 句柄 ) Intuition: Want to reduce only if the result can still be reduced to the start symbol Assume a rightmost derivation S X hen X in the position after is a handle 句柄 )of 2

8 Handles Cont.) Handles formalize the uition A handle is a string that can be reduced and also allows further reductions back to the start symbol using a particular production at a specific spot) Important Fact #2 Important Fact #2 about bottom-up parsing: In shift-reduce parsing, handles appear only at the top of the stack, never inside We only want to reduce at handles Note: We have said what a handle is, not how to find handles 3 Why? Informal induction on # of reduce moves: rue initially, stack is empty Immediately after reducing a handle right-most non-terminal on top of the stack next handle must be to right of right-most nonterminal, because this is a right-most derivation Sequence of shift moves reaches next handle Summary of Handles In shift-reduce parsing, handles always appear at the top of the stack Handles are never to the left of the rightmost non-terminal herefore, shift-reduce moves are sufficient; the need never move left Bottom-up parsing algorithms are based on recognizing handles 5 6 Recognizing Handles Grammars here are no known efficient algorithms to recognize handles Solution: use heuristics to guess which stacks are handles On some CFGs, the heuristics always guess correctly For the heuristics we use here, these are the SLR grammars Other heuristics work for other grammars All CFGs Unambiguous CFGs SLR CFGs will generate conflicts 8

9 Viable Prefixes 活前缀 ) It is not obvious how to detect handles At each step the parser sees only the stack, not the entire input; start with that... is a viable prefix if there is an such that is a state of a shift-reduce parser 活前缀 : 是右句型的前缀, 该前缀不超过最右句柄的右端 S rm, S 是开始符号, 是右句型 9 Huh? What does this mean? A few things: A viable prefix does not extend past the right end of the handle It s a viable prefix because it is a prefix of the handle As long as a parser has viable prefixes on the stack no parsing error has been detected 50 Important Fact #3 Important Fact #3 about bottom-up parsing: For any grammar, the set of viable prefixes is a regular language Important Fact #3 Cont.) Important Fact #3 is non-obvious We show how to compute automata that accept viable prefixes Items 项 ) An item is a production with a. somewhere on the rhs he items for ) are.).).) ). Items Cont.) he only item for X is X. Items are often called LR0) items 53 5

10 Intuition he problem in recognizing viable prefixes is that the stack has only bits and pieces of the rhs of productions If it had a complete rhs, we could reduce hese bits and pieces are always prefixes of rhs of productions xample Consider the input ) hen ) is a state of a shift-reduce parse is a prefix of the rhs of ) Will be reduced after the next shift Item.) says that so far we have seen of this production and hope to see ) Generalization he stack may have many prefixes of rhs s Prefix 1 Prefix 2... Prefix n-1 Prefix n Let Prefix i be a prefix of rhs of X i i Prefix i will eventually reduce to X i he missing part of i-1 starts with X i i.e. there is a X i-1 Prefix i-1 X i for some An xample Consider the string ): ) is a state of a shift-reduce parse is a prefix of the rhs of ) is a prefix of the rhs of is a prefix of the rhs of Recursively, Prefix k+1 Prefix n eventually reduces to the missing part of k 5 58 An xample Cont.) he stack of items.).. Says We ve seen of ) We ve seen of We ve seen of Recognizing Viable Prefixes Idea: o recognize viable prefixes, we must Recognize a sequence of partial rhs s of productions, where ach sequence can eventually reduce to part of the missing suffix of its predecessor 59 60

11 An NFA Recognizing Viable Prefixes 1. Add a dummy production S S to G 2. he NFA states are the items of G Including the extra production An NFA Recognizing Viable Prefixes Cont.) 5. very state is an accepting state 6. Start state is S.S 3. For item.x add transition.x X X.. For item.x and production X add.x X NFA for Viable Prefixes of the xample ). ).).) ). S S.. +. NFA for Viable Prefixes in Detail 1) S NFA for Viable Prefixes in Detail 2) NFA for Viable Prefixes in Detail 3). ) S. S.. + S. S

12 NFA for Viable Prefixes in Detail ) NFA for Viable Prefixes in Detail 5). ). ).) S. S S. S NFA for Viable Prefixes in Detail 6) NFA for Viable Prefixes in Detail ). ) S. S..). +..).+. ) S. S..). +..).+ ) ) NFA for Viable Prefixes in Detail 8) NFA for Viable Prefixes in Detail 9). ) S. S..). +..).+ ) ). +.. ) S. S..). +..).+ ) )

13 NFA for Viable Prefixes in Detail 10) NFA for Viable Prefixes in Detail ). ) S. S..). +. ).).+. ) ) S. S..). +. ).).+. ) NFA for Viable Prefixes in Detail 12) NFA for Viable Prefixes in Detail 13). ) S. S..). +. ).).+. ) ) S. S..). +. ).).+. ) ranslation to the DFA.. +.) S S )...).).. ). ). +.. ).. +.).. Lingo he states of the DFA are canonical collections of items or canonical collections of LR0) items 项目集规范族 ) he Dragon book gives another way of constructing LR0) items 8

14 Valid Items 有效项目 ) Item X. is valid for a viable prefix if S X by a right-most derivation After parsing, the valid items are the possible tops of the stack of items Items Valid for a Prefix An item I is valid for a viable prefix if the DFA recognizing viable prefixes terminates on input in a state s containing I he items in s describe what the top of the item stack might be after reading input Valid Items xample An item is often valid for many prefixes xample: he item.) is valid for prefixes... Valid Items for S S )....) )....) ) ). +.. ).. +.) LR0) Parsing Idea: Assume stack contains next input is t DFA on input terminates in state s Reduce by X if scontains item X. Shift if scontains item X.t equivalent to saying s has a transition labeled t LR0) Conflicts LR0) has a reduce/reduce conflict if: Any state has two reduce items: X. and Y. LR0) has a shift/reduce conflict if: Any state has a reduce item and a shift item: X. and Y.t 83 8

15 +. LR0) Conflicts S... + S )....) )... ).. +.) wo. shift/reduce. conflicts with..) LR0) rules ) ). 85 例 S as 1S S. S 0S. S S. as S. a 2S a.s S. as S. S a 3S as. shift/reduce conflicts with LR0) rules 1S S. S S. a 0S. S S. Sa S. S Sa S No shift/reduce conflict a 2S Sa. SLR SLR Parsing L = Left-to-right scan R = Rightmost derivation SLR = Simple LR SLR improves on LR0) shift/reduce heuristics Fewer states have conflicts Idea: Assume stack contains next input is t DFA on input terminates in state s Reduce by X if scontains item X. t FollowX) Shift if scontains item X.t 8 88 SLR Parsing Cont.) If there are conflicts under these rules, the grammar is not SLR he rules amount to a heuristic for detecting handles he SLR grammars are those where the heuristics detect exactly the handles 89 例 S as 1S S. S 0S. S S. as S. a 2S a.s S. as S. S a 3S as. FollowS) = {$} 0, 2: reduce by S if t is $ S Sa 1S S. a S S. a S 2S Sa. 0S. S S. Sa S.

16 +. SLR Conflicts S... + S )....) )... ).. + Follow) = { ), $ }.) Follow). = { +, ), $ }.. No.) conflicts with ) SLR rules! ). Precedence Declarations Digression Lots of grammars aren t SLR including all ambiguous grammars We can parse more grammars by using precedence declarations Instructions for resolving conflicts Precedence Declarations Cont.) Consider our favorite ambiguous grammar: + ) he DFA for this grammar contains a state with the following items:.. + shift/reduce conflict! Declaring has higher precedence than + resolves this conflict in favor of reducing Precedence Declarations Cont.) he term precedence declaration is misleading hese declarations do not define precedence; they define conflict resolutions Not quite the same thing! 93 9 Notes If there is a conflict in the last step, grammar is not SLRk) k is the amount of lookahead In practice k = 1 Naïve SLR Parsing Algorithm 1. Let M be DFA for viable prefixes of G 2. Let x 1 x n $ be initial configuration 3. Repeat until configuration is S $ Let be current configuration Run M on current stack If M rejects, report parsing error Stack is not a viable prefix If M accepts with items I, let a be next input Shift if X. a I Reduce if X I and a FollowX) Report parsing error if neither applies 95 96

17 SLR xample Configuration DFA Halt State Action $ 1 shift Configuration $ ) 6 2 S S )...).).. ) ) ) SLR xample Configuration DFA Halt State Action $ 1 shift $ 3 not in Follow) shift Configuration $ ) 6 2 S S )...).).. ) ) ) Configuration $ ) 6 2 S S )...).).. ) ) ).. SLR xample Configuration DFA Halt State Action $ 1 shift $ 3 not in Follow) shift $ shift

18 Configuration $ ) 6 2 S S )...).).. ) ) ).. Configuration $ ) 6 2 S S )...).).. ) ) ) Configuration $ ) 6 2 S S )...).).. ) ) ) SLR xample Configuration DFA Halt State Action $ 1 shift $ 3 not in Follow) shift $ shift $ 3 $ Follow) red. 106 Configuration $ ) 6 2 S S )...).).. ) ) ).. Configuration $ ) 6 2 S S )...).).. ) ) )

19 Configuration $ ) 6 2 S S )...).).. ) ) ).. Configuration $ ) 6 2 S S )...).).. ) ) ) SLR xample Configuration DFA Halt State Action $ 1 shift $ 3 not in Follow) shift $ shift $ 3 $ Follow) red. $ $ Follow) red. 1 Configuration $ ) 6 2 S S )...).).. ) ) ).. 2 Configuration $ ) 6 2 S S )...).).. ) ) ).. Configuration $ ) 6 2 S S )...).).. ) ) ).. 3

20 Configuration $ ) 6 2 S S )...).).. ) ) ).. 5 SLR xample Configuration DFA Halt State Action $ 1 shift $ 3 not in Follow) shift $ shift $ 3 $ Follow) red. $ $ Follow) red. $ 5 $ Follow) red. 6 Configuration $ 2 S S )...) ) 6....) ) ) ).. Configuration $ 2 S S )...) ) 6....) ) ) ).. 8 SLR xample Notes Configuration DFA Halt State Action $ 1 shift $ 3 not in Follow) shift $ shift $ 3 $ Follow) red. $ $ Follow) red. $ 5 $ Follow) red. $ accept Skipped using extra start state S in this example to save space on slides Rerunning the automaton at each step is wasteful Most of the work is repeated 9 120

21 An Improvement Remember the state of the automaton on each prefix of the stack Change stack to contain pairs Symbol, DFA State An Improvement Cont.) For a stack sym 1, state 1... sym n, state n state n is the final state of the DFA on sym 1 sym n Detail: he bottom of the stack is any,start where anyis any dummy symbol startis the start state of the DFA Goto able Define goto[i,a] = j if state i A state j goto is just the transition function of the DFA One of two parsing tables Refined Parser Moves Shift x Push a, x on the stack ais current input xis a DFA state Reduce X As before Accept rror Action able For each state s i and terminal a If s i has item X.a and goto[i,a] = j then action[i,a] = shift j If s i has item X. and a FollowX) and X S then action[i,a] = reduce X If s i has item S S. then action[i,$] = accept Otherwise, action[i,a] = error 125 SLR Parsing Algorithm Let I = w$ be initial input Let j = 0 Let DFA state 1 have item S.S Let stack = dummy, 1 repeat case action[top_statestack),i[j]] of shift k: push I[j++], k reduce X A: pop A pairs, push X, goto[top_statestack),x] accept: halt normally error: halt and report error 126

22 例 S as S S Sa S 1S S. 1S S. 2S a.s S S. a a S S a. as S 0S. S S. 0S. S 2S Sa. S. as S. a S 3S as. S. Sa S. a $ S a $ S 0 s2 r2 1 0 r2 r2 1 1 acc 1 s2 acc 2 s2 r2 3 2 r1 r1 3 r1 例 S as S S Sa S a $ S a $ S 0 s2 r2 1 0 r2 r2 1 1 acc 1 s2 acc 2 s2 r2 3 2 r1 r1 3 r1 aaa$ shift aaa$ reduce a2 aa$ shift S1 aaa$ shift a2a2 a$ shift S1a2 aa$ reduce a2a2a2 $ reduce S1 aa$ shift a2a2a2s3 $ reduce S1a2 a$ reduce a2a2s3 $ reduce S1 a$ shift a2s3 $ reduce S1a2 $ reduce S1 $ accept S1 $ accept Notes on SLR Parsing Algorithm Note that the algorithm uses only the DFA states and the input he stack symbols are never used! However, we still need the symbols for semantic actions More Notes Some common constructs are not SLR1) LR1) is more powerful Build lookahead o the items An LR1) item is a pair: LR0) item x lookahead [, $] means After seeing reduce if lookahead is $ More accurate than just using follow sets ake a look at the LR1) automaton for your parser!

Bottom-Up Parsing II (Different types of Shift-Reduce Conflicts) Lecture 10. Prof. Aiken (Modified by Professor Vijay Ganesh.

Bottom-Up Parsing II (Different types of Shift-Reduce Conflicts) Lecture 10. Prof. Aiken (Modified by Professor Vijay Ganesh. Bottom-Up Parsing II Different types of Shift-Reduce Conflicts) Lecture 10 Ganesh. Lecture 10) 1 Review: Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient Doesn

More information

Review: Shift-Reduce Parsing. Bottom-up parsing uses two actions: Bottom-Up Parsing II. Shift ABC xyz ABCx yz. Lecture 8. Reduce Cbxy ijk CbA ijk

Review: Shift-Reduce Parsing. Bottom-up parsing uses two actions: Bottom-Up Parsing II. Shift ABC xyz ABCx yz. Lecture 8. Reduce Cbxy ijk CbA ijk Review: Shift-Reduce Parsing Bottom-up parsing uses two actions: Bottom-Up Parsing II Lecture 8 Shift ABC xyz ABCx yz Reduce Cbxy ijk CbA ijk Prof. Aiken CS 13 Lecture 8 1 Prof. Aiken CS 13 Lecture 8 2

More information

Bottom-Up Parsing II. Lecture 8

Bottom-Up Parsing II. Lecture 8 Bottom-Up Parsing II Lecture 8 1 Review: Shift-Reduce Parsing Bottom-up parsing uses two actions: Shift ABC xyz ABCx yz Reduce Cbxy ijk CbA ijk 2 Recall: he Stack Left string can be implemented by a stack

More information

Introduction to Bottom-Up Parsing

Introduction to Bottom-Up Parsing Introduction to Bottom-Up Parsing Lecture 11 CS 536 Spring 2001 1 Outline he strategy: shift-reduce parsing Ambiguity and precedence declarations Next lecture: bottom-up parsing algorithms CS 536 Spring

More information

Outline. The strategy: shift-reduce parsing. Introduction to Bottom-Up Parsing. A key concept: handles

Outline. The strategy: shift-reduce parsing. Introduction to Bottom-Up Parsing. A key concept: handles Outline Introduction to Bottom-Up Parsing Lecture Notes by Profs. Alex Aiken and George Necula (UCB) he strategy: -reduce parsing A key concept: handles Ambiguity and precedence declarations CS780(Prasad)

More information

Intro to Bottom-up Parsing. Lecture 9

Intro to Bottom-up Parsing. Lecture 9 Intro to Bottom-up Parsing Lecture 9 Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient Builds on ideas in top-down parsing Bottom-up is the preferred method

More information

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1 Review of CFGs and Parsing II Bottom-up Parsers Lecture 5 1 Outline Parser Overview op-down Parsers (Covered largely through labs) Bottom-up Parsers 2 he Functionality of the Parser Input: sequence of

More information

Bottom-Up Parsing. Lecture 11-12

Bottom-Up Parsing. Lecture 11-12 Bottom-Up Parsing Lecture 11-12 (From slides by G. Necula & R. Bodik) 2/20/08 Prof. Hilfinger CS164 Lecture 11 1 Administrivia Test I during class on 10 March. 2/20/08 Prof. Hilfinger CS164 Lecture 11

More information

Bottom-Up Parsing. Lecture 11-12

Bottom-Up Parsing. Lecture 11-12 Bottom-Up Parsing Lecture 11-12 (From slides by G. Necula & R. Bodik) 9/22/06 Prof. Hilfinger CS164 Lecture 11 1 Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient

More information

Homework 3 Dragon: 4.6.2, 4.6.3, 4.6.5, 4.6.6, , 4.7.4, , 4.8.2

Homework 3 Dragon: 4.6.2, 4.6.3, 4.6.5, 4.6.6, , 4.7.4, , 4.8.2 Homework 3 Dragon: 4.6.2, 4.6.3, 4.6.5, 4.6.6, 4.6.9 4.7.1, 4.7.4, 4.7.5 4.8.1, 4.8.2 Tiger: 3.8, 3.10, 3.11, 3.12, 3.13 Dragon Book 4.6.2: Construct the SLR sets of items for the (augmented) grammar of

More information

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7 Top-Down Parsing and Intro to Bottom-Up Parsing Lecture 7 1 Predictive Parsers Like recursive-descent but parser can predict which production to use Predictive parsers are never wrong Always able to guess

More information

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7 Top-Down Parsing and Intro to Bottom-Up Parsing Lecture 7 1 Predictive Parsers Like recursive-descent but parser can predict which production to use Predictive parsers are never wrong Always able to guess

More information

LR Parsing Techniques

LR Parsing Techniques LR Parsing Techniques Introduction Bottom-Up Parsing LR Parsing as Handle Pruning Shift-Reduce Parser LR(k) Parsing Model Parsing Table Construction: SLR, LR, LALR 1 Bottom-UP Parsing A bottom-up parser

More information

Lecture 8: Deterministic Bottom-Up Parsing

Lecture 8: Deterministic Bottom-Up Parsing Lecture 8: Deterministic Bottom-Up Parsing (From slides by G. Necula & R. Bodik) Last modified: Fri Feb 12 13:02:57 2010 CS164: Lecture #8 1 Avoiding nondeterministic choice: LR We ve been looking at general

More information

Bottom-Up Parsing LR Parsing

Bottom-Up Parsing LR Parsing Bottom-Up Parsing LR Parsing Maryam Siahbani 2/19/2016 1 What we need for LR parsing LR0) states: Describe all possible states in which parser can be Parsing table ransition between LR0) states Actions

More information

Lecture 7: Deterministic Bottom-Up Parsing

Lecture 7: Deterministic Bottom-Up Parsing Lecture 7: Deterministic Bottom-Up Parsing (From slides by G. Necula & R. Bodik) Last modified: Tue Sep 20 12:50:42 2011 CS164: Lecture #7 1 Avoiding nondeterministic choice: LR We ve been looking at general

More information

LR Parsing LALR Parser Generators

LR Parsing LALR Parser Generators Outline LR Parsing LALR Parser Generators Review of bottom-up parsing Computing the parsing DFA Using parser generators 2 Bottom-up Parsing (Review) A bottom-up parser rewrites the input string to the

More information

LR Parsing LALR Parser Generators

LR Parsing LALR Parser Generators LR Parsing LALR Parser Generators Outline Review of bottom-up parsing Computing the parsing DFA Using parser generators 2 Bottom-up Parsing (Review) A bottom-up parser rewrites the input string to the

More information

S Y N T A X A N A L Y S I S LR

S Y N T A X A N A L Y S I S LR LR parsing There are three commonly used algorithms to build tables for an LR parser: 1. SLR(1) = LR(0) plus use of FOLLOW set to select between actions smallest class of grammars smallest tables (number

More information

Shift. Reduce. Review: Shift-Reduce Parsing. Bottom-up parsing uses two actions: Bottom-Up Parsing II. ABC xyz ABCx yz. Lecture 8.

Shift. Reduce. Review: Shift-Reduce Parsing. Bottom-up parsing uses two actions: Bottom-Up Parsing II. ABC xyz ABCx yz. Lecture 8. Rviw: Shift-Rduc Parsing Bottom-up parsing uss two actions: Bottom-Up Parsing II Lctur 8 Shift ABC xyz ABCx yz Rduc Cbxy ijk CbA ijk Prof. Aikn CS 13 Lctur 8 1 Prof. Aikn CS 13 Lctur 8 2 Rcall: h Stack

More information

Compilers. Bottom-up Parsing. (original slides by Sam

Compilers. Bottom-up Parsing. (original slides by Sam Compilers Bottom-up Parsing Yannis Smaragdakis U Athens Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Bottom-Up Parsing More general than top-down parsing And just as efficient Builds

More information

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1 CSE P 501 Compilers LR Parsing Hal Perkins Spring 2018 UW CSE P 501 Spring 2018 D-1 Agenda LR Parsing Table-driven Parsers Parser States Shift-Reduce and Reduce-Reduce conflicts UW CSE P 501 Spring 2018

More information

Lexical and Syntax Analysis. Bottom-Up Parsing

Lexical and Syntax Analysis. Bottom-Up Parsing Lexical and Syntax Analysis Bottom-Up Parsing Parsing There are two ways to construct derivation of a grammar. Top-Down: begin with start symbol; repeatedly replace an instance of a production s LHS with

More information

Bottom up parsing. The sentential forms happen to be a right most derivation in the reverse order. S a A B e a A d e. a A d e a A B e S.

Bottom up parsing. The sentential forms happen to be a right most derivation in the reverse order. S a A B e a A d e. a A d e a A B e S. Bottom up parsing Construct a parse tree for an input string beginning at leaves and going towards root OR Reduce a string w of input to start symbol of grammar Consider a grammar S aabe A Abc b B d And

More information

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Formal Languages and Compilers Lecture VII Part 3: Syntactic A Formal Languages and Compilers Lecture VII Part 3: Syntactic Analysis Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

More information

A left-sentential form is a sentential form that occurs in the leftmost derivation of some sentence.

A left-sentential form is a sentential form that occurs in the leftmost derivation of some sentence. Bottom-up parsing Recall For a grammar G, with start symbol S, any string α such that S α is a sentential form If α V t, then α is a sentence in L(G) A left-sentential form is a sentential form that occurs

More information

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309 PART 3 - SYNTAX ANALYSIS F. Wotawa (IST @ TU Graz) Compiler Construction Summer term 2016 64 / 309 Goals Definition of the syntax of a programming language using context free grammars Methods for parsing

More information

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino 3. Syntax Analysis Andrea Polini Formal Languages and Compilers Master in Computer Science University of Camerino (Formal Languages and Compilers) 3. Syntax Analysis CS@UNICAM 1 / 54 Syntax Analysis: the

More information

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1) TD parsing - LL(1) Parsing First and Follow sets Parse table construction BU Parsing Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1) Problems with SLR Aho, Sethi, Ullman, Compilers

More information

UNIT-III BOTTOM-UP PARSING

UNIT-III BOTTOM-UP PARSING UNIT-III BOTTOM-UP PARSING Constructing a parse tree for an input string beginning at the leaves and going towards the root is called bottom-up parsing. A general type of bottom-up parser is a shift-reduce

More information

LR Parsing Techniques

LR Parsing Techniques LR Parsing Techniques Bottom-Up Parsing - LR: a special form of BU Parser LR Parsing as Handle Pruning Shift-Reduce Parser (LR Implementation) LR(k) Parsing Model - k lookaheads to determine next action

More information

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1 CSE 401 Compilers LR Parsing Hal Perkins Autumn 2011 10/10/2011 2002-11 Hal Perkins & UW CSE D-1 Agenda LR Parsing Table-driven Parsers Parser States Shift-Reduce and Reduce-Reduce conflicts 10/10/2011

More information

Bottom-up parsing. Bottom-Up Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form

Bottom-up parsing. Bottom-Up Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form Bottom-up parsing Bottom-up parsing Recall Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form If α V t,thenα is called a sentence in L(G) Otherwise it is just

More information

shift-reduce parsing

shift-reduce parsing Parsing #2 Bottom-up Parsing Rightmost derivations; use of rules from right to left Uses a stack to push symbols the concatenation of the stack symbols with the rest of the input forms a valid bottom-up

More information

MIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

MIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology MIT 6.035 Parse Table Construction Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Parse Tables (Review) ACTION Goto State ( ) $ X s0 shift to s2 error error goto s1

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 8! Bo;om- Up Parsing Shi?- Reduce LR(0) automata and

More information

LALR Parsing. What Yacc and most compilers employ.

LALR Parsing. What Yacc and most compilers employ. LALR Parsing Canonical sets of LR(1) items Number of states much larger than in the SLR construction LR(1) = Order of thousands for a standard prog. Lang. SLR(1) = order of hundreds for a standard prog.

More information

Previous on Computer Networks Class 18. ICMP: Internet Control Message Protocol IP Protocol Actually a IP packet

Previous on Computer Networks Class 18. ICMP: Internet Control Message Protocol IP Protocol Actually a IP packet ICMP: Internet Control Message Protocol IP Protocol Actually a IP packet 前 4 个字节都是一样的 0 8 16 31 类型代码检验和 ( 这 4 个字节取决于 ICMP 报文的类型 ) ICMP 的数据部分 ( 长度取决于类型 ) ICMP 报文 首部 数据部分 IP 数据报 ICMP: Internet Control Message

More information

MODULE 14 SLR PARSER LR(0) ITEMS

MODULE 14 SLR PARSER LR(0) ITEMS MODULE 14 SLR PARSER LR(0) ITEMS In this module we shall discuss one of the LR type parser namely SLR parser. The various steps involved in the SLR parser will be discussed with a focus on the construction

More information

Context-free grammars

Context-free grammars Context-free grammars Section 4.2 Formal way of specifying rules about the structure/syntax of a program terminals - tokens non-terminals - represent higher-level structures of a program start symbol,

More information

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing Parsing Wrapup Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing LR(1) items Computing closure Computing goto LR(1) canonical collection This lecture LR(1) parsing Building ACTION

More information

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous. Section A 1. What do you meant by parser and its types? A parser for grammar G is a program that takes as input a string w and produces as output either a parse tree for w, if w is a sentence of G, or

More information

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table. MODULE 18 LALR parsing After understanding the most powerful CALR parser, in this module we will learn to construct the LALR parser. The CALR parser has a large set of items and hence the LALR parser is

More information

Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale

Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (1) Principle of Compilers Lecture IV Part 4: Syntactic Analysis Alessandro Artale Faculty of Computer Science Free

More information

Bottom-Up Parsing. Parser Generation. LR Parsing. Constructing LR Parser

Bottom-Up Parsing. Parser Generation. LR Parsing. Constructing LR Parser Parser Generation Main Problem: given a grammar G, how to build a top-down parser or a bottom-up parser for it? parser : a program that, given a sentence, reconstructs a derivation for that sentence ----

More information

Bottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing

Bottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing Also known as Shift-Reduce parsing More powerful than top down Don t need left factored grammars Can handle left recursion Attempt to construct parse tree from an input string eginning at leaves and working

More information

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised: EDAN65: Compilers, Lecture 06 A LR parsing Görel Hedin Revised: 2017-09-11 This lecture Regular expressions Context-free grammar Attribute grammar Lexical analyzer (scanner) Syntactic analyzer (parser)

More information

Monday, September 13, Parsers

Monday, September 13, Parsers Parsers Agenda Terminology LL(1) Parsers Overview of LR Parsing Terminology Grammar G = (Vt, Vn, S, P) Vt is the set of terminals Vn is the set of non-terminals S is the start symbol P is the set of productions

More information

计算机组成原理第二讲 第二章 : 运算方法和运算器 数据与文字的表示方法 (1) 整数的表示方法. 授课老师 : 王浩宇

计算机组成原理第二讲 第二章 : 运算方法和运算器 数据与文字的表示方法 (1) 整数的表示方法. 授课老师 : 王浩宇 计算机组成原理第二讲 第二章 : 运算方法和运算器 数据与文字的表示方法 (1) 整数的表示方法 授课老师 : 王浩宇 haoyuwang@bupt.edu.cn 1 Today: Bits, Bytes, and Integers Representing information as bits Bit-level manipulations Integers Representation: unsigned

More information

Lecture Bottom-Up Parsing

Lecture Bottom-Up Parsing Lecture 14+15 Bottom-Up Parsing CS 241: Foundations of Sequential Programs Winter 2018 Troy Vasiga et al University of Waterloo 1 Example CFG 1. S S 2. S AyB 3. A ab 4. A cd 5. B z 6. B wz 2 Stacks in

More information

CS 4120 Introduction to Compilers

CS 4120 Introduction to Compilers CS 4120 Introduction to Compilers Andrew Myers Cornell University Lecture 6: Bottom-Up Parsing 9/9/09 Bottom-up parsing A more powerful parsing technology LR grammars -- more expressive than LL can handle

More information

In One Slide. Outline. LR Parsing. Table Construction

In One Slide. Outline. LR Parsing. Table Construction LR Parsing Table Construction #1 In One Slide An LR(1) parsing table can be constructed automatically from a CFG. An LR(1) item is a pair made up of a production and a lookahead token; it represents a

More information

Syntax Analysis. Amitabha Sanyal. (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Syntax Analysis. Amitabha Sanyal. (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay Syntax Analysis (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay September 2007 College of Engineering, Pune Syntax Analysis: 2/124 Syntax

More information

Wednesday, August 31, Parsers

Wednesday, August 31, Parsers Parsers How do we combine tokens? Combine tokens ( words in a language) to form programs ( sentences in a language) Not all combinations of tokens are correct programs (not all sentences are grammatically

More information

CS5371 Theory of Computation. Lecture 8: Automata Theory VI (PDA, PDA = CFG)

CS5371 Theory of Computation. Lecture 8: Automata Theory VI (PDA, PDA = CFG) CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG) Objectives Introduce Pushdown Automaton (PDA) Show that PDA = CFG In terms of descriptive power Pushdown Automaton (PDA) Roughly

More information

UNIT III & IV. Bottom up parsing

UNIT III & IV. Bottom up parsing UNIT III & IV Bottom up parsing 5.0 Introduction Given a grammar and a sentence belonging to that grammar, if we have to show that the given sentence belongs to the given grammar, there are two methods.

More information

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

Formal Languages and Compilers Lecture VII Part 4: Syntactic A Formal Languages and Compilers Lecture VII Part 4: Syntactic Analysis Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

More information

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468 Parsers Xiaokang Qiu Purdue University ECE 468 August 31, 2018 What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure

More information

Compiler Construction: Parsing

Compiler Construction: Parsing Compiler Construction: Parsing Mandar Mitra Indian Statistical Institute M. Mitra (ISI) Parsing 1 / 33 Context-free grammars. Reference: Section 4.2 Formal way of specifying rules about the structure/syntax

More information

SLR parsers. LR(0) items

SLR parsers. LR(0) items SLR parsers LR(0) items As we have seen, in order to make shift-reduce parsing practical, we need a reasonable way to identify viable prefixes (and so, possible handles). Up to now, it has not been clear

More information

WWW.STUDENTSFOCUS.COM UNIT -3 SYNTAX ANALYSIS 3.1 ROLE OF THE PARSER Parser obtains a string of tokens from the lexical analyzer and verifies that it can be generated by the language for the source program.

More information

Wednesday, September 9, 15. Parsers

Wednesday, September 9, 15. Parsers Parsers What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda

More information

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs: What is a parser Parsers A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda

More information

Compiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6

Compiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6 Compiler Design 1 Bottom-UP Parsing Compiler Design 2 The Process The parse tree is built starting from the leaf nodes labeled by the terminals (tokens). The parser tries to discover appropriate reductions,

More information

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution Exam Facts Format Wednesday, July 20 th from 11:00 a.m. 1:00 p.m. in Gates B01 The exam is designed to take roughly 90

More information

Parser Generation. Bottom-Up Parsing. Constructing LR Parser. LR Parsing. Construct parse tree bottom-up --- from leaves to the root

Parser Generation. Bottom-Up Parsing. Constructing LR Parser. LR Parsing. Construct parse tree bottom-up --- from leaves to the root Parser Generation Main Problem: given a grammar G, how to build a top-down parser or a bottom-up parser for it? parser : a program that, given a sentence, reconstructs a derivation for that sentence ----

More information

Parsing Algorithms. Parsing: continued. Top Down Parsing. Predictive Parser. David Notkin Autumn 2008

Parsing Algorithms. Parsing: continued. Top Down Parsing. Predictive Parser. David Notkin Autumn 2008 Parsing: continued David Notkin Autumn 2008 Parsing Algorithms Earley s algorithm (1970) works for all CFGs O(N 3 ) worst case performance O(N 2 ) for unambiguous grammars Based on dynamic programming,

More information

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F). CS 2210 Sample Midterm 1. Determine if each of the following claims is true (T) or false (F). F A language consists of a set of strings, its grammar structure, and a set of operations. (Note: a language

More information

LR Parsing. Leftmost and Rightmost Derivations. Compiler Design CSE 504. Derivations for id + id: T id = id+id. 1 Shift-Reduce Parsing.

LR Parsing. Leftmost and Rightmost Derivations. Compiler Design CSE 504. Derivations for id + id: T id = id+id. 1 Shift-Reduce Parsing. LR Parsing Compiler Design CSE 504 1 Shift-Reduce Parsing 2 LR Parsers 3 SLR and LR(1) Parsers Last modifled: Fri Mar 06 2015 at 13:50:06 EST Version: 1.7 16:58:46 2016/01/29 Compiled at 12:57 on 2016/02/26

More information

LR Parsing E T + E T 1 T

LR Parsing E T + E T 1 T LR Parsing 1 Introduction Before reading this quick JFLAP tutorial on parsing please make sure to look at a reference on LL parsing to get an understanding of how the First and Follow sets are defined.

More information

Compiler Construction 2016/2017 Syntax Analysis

Compiler Construction 2016/2017 Syntax Analysis Compiler Construction 2016/2017 Syntax Analysis Peter Thiemann November 2, 2016 Outline 1 Syntax Analysis Recursive top-down parsing Nonrecursive top-down parsing Bottom-up parsing Syntax Analysis tokens

More information

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012 Predictive Parsers LL(k) Parsing Can we avoid backtracking? es, if for a given input symbol and given nonterminal, we can choose the alternative appropriately. his is possible if the first terminal of

More information

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones Parsing III (Top-down parsing: recursive descent & LL(1) ) (Bottom-up parsing) CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones Copyright 2003, Keith D. Cooper,

More information

Syntax Analysis, VI Examples from LR Parsing. Comp 412

Syntax Analysis, VI Examples from LR Parsing. Comp 412 Midterm Exam: Thursday October 18, 7PM Herzstein Amphitheater Syntax Analysis, VI Examples from LR Parsing Comp 412 COMP 412 FALL 2018 source code IR IR target Front End Optimizer Back End code Copyright

More information

Syntax Analysis Part I

Syntax Analysis Part I Syntax Analysis Part I Chapter 4: Context-Free Grammars Slides adapted from : Robert van Engelen, Florida State University Position of a Parser in the Compiler Model Source Program Lexical Analyzer Token,

More information

Command Dictionary CUSTOM

Command Dictionary CUSTOM 命令模式 CUSTOM [(filename)] [parameters] Executes a "custom-designed" command which has been provided by special programming using the GHS Programming Interface. 通过 GHS 程序接口, 执行一个 用户设计 的命令, 该命令由其他特殊程序提供 参数说明

More information

Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412

Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412 Midterm Exam: Thursday October 18, 7PM Herzstein Amphitheater Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412 COMP 412 FALL 2018 source code IR Front End Optimizer Back End IR target

More information

CS308 Compiler Principles Syntax Analyzer Li Jiang

CS308 Compiler Principles Syntax Analyzer Li Jiang CS308 Syntax Analyzer Li Jiang Department of Computer Science and Engineering Shanghai Jiao Tong University Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program.

More information

CS 164 Programming Languages and Compilers Handout 9. Midterm I Solution

CS 164 Programming Languages and Compilers Handout 9. Midterm I Solution Midterm I Solution Please read all instructions (including these) carefully. There are 5 questions on the exam, some with multiple parts. You have 1 hour and 20 minutes to work on the exam. The exam is

More information

Ambiguity. Grammar E E + E E * E ( E ) int. The string int * int + int has two parse trees. * int

Ambiguity. Grammar E E + E E * E ( E ) int. The string int * int + int has two parse trees. * int Administrivia Ambiguity, Precedence, Associativity & op-down Parsing eam assignments this evening for all those not listed as having one. HW#3 is now available, due next uesday morning (Monday is a holiday).

More information

Conflicts in LR Parsing and More LR Parsing Types

Conflicts in LR Parsing and More LR Parsing Types Conflicts in LR Parsing and More LR Parsing Types Lecture 10 Dr. Sean Peisert ECS 142 Spring 2009 1 Status Project 2 Due Friday, Apr. 24, 11:55pm The usual lecture time is being replaced by a discussion

More information

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 4. Y.N. Srikant

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 4. Y.N. Srikant Syntax Analysis: Context-free Grammars, Pushdown Automata and Part - 4 Department of Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler

More information

Ambiguity, Precedence, Associativity & Top-Down Parsing. Lecture 9-10

Ambiguity, Precedence, Associativity & Top-Down Parsing. Lecture 9-10 Ambiguity, Precedence, Associativity & Top-Down Parsing Lecture 9-10 (From slides by G. Necula & R. Bodik) 9/18/06 Prof. Hilfinger CS164 Lecture 9 1 Administrivia Please let me know if there are continued

More information

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1 CSE P 501 Compilers Parsing & Context-Free Grammars Hal Perkins Winter 2008 1/15/2008 2002-08 Hal Perkins & UW CSE C-1 Agenda for Today Parsing overview Context free grammars Ambiguous grammars Reading:

More information

Downloaded from Page 1. LR Parsing

Downloaded from  Page 1. LR Parsing Downloaded from http://himadri.cmsdu.org Page 1 LR Parsing We first understand Context Free Grammars. Consider the input string: x+2*y When scanned by a scanner, it produces the following stream of tokens:

More information

Outline. Limitations of regular languages Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

Outline. Limitations of regular languages Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation Outline Introduction to Parsing Lecture 8 Adapted from slides by G. Necula and R. Bodik Limitations of regular languages Parser overview Context-free grammars (CG s) Derivations Syntax-Directed ranslation

More information

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite LALR r Driver Action Table for CSX-Lite Given the GoTo and parser action tables, a Shift/Reduce (LALR) parser is fairly simple: { S 5 9 5 9 void LALRDriver(){ Push(S ); } R S R R R R5 if S S R S R5 while(true){

More information

CSE P 501 Exam 11/17/05 Sample Solution

CSE P 501 Exam 11/17/05 Sample Solution 1. (8 points) Write a regular expression or set of regular expressions that generate the following sets of strings. You can use abbreviations (i.e., name = regular expression) if it helps to make your

More information

CS606- compiler instruction Solved MCQS From Midterm Papers

CS606- compiler instruction Solved MCQS From Midterm Papers CS606- compiler instruction Solved MCQS From Midterm Papers March 06,2014 MC100401285 Moaaz.pk@gmail.com Mc100401285@gmail.com PSMD01 Final Term MCQ s and Quizzes CS606- compiler instruction If X is a

More information

Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals.

Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals. Bottom-up Parsing: Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals. Basic operation is to shift terminals from the input to the

More information

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1 Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery Last modified: Mon Feb 23 10:05:56 2015 CS164: Lecture #14 1 Shift/Reduce Conflicts If a DFA state contains both [X: α aβ, b] and [Y: γ, a],

More information

More Bottom-Up Parsing

More Bottom-Up Parsing More Bottom-Up Parsing Lecture 7 Dr. Sean Peisert ECS 142 Spring 2009 1 Status Project 1 Back By Wednesday (ish) savior lexer in ~cs142/s09/bin Project 2 Due Friday, Apr. 24, 11:55pm My office hours 3pm

More information

SYNTAX ANALYSIS 1. Define parser. Hierarchical analysis is one in which the tokens are grouped hierarchically into nested collections with collective meaning. Also termed as Parsing. 2. Mention the basic

More information

Parsing II Top-down parsing. Comp 412

Parsing II Top-down parsing. Comp 412 COMP 412 FALL 2018 Parsing II Top-down parsing Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled

More information

CS143 Midterm Fall 2008

CS143 Midterm Fall 2008 CS143 Midterm Fall 2008 Please read all instructions (including these) carefully. There are 4 questions on the exam, some with multiple parts. You have 75 minutes to work on the exam. The exam is closed

More information

Midterm I (Solutions) CS164, Spring 2002

Midterm I (Solutions) CS164, Spring 2002 Midterm I (Solutions) CS164, Spring 2002 February 28, 2002 Please read all instructions (including these) carefully. There are 9 pages in this exam and 5 questions, each with multiple parts. Some questions

More information

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI UNIT I - LEXICAL ANALYSIS 1. What is the role of Lexical Analyzer? [NOV 2014] 2. Write

More information

Compilation 2012 Context-Free Languages Parsers and Scanners. Jan Midtgaard Michael I. Schwartzbach Aarhus University

Compilation 2012 Context-Free Languages Parsers and Scanners. Jan Midtgaard Michael I. Schwartzbach Aarhus University Compilation 2012 Parsers and Scanners Jan Midtgaard Michael I. Schwartzbach Aarhus University Context-Free Grammars Example: sentence subject verb object subject person person John Joe Zacharias verb asked

More information

CSE 401 Midterm Exam Sample Solution 2/11/15

CSE 401 Midterm Exam Sample Solution 2/11/15 Question 1. (10 points) Regular expression warmup. For regular expression questions, you must restrict yourself to the basic regular expression operations covered in class and on homework assignments:

More information

PCU50 的整盘备份. 本文只针对操作系统为 Windows XP 版本的 PCU50 PCU50 启动硬件自检完后, 出现下面文字时, 按向下光标键 光标条停在 SINUMERIK 下方的空白处, 如下图, 按回车键 PCU50 会进入到服务画面, 如下图

PCU50 的整盘备份. 本文只针对操作系统为 Windows XP 版本的 PCU50 PCU50 启动硬件自检完后, 出现下面文字时, 按向下光标键 光标条停在 SINUMERIK 下方的空白处, 如下图, 按回车键 PCU50 会进入到服务画面, 如下图 PCU50 的整盘备份 本文只针对操作系统为 Windows XP 版本的 PCU50 PCU50 启动硬件自检完后, 出现下面文字时, 按向下光标键 OS Loader V4.00 Please select the operating system to start: SINUMERIK Use and to move the highlight to your choice. Press Enter

More information