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


 Basil Strickland
 4 years ago
 Views:
Transcription
1 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 University of Bolzano Room: artale/ 2003/2004 Second Semester
2 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (2) Summary of Lecture IV Part 4 Inadequacy of SLR Parsing LR(1) Parsing Closure and Goto Operations, Canonical Collection LR(1) Parsing Tables LALR(1) Parsing Conflicts in LALR Parsers LALR(1) Parsing Tables LALR Vs LR Parsers Dealing with Ambiguous Grammars
3 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (3) When SLR is not Adequate Example Consider the following Grammar and the Canonical Collection: Augmented Grammar! "
4 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (4) When SLR is not Adequate (Cont) Consider now the set of items in : 1 If we consider the first item, then, action shift 6 2 If we consider the second item, since action reduce FOLLOW, then, Since there is both a shift and a reduce action the entry for action defined (shift/reduce conflict) The Grammar is not ambiguous, but the SLR is not powerful enough is multiply
5 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (5) The problem can be modeled as follow: Why SLR fails 1 The stack contains the string ; 2 The item set contains the item ; 3 Lookahead symbol is a and a FOLLOW Problem The state that is on top of the stack BUT reducing with cannot be followed by a in a rightsentential form is such
6 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (6) Why SLR fails: An Example * id id * * =! " If we are in state = then the stack contains with lookahead on top If we reduce with then the sack is can only reduce with In state with we with lookahead $, then the parsing fails since action error id
7 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (7) Summary Inadequacy of SLR Parsing LR(1) Parsing Closure and Goto Operations, Canonical Collection LR(1) Parsing Tables LALR(1) Parsing Conflicts in LALR Parsers LALR(1) Parsing Tables LALR Vs LR Parsers Dealing with Ambiguous Grammars
8 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (8) Towards LR Parsing: A Solution to SLR failure Add more information in the state to rule out invalid reductions Each state of an LR Parser indicates what symbol can follow a handle for which there is a possible reduction Definition An LR(1) Item is then a pair is called the Lookahead of the Item Furthermore,, where a FOLLOW
9 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (9) LR(1) Items describes a context of the parser: We are trying to find an We have followed by an a, and already on top of the stack Thus we need to see next a prefix derived from a The lookahead has no effect in items of the form BUT, with, On items input symbol is a, the parser reduces with only if the next
10 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (10) Summary Inadequacy of SLR Parsing LR(1) Parsing Closure and Goto Operations, Canonical Collection LR(1) Parsing Tables LALR(1) Parsing Conflicts in LALR Parsers LALR(1) Parsing Tables LALR Vs LR Parsers Dealing with Ambiguous Grammars
11 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (11) The Closure Operation To construct the Collection of sets of Items we proceed as in the construction of the LR(0) Canonical Collection but with new closure and goto operations Algorithm Closure(I) Let a set of LR(1) items for an augmented Grammar, then closure is a set of items such that: 1 Initially every item in is added to clusure(i); 2 If closure(i) and to closure(i), where more items can be added to closure(i) FIRST, then we add the item Go to step 1 until no Intuition Considering the addition of the productions involving must take into account the context of the parser
12 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (12) The Goto Operation and LR(1) Item Collection Definition If is a set of items and is a grammar symbol, then, goto is the closure of the set of all items is in such that LR(1) Item Collection We proceed as in the construction of the LR(0) Canonical Collection but with new closure and goto operations The initial condition is: closure
13 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (13) LR(1) Item Collection: An Example Example Build the LR(1) Item Collection for the following augmented Grammar:
14 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (14) Summary Inadequacy of SLR Parsing LR(1) Parsing Closure and Goto Operations, Canonical Collection LR(1) Parsing Tables LALR(1) Parsing Conflicts in LALR Parsers LALR(1) Parsing Tables LALR Vs LR Parsers Dealing with Ambiguous Grammars
15 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (15) LR(1) Parsing Table Algorithm LR(1) Parsing Tables Action and Goto 1 Construct grammar, the LR(1) item collection for the augmented 2 To each item set action action action we create a new state = shift = reduce, if = accept, if, for all Then the action table is:, and goto Here 3 goto, if goto 4 All the entries not defined by rules (2) and (3) are made error 5 The initial state is the one constructed from the closure of
16 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (16) Canonical LR(1) Parsers The parsing table produced by the above algorithm is called the Canonical LR(1) Parsing Table; A parser using this table is called a Canonica LR(1) Parser; If the table has no multiplydefined entries then the Grammar is called an LR(1) Grammar
17 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (17) Summary Inadequacy of SLR Parsing LR(1) Parsing Closure and Goto Operations, Canonical Collection LR(1) Parsing Tables LALR(1) Parsing Conflicts in LALR Parsers LALR(1) Parsing Tables LALR Vs LR Parsers Dealing with Ambiguous Grammars
18 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (18) LALR Parsers LALR stands for LookAheadLR Parser Is often used in practice since the parsing tables are considerably smaller than the canonical LR tables (SLR and LALR) Vs LR The comparison is in term of size For a language like Pascal we go from hundreds of states to thousand of states Main Idea: Merge the states (Item sets) whose items differ only in the lookahead We say that such states have the same core
19 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (19) LALR Parsers: An Example Example The Grammar: Has items,, and with the same core we obtain: with the same core By merging items Consider the merged state The LALR parser will reduce d to on any input, even in situations where the original LR parser would generate an error! In any case, the LALR parser will communicate the same error but at a later stage
20 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (20) Summary Inadequacy of SLR Parsing LR(1) Parsing Closure and Goto Operations, Canonical Collection LR(1) Parsing Tables LALR(1) Parsing Conflicts in LALR Parsers LALR(1) Parsing Tables LALR Vs LR Parsers Dealing with Ambiguous Grammars
21 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (21) Introducing Conflicts by Merging States Starting from an LR(1) Grammar and merging states with the same core we can only introduce reduce/reduce conflicts We show that no shift/reduce conflicts are present Suppose there is a merged state with a shift/reduce conflict on input a: Then, there is an LR(1) state such that: (for some c) And presents a shift/reduce conflict on input a Absurd
22 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (22) Introducing Conflict by Merging States (Cont) We show how it is possible to introduce reduce/reduce conflicts Consider the following LR states: Neither of these sets generates a conflict, however, their merge: generates a reduce/reduce conflict on both input d and e Note In case of a reduce/reduce conflict the Grammar is no more LALR Thus, LALR is a simpler Grammar then an LR Grammar
23 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (23) Summary Inadequacy of SLR Parsing LR(1) Parsing Closure and Goto Operations, Canonical Collection LR(1) Parsing Tables LALR(1) Parsing Conflicts in LALR Parsers LALR(1) Parsing Tables LALR Vs LR Parsers Dealing with Ambiguous Grammars
24 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (24) LALR Parsing Table The algorithm we present serves primarily for defining LALR Grammars there are more efficient techniques Algorithm LALR(1) Parsing Tables Action and Goto 1 Construct grammar, the LR(1) item collection for the augmented 2 Find all LR(1) states having the same core and merge them Let the resulting item collection 3 To each item set we create a new state constructed as in case of LR(1) parsers Then the action table is 4 The goto function is constructed as follows If cores of goto goto are all the same Let sets of item having the same core as goto, then goto, then the the union of all
25 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (25) LALR Parsing Table (Cont) If there is a reduce/reduce conflict in the above construction the Grammar is not LALR(1); The parsing table produced by the above algorithm is called the LALR Parsing Table; A parser using this table is called an LALR(1) Parser;
26 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (26) LALR Parser: An Example Example The Grammar: Has the following LALR(1) Item Collection: where: The LALR Parsing Table is: c d $ 0 s36 s acc 2 s36 s s36 s r3 r3 r3 5 r1 89 r2 r2 r2,
27 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (27) Summary Inadequacy of SLR Parsing LR(1) Parsing Closure and Goto Operations, Canonical Collection LR(1) Parsing Tables LALR(1) Parsing Conflicts in LALR Parsers LALR(1) Parsing Tables LALR Vs LR Parsers Dealing with Ambiguous Grammars
28 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (28) Behavior of LR Vs LALR On correct input both parsers make the same sequence of shift and reduce, only the state s names change On erroneous input the LALR parser make more moves (reductions) than the LR parser
29 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (29) Behavior of LR Vs LALR: An Example Example Consider as input the string ccd The LR parser will push onto the stack: Ending with an error in state and current input $ The LALR parser will push onto the stack: State State with input $ reduces with with input $ reduces with A similar reduction applies and the stack is: Ending finally with an error in state and the stack is: and the stack is: and current input $
30 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (30) Summary of Lecture IV Part 4 Inadequacy of SLR Parsing LR(1) Parsing Closure and Goto Operations, Canonical Collection LR(1) Parsing Tables LALR(1) Parsing Conflicts in LALR Parsers LALR(1) Parsing Tables LALR Vs LR Parsers Dealing with Ambiguous Grammars
31 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (31) Ambiguous Grammars Ambiguous Grammars provide a shorter and more natural specification This is reflected by a parser requiring a minor number of states Ambiguous Grammars are not LR: A parsing table will have multiplydefined entries; A parsing table will contain shift/shift and shift/reduce conflicts To deal with ambiguous grammars we use disambiguating rules that eliminate all the conflicts allowing for only one ParseTree
32 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (32) Precedence and Associativity Rules The following ambiguous Grammar for arithmetic expressions does not specify the associativity and the precedence of : The unambiguous Grammar: Enforces precedence of over +, and leftassociativity of + and Note The parser for the unambiguous Grammar is less efficient: The time spent for reducing the single productions and has the sole function to enforce associativity and precedence
33 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (33) Precedence and Associativity Rules (Cont) Let us consider the following LR(0) items for the ambiguous Grammar (see the Book for the complete set of states): FOLLOW State generates a conflict between reduce with with input + and, and shift State generates a conflict between reduce with with input + and, and shift Both Conflicts can be solved using the Precedence and Associativity for + and
34 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (34) Precedence and Associativity Rules (Cont) Consider the input id + id id and the following configuration: STACK INPUT Assuming that stack instead of reducing with takes precedence over +, the parser should shift onto the Consider the input id + id + id and the following configuration: STACK INPUT Assuming that + is leftassociative, the parser should reduce with instead of shift onto the stack
35 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (35) Precedence and Associativity Rules (Cont) Summary Precedence and Associativity uniquely determine the actions of the parser eliminating the ambiguity: 1 LeftAssociativity of : action reduce with 2 LeftAssociativity of : action reduce with 3 Precedence of over : action shift 4 Precedence of over : action reduce with
36 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (36) DanglingElse Ambiguity Consider the following Grammar for IFTHENELSE statements: Where, other productions stands for if expr then, stands for else, and stands for all Let us consider the following LR(0) item for this ambiguous Grammar (see the Book for the complete set of states): FOLLOW Assuming that the stack contains: if expr then stmt ( top, and the next input is else there is a shift/reduce conflict ) with state on Solution else must match the last if: action shift else
37 Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (37) Summary of Lecture IV Part 4 Inadequacy of SLR Parsing LR(1) Parsing Closure and Goto Operations, Canonical Collection LR(1) Parsing Tables LALR(1) Parsing Conflicts in LALR Parsers LALR(1) Parsing Tables LALR Vs LR Parsers Dealing with Ambiguous Grammars
Formal Languages and Compilers Lecture VII Part 4: Syntactic A
Formal Languages and Compilers Lecture VII Part 4: Syntactic Analysis Free University of BozenBolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/
More informationFormal Languages and Compilers Lecture VII Part 3: Syntactic A
Formal Languages and Compilers Lecture VII Part 3: Syntactic Analysis Free University of BozenBolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/
More informationS 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 informationLALR 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 informationLet 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 informationLexical and Syntax Analysis. BottomUp Parsing
Lexical and Syntax Analysis BottomUp Parsing Parsing There are two ways to construct derivation of a grammar. TopDown: begin with start symbol; repeatedly replace an instance of a production s LHS with
More informationMODULE 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 informationLR Parsing Techniques
LR Parsing Techniques Introduction BottomUp Parsing LR Parsing as Handle Pruning ShiftReduce Parser LR(k) Parsing Model Parsing Table Construction: SLR, LR, LALR 1 BottomUP Parsing A bottomup parser
More informationContextfree grammars
Contextfree grammars Section 4.2 Formal way of specifying rules about the structure/syntax of a program terminals  tokens nonterminals  represent higherlevel structures of a program start symbol,
More informationConflicts 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 informationCompiler Construction: Parsing
Compiler Construction: Parsing Mandar Mitra Indian Statistical Institute M. Mitra (ISI) Parsing 1 / 33 Contextfree grammars. Reference: Section 4.2 Formal way of specifying rules about the structure/syntax
More informationFormal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr
Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Grammars Free University of BozenBolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/
More informationParsing Wrapup. Roadmap (Where are we?) Last lecture Shiftreduce parser LR(1) parsing. This lecture LR(1) parsing
Parsing Wrapup Roadmap (Where are we?) Last lecture Shiftreduce parser LR(1) parsing LR(1) items Computing closure Computing goto LR(1) canonical collection This lecture LR(1) parsing Building ACTION
More informationPrinciples 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 informationLR Parsing Techniques
LR Parsing Techniques BottomUp Parsing  LR: a special form of BU Parser LR Parsing as Handle Pruning ShiftReduce Parser (LR Implementation) LR(k) Parsing Model  k lookaheads to determine next action
More information3. 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 informationEDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:
EDAN65: Compilers, Lecture 06 A LR parsing Görel Hedin Revised: 20170911 This lecture Regular expressions Contextfree grammar Attribute grammar Lexical analyzer (scanner) Syntactic analyzer (parser)
More informationUNIT 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 informationBottomup parsing. BottomUp Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form
Bottomup parsing Bottomup 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 informationParsing. 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 informationBottomUp Parsing. Lecture 1112
BottomUp Parsing Lecture 1112 (From slides by G. Necula & R. Bodik) 9/22/06 Prof. Hilfinger CS164 Lecture 11 1 BottomUp Parsing Bottomup parsing is more general than topdown parsing And just as efficient
More informationA leftsentential form is a sentential form that occurs in the leftmost derivation of some sentence.
Bottomup 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 leftsentential form is a sentential form that occurs
More informationDownloaded 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 informationCSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D1
CSE 401 Compilers LR Parsing Hal Perkins Autumn 2011 10/10/2011 200211 Hal Perkins & UW CSE D1 Agenda LR Parsing Tabledriven Parsers Parser States ShiftReduce and ReduceReduce conflicts 10/10/2011
More informationLecture 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 informationLR Parsing LALR Parser Generators
LR Parsing LALR Parser Generators Outline Review of bottomup parsing Computing the parsing DFA Using parser generators 2 Bottomup Parsing (Review) A bottomup parser rewrites the input string to the
More informationIn 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 informationLR Parsing  Conflicts
LR Parsing  Conflicts Lecture 15 Sections 4.5, 4.6 Robb T. Koether HampdenSydney College Fri, Feb 20, 2015 Robb T. Koether (HampdenSydney College) LR Parsing  Conflicts Fri, Feb 20, 2015 1 / 15 1 Shift/Reduce
More informationMIT 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 informationCSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D1
CSE P 501 Compilers LR Parsing Hal Perkins Spring 2018 UW CSE P 501 Spring 2018 D1 Agenda LR Parsing Tabledriven Parsers Parser States ShiftReduce and ReduceReduce conflicts UW CSE P 501 Spring 2018
More informationWWW.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 informationAction Table for CSXLite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSXLite
LALR r Driver Action Table for CSXLite 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 informationTabledriven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and nonterminals.
Bottomup Parsing: Tabledriven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and nonterminals. Basic operation is to shift terminals from the input to the
More informationLR Parsing LALR Parser Generators
Outline LR Parsing LALR Parser Generators Review of bottomup parsing Computing the parsing DFA Using parser generators 2 Bottomup Parsing (Review) A bottomup parser rewrites the input string to the
More informationSimple LR (SLR) LR(0) Drawbacks LR(1) SLR Parse. LR(1) Start State and Reduce. LR(1) Items 10/3/2012
LR(0) Drawbacks Consider the unambiguous augmented grammar: 0.) S E $ 1.) E T + E 2.) E T 3.) T x If we build the LR(0) DFA table, we find that there is a shiftreduce conflict. This arises because the
More informationLR Parsers. Aditi Raste, CCOEW
LR Parsers Aditi Raste, CCOEW 1 LR Parsers Most powerful shiftreduce parsers and yet efficient. LR(k) parsing L : left to right scanning of input R : constructing rightmost derivation in reverse k : number
More informationPrinciple of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale
Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (1 Principle of Compilers Lecture VIII: Intermediate Code Generation Alessandro Artale Faculty of Computer Science Free
More informationSYNTAX 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 informationBottomUp Parsing. Parser Generation. LR Parsing. Constructing LR Parser
Parser Generation Main Problem: given a grammar G, how to build a topdown parser or a bottomup parser for it? parser : a program that, given a sentence, reconstructs a derivation for that sentence 
More informationLecture 8: Deterministic BottomUp Parsing
Lecture 8: Deterministic BottomUp 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 informationBottomUp Parsing LR Parsing
BottomUp 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 informationMonday, 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 nonterminals S is the start symbol P is the set of productions
More informationLecture 7: Deterministic BottomUp Parsing
Lecture 7: Deterministic BottomUp 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 informationParsers. 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 informationBottom 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 informationshiftreduce parsing
Parsing #2 Bottomup 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 bottomup
More informationCompiler Construction 2016/2017 Syntax Analysis
Compiler Construction 2016/2017 Syntax Analysis Peter Thiemann November 2, 2016 Outline 1 Syntax Analysis Recursive topdown parsing Nonrecursive topdown parsing Bottomup parsing Syntax Analysis tokens
More informationWednesday, 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 informationWednesday, 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 informationParsers. 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 informationBottomUp Parsing. Lecture 1112
BottomUp Parsing Lecture 1112 (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 informationCompilers. Bottomup Parsing. (original slides by Sam
Compilers Bottomup Parsing Yannis Smaragdakis U Athens Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) BottomUp Parsing More general than topdown parsing And just as efficient Builds
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 informationParser Generation. BottomUp Parsing. Constructing LR Parser. LR Parsing. Construct parse tree bottomup  from leaves to the root
Parser Generation Main Problem: given a grammar G, how to build a topdown parser or a bottomup parser for it? parser : a program that, given a sentence, reconstructs a derivation for that sentence 
More informationCompiler Design 1. BottomUP Parsing. Goutam Biswas. Lect 6
Compiler Design 1 BottomUP 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 informationReview: ShiftReduce Parsing. Bottomup parsing uses two actions: BottomUp Parsing II. Shift ABC xyz ABCx yz. Lecture 8. Reduce Cbxy ijk CbA ijk
Review: ShiftReduce Parsing Bottomup parsing uses two actions: BottomUp 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 informationCS453 : JavaCUP and error recovery. CS453 Shiftreduce Parsing 1
CS453 : JavaCUP and error recovery CS453 Shiftreduce Parsing 1 Shiftreduce parsing in an LR parser LR(k) parser Lefttoright parse Rightmost derivation Ktoken look ahead LR parsing algorithm using
More informationPART 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 informationLR Parsing. Leftmost and Rightmost Derivations. Compiler Design CSE 504. Derivations for id + id: T id = id+id. 1 ShiftReduce Parsing.
LR Parsing Compiler Design CSE 504 1 ShiftReduce 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 informationLecture Notes on BottomUp LR Parsing
Lecture Notes on BottomUp LR Parsing 15411: Compiler Design Frank Pfenning Lecture 9 1 Introduction In this lecture we discuss a second parsing algorithm that traverses the input string from left to
More informationLR Parsing. Table Construction
#1 LR Parsing Table Construction #2 Outline Review of bottomup parsing Computing the parsing DFA Closures, LR(1) Items, States Transitions Using parser generators Handling Conflicts #3 In One Slide An
More informationBottomUp Parsing II. Lecture 8
BottomUp Parsing II Lecture 8 1 Review: ShiftReduce Parsing Bottomup 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 informationLecture Notes on BottomUp LR Parsing
Lecture Notes on BottomUp LR Parsing 15411: Compiler Design Frank Pfenning Lecture 9 September 23, 2009 1 Introduction In this lecture we discuss a second parsing algorithm that traverses the input string
More informationSyntax 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 informationLALR stands for look ahead left right. It is a technique for deciding when reductions have to be made in shift/reduce parsing. Often, it can make the
LALR parsing 1 LALR stands for look ahead left right. It is a technique for deciding when reductions have to be made in shift/reduce parsing. Often, it can make the decisions without using a look ahead.
More informationCS143 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 informationCS 314 Principles of Programming Languages
CS 314 Principles of Programming Languages Lecture 5: Syntax Analysis (Parsing) Zheng (Eddy) Zhang Rutgers University January 31, 2018 Class Information Homework 1 is being graded now. The sample solution
More informationCS308 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 informationLR(0) Parsing Summary. LR(0) Parsing Table. LR(0) Limitations. A NonLR(0) Grammar. LR(0) Parsing Table CS412/CS413
LR(0) Parsing ummary C412/C41 Introduction to Compilers Tim Teitelbaum Lecture 10: LR Parsing February 12, 2007 LR(0) item = a production with a dot in RH LR(0) state = set of LR(0) items valid for viable
More informationHow do LL(1) Parsers Build Syntax Trees?
How do LL(1) Parsers Build Syntax Trees? So far our LL(1) parser has acted like a recognizer. It verifies that input token are syntactically correct, but it produces no output. Building complete (concrete)
More informationSection 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 informationCOMP421 Compiler Design. Presented by Dr Ioanna Dionysiou
COMP421 Compiler Design Presented by Dr Ioanna Dionysiou Administrative! Any questions about the syllabus?! Course Material available at www.cs.unic.ac.cy/ioanna! Next time reading assignment [ALSU07]
More informationBottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as ShiftReduce parsing
Also known as ShiftReduce 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 informationCompilation 2013 Parser Generators, Conflict Management, and MLYacc
Compilation 2013 Parser Generators, Conflict Management, and MLYacc Erik Ernst Aarhus University Parser generators, MLYacc LR parsers are tedious to write, but can be generated, e.g., by MLYacc Input:
More informationBottomUp Parsing II (Different types of ShiftReduce Conflicts) Lecture 10. Prof. Aiken (Modified by Professor Vijay Ganesh.
BottomUp Parsing II Different types of ShiftReduce Conflicts) Lecture 10 Ganesh. Lecture 10) 1 Review: BottomUp Parsing Bottomup parsing is more general than topdown parsing And just as efficient Doesn
More informationBuilding a Parser Part III
COMP 506 Rice University Spring 2018 Building a Parser Part III With Practical Application To Lab One source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda
More informationConfiguration Sets for CSX Lite. Parser Action Table
Configuration Sets for CSX Lite State s 6 s 7 Cofiguration Set Prog { Stmts } Eof Stmts Stmt Stmts State s s Cofiguration Set Prog { Stmts } Eof Prog { Stmts } Eof Stmts Stmt Stmts Stmts λ Stmt if ( Expr
More informationVIVA QUESTIONS WITH ANSWERS
VIVA QUESTIONS WITH ANSWERS 1. What is a compiler? A compiler is a program that reads a program written in one language the source language and translates it into an equivalent program in another languagethe
More informationError Detection in LALR Parsers. LALR is More Powerful. { b + c = a; } Eof. Expr Expr + id Expr id we can first match an id:
Error Detection in LALR Parsers In bottomup, LALR parsers syntax errors are discovered when a blank (error) entry is fetched from the parser action table. Let s again trace how the following illegal CSXlite
More informationCS415 Compilers. LR Parsing & Error Recovery
CS415 Compilers LR Parsing & Error Recovery These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Review: LR(k) items The LR(1) table construction
More informationCOMPILER (CSE 4120) (Lecture 6: Parsing 4 Bottomup Parsing )
COMPILR (CS 4120) (Lecture 6: Parsing 4 Bottomup Parsing ) Sungwon Jung Mobile Computing & Data ngineering Lab Dept. of Computer Science and ngineering Sogang University Seoul, Korea Tel: +8227058930
More informationLR Parsing, Part 2. Constructing Parse Tables. An NFA Recognizing Viable Prefixes. Computing the Closure. GOTO Function and DFA States
TDDD16 Compilers and Interpreters TDDB44 Compiler Construction LR Parsing, Part 2 Constructing Parse Tables Parse table construction Grammar conflict handling Categories of LR Grammars and Parsers An NFA
More informationMore BottomUp Parsing
More BottomUp 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 informationCOP4020 Programming Languages. Syntax Prof. Robert van Engelen
COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview n Tokens and regular expressions n Syntax and contextfree grammars n Grammar derivations n More about parse trees n Topdown and
More informationThe analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.
COMPILER DESIGN 1. What is a compiler? A compiler is a program that reads a program written in one language the source language and translates it into an equivalent program in another languagethe target
More informationSLR parsers. LR(0) items
SLR parsers LR(0) items As we have seen, in order to make shiftreduce parsing practical, we need a reasonable way to identify viable prefixes (and so, possible handles). Up to now, it has not been clear
More informationCompilation 2012 ContextFree Languages Parsers and Scanners. Jan Midtgaard Michael I. Schwartzbach Aarhus University
Compilation 2012 Parsers and Scanners Jan Midtgaard Michael I. Schwartzbach Aarhus University ContextFree Grammars Example: sentence subject verb object subject person person John Joe Zacharias verb asked
More informationParsing 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 informationG53CMP: Lecture 4. Syntactic Analysis: Parser Generators. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 4 p.1/32
G53CMP: Lecture 4 Syntactic Analysis: Parser Generators Henrik Nilsson University of Nottingham, UK G53CMP: Lecture 4 p.1/32 This Lecture Parser generators ( compiler compilers ) The parser generator Happy
More informationSYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram
CS6660 COMPILER DESIGN Question Bank UNIT IINTRODUCTION TO COMPILERS 1. Define compiler. 2. Differentiate compiler and interpreter. 3. What is a language processing system? 4. List four software tools
More informationAlgorithms for NLP. LR Parsing. Reading: Hopcroft and Ullman, Intro. to Automata Theory, Lang. and Comp. Section , pp.
11711 Algorithms for NL L arsing eading: Hopcroft and Ullman, Intro. to Automata Theory, Lang. and Comp. Section 10.610.7, pp. 248 256 Shifteduce arsing A class of parsers with the following principles:
More informationUNITIII BOTTOMUP PARSING
UNITIII BOTTOMUP PARSING Constructing a parse tree for an input string beginning at the leaves and going towards the root is called bottomup parsing. A general type of bottomup parser is a shiftreduce
More informationDEPARTMENT 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 informationPrinciples of Programming Languages
Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp 15/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 10! LR parsing with ambiguous grammars Error recovery
More informationSyntax Analyzer  Parser
Syntax Analyzer  Parser ASU Textbook Chapter 4.24.9 (w/o error handling) Tsansheng Hsu tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu 1 A program represented by a sequence of tokens
More informationUNIVERSITY OF CALIFORNIA
UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division CS164 Fall 1997 P. N. Hilfinger CS 164: Midterm Name: Please do not discuss the contents of
More informationConcepts Introduced in Chapter 4
Concepts Introduced in Chapter 4 Grammars ContextFree Grammars Derivations and Parse Trees Ambiguity, Precedence, and Associativity Top Down Parsing Recursive Descent, LL Bottom Up Parsing SLR, LR, LALR
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 March 20, 2007 Outline Recap LR(0)
More informationSyntax Analysis, VII One more LR(1) example, plus some more stuff. Comp 412 COMP 412 FALL Chapter 3 in EaC2e. target code.
COMP 412 FALL 2017 Syntax Analysis, VII One more LR(1) example, plus some more stuff Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2017, Keith D. Cooper & Linda Torczon,
More informationLecture BottomUp Parsing
Lecture 14+15 BottomUp 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