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

Size: px
Start display at page:

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

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 right-sentential 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 multiply-defined 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 LookAhead-LR 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 multiply-defined 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 Parse-Tree

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 left-associativity 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 left-associative, 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 Left-Associativity of : action reduce with 2 Left-Associativity 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) Dangling-Else Ambiguity Consider the following Grammar for IF-THEN-ELSE 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 Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

### 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/

### 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

### 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.

### 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

### 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

### 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

### 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

### 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,

### 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

### 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

### Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Grammars Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

### 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

### 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

### 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

### 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

### 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)

### 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.

### 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

### 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

### 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

### 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

### 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:

### 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

### 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],

### 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

### 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

### LR Parsing - Conflicts

LR Parsing - Conflicts Lecture 15 Sections 4.5, 4.6 Robb T. Koether Hampden-Sydney College Fri, Feb 20, 2015 Robb T. Koether (Hampden-Sydney College) LR Parsing - Conflicts Fri, Feb 20, 2015 1 / 15 1 Shift/Reduce

### 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

### 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

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.

### 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){

### 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

### 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

### Simple 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 shift-reduce conflict. This arises because the

### LR Parsers. Aditi Raste, CCOEW

LR Parsers Aditi Raste, CCOEW 1 LR Parsers Most powerful shift-reduce parsers and yet efficient. LR(k) parsing L : left to right scanning of input R : constructing rightmost derivation in reverse k : number

### Principle 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

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

### 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 ----

### 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

### 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

### 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

### 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

### 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

### 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

### 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

### 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

### 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

### 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

### 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

### 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

### 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

### EXAM. 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

### 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 ----

### 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,

### 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

### CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1

CS453 : JavaCUP and error recovery CS453 Shift-reduce Parsing 1 Shift-reduce parsing in an LR parser LR(k) parser Left-to-right parse Right-most derivation K-token look ahead LR parsing algorithm using

### 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

### 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

### Lecture Notes on Bottom-Up LR Parsing

Lecture Notes on Bottom-Up LR Parsing 15-411: 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

### LR Parsing. Table Construction

#1 LR Parsing Table Construction #2 Outline Review of bottom-up parsing Computing the parsing DFA Closures, LR(1) Items, States Transitions Using parser generators Handling Conflicts #3 In One Slide An

### 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

### Lecture Notes on Bottom-Up LR Parsing

Lecture Notes on Bottom-Up LR Parsing 15-411: 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

### 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

### 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

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.

### 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

### CS 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

### 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.

### LR(0) Parsing Summary. LR(0) Parsing Table. LR(0) Limitations. A Non-LR(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

### How 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)

### 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

### COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

COMP-421 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]

### 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

### Compilation 2013 Parser Generators, Conflict Management, and ML-Yacc

Compilation 2013 Parser Generators, Conflict Management, and ML-Yacc Erik Ernst Aarhus University Parser generators, ML-Yacc LR parsers are tedious to write, but can be generated, e.g., by ML-Yacc Input:

### 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

### Building 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

### Configuration 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

### VIVA 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 language-the

### Error 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 bottom-up, 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 CSX-lite

### CS415 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

### COMPILER (CSE 4120) (Lecture 6: Parsing 4 Bottom-up Parsing )

COMPILR (CS 4120) (Lecture 6: Parsing 4 Bottom-up Parsing ) Sungwon Jung Mobile Computing & Data ngineering Lab Dept. of Computer Science and ngineering Sogang University Seoul, Korea Tel: +82-2-705-8930

### LR 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 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

### COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview n Tokens and regular expressions n Syntax and context-free grammars n Grammar derivations n More about parse trees n Top-down and

### The 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 language-the target

### 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

### 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

### 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,

### G53CMP: 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

### SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

CS6660 COMPILER DESIGN Question Bank UNIT I-INTRODUCTION TO COMPILERS 1. Define compiler. 2. Differentiate compiler and interpreter. 3. What is a language processing system? 4. List four software tools

### Algorithms for NLP. LR Parsing. Reading: Hopcroft and Ullman, Intro. to Automata Theory, Lang. and Comp. Section , pp.

11-711 Algorithms for NL L arsing eading: Hopcroft and Ullman, Intro. to Automata Theory, Lang. and Comp. Section 10.6-10.7, pp. 248 256 Shift-educe arsing A class of parsers with the following principles:

### 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

### 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

### Principles 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

### Syntax Analyzer --- Parser

Syntax Analyzer --- Parser ASU Textbook Chapter 4.2--4.9 (w/o error handling) Tsan-sheng Hsu tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu 1 A program represented by a sequence of tokens

### UNIVERSITY 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

### Concepts Introduced in Chapter 4

Concepts Introduced in Chapter 4 Grammars Context-Free Grammars Derivations and Parse Trees Ambiguity, Precedence, and Associativity Top Down Parsing Recursive Descent, LL Bottom Up Parsing SLR, LR, LALR

### CSE302: 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)