Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Similar documents
Formal Languages and Compilers Lecture VII Part 4: Syntactic A

LR Parsers. Aditi Raste, CCOEW

Principles of Programming Languages

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

SLR parsers. LR(0) items

Compiler Construction: Parsing

Context-free grammars

UNIT-III BOTTOM-UP PARSING

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

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

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

LR Parsing - The Items

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

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

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

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

CS 4120 Introduction to Compilers

Syntax Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

MODULE 14 SLR PARSER LR(0) ITEMS

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

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

LR Parsing Techniques

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

Compiler Construction 2016/2017 Syntax Analysis

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.

Lexical and Syntax Analysis. Bottom-Up Parsing

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

Formal Languages and Compilers Lecture VI: Lexical Analysis


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

Lecture Bottom-Up Parsing

LR Parsing, Part 2. Constructing Parse Tables. An NFA Recognizing Viable Prefixes. Computing the Closure. GOTO Function and DFA States

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

Concepts Introduced in Chapter 4

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

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

UNIT III & IV. Bottom up parsing

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

Syntax Analysis Part I

Bottom-Up Parsing LR Parsing

Formal Languages and Compilers Lecture IV: Regular Languages and Finite. Finite Automata

4. Lexical and Syntax Analysis

4. Lexical and Syntax Analysis

Lecture 8: Deterministic Bottom-Up Parsing

Chapter 4: LR Parsing

Lecture 7: Deterministic Bottom-Up Parsing

Bottom-Up Parsing II. Lecture 8

LR Parsing Techniques


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

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

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

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

CSE302: Compiler Design

Top down vs. bottom up parsing

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

Syntax Analyzer --- Parser

Bottom-Up Parsing. Lecture 11-12

Bottom-Up Parsing. Lecture 11-12

LR(0) Parsing Summary. LR(0) Parsing Table. LR(0) Limitations. A Non-LR(0) Grammar. LR(0) Parsing Table CS412/CS413

How do LL(1) Parsers Build Syntax Trees?

CS 314 Principles of Programming Languages

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

8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing

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

Chapter 4. Lexical and Syntax Analysis

VIVA QUESTIONS WITH ANSWERS

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

2068 (I) Attempt all questions.

Exercises II. Exercise: Lexical Analysis

Outline CS412/413. Administrivia. Review. Grammars. Left vs. Right Recursion. More tips forll(1) grammars Bottom-up parsing LR(0) parser construction

Example CFG. Lectures 16 & 17 Bottom-Up Parsing. LL(1) Predictor Table Review. Stacks in LR Parsing 1. Sʹ " S. 2. S " AyB. 3. A " ab. 4.

shift-reduce parsing

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

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

Syntactic Analysis. Chapter 4. Compiler Construction Syntactic Analysis 1

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.

Simple LR (SLR) LR(0) Drawbacks LR(1) SLR Parse. LR(1) Start State and Reduce. LR(1) Items 10/3/2012

LR Parsing LALR Parser Generators

LR Parsing LALR Parser Generators

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

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

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

LR Parsing E T + E T 1 T

Wednesday, August 31, Parsers

Parsing. Rupesh Nasre. CS3300 Compiler Design IIT Madras July 2018

Downloaded from Page 1. LR Parsing

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

Syn S t yn a t x a Ana x lysi y s si 1

CSCI312 Principles of Programming Languages

Monday, September 13, Parsers

LALR Parsing. What Yacc and most compilers employ.

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

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

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

Wednesday, September 9, 15. Parsers

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

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

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

CS606- compiler instruction Solved MCQS From Midterm Papers

Transcription:

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/ artale/ Formal Languages and Compilers BSc course 2017/18 First Semester

Summary of Lecture VII Part 3 LR Parsing Algorithm: An Intro Automata and Bottom-up Parsing SLR Parsing Closure and Goto Operations, Canonical Collection; SLR Parsing Tables

Intro to LR Parsers LR(k) Grammars are the most general Non-Backtracking Grammars that can be used in bottom-up parsers. L : Left-to-right scanning of the input; R : Rightmost derivations; k : number of lookahead symbols to take a decision. Predictive Grammars, i.e., LL Grammars, are a proper subset of LR Grammars (e.g., if-then-else is not LL but it is LR). An LR parser can detect a syntactic error as soon as possible. Disadvantage. Is difficult to build an LR parser by hand. We need specialized tools like YACC.

LR Parser Architecture An LR parser has: An input buffer (Tokens returned from the Lexical Analyzer); A stack containing Grammar symbols and States; A parsing table with two parts, Action and Goto, implementing a DFA to decide between shift and reduce. Input id + id $ s m X m s m 1... LR Parser Program Output X 1 s 0 Stack Action Goto

LR Parsing Algorithm The stack stores a string of the form s 0 X 1 s 1... s m 1 X m s m, where: X i is a Grammar Symbol; s i is a state summarizing the information contained in the stack below it. The combination State on top of the stack, Lookahead symbol is used to index the Action-Goto table. Configuration of an LR Parser. Is a pair made by the content of the stack (s m on top) and the right-part of the input (starting with the Lookahead): s 0 X 1 s 1... s m 1 X m s m, a i a i+1... a n $

LR Parsing Algorithm (Cont.) The next move of the parser is based on the pair (s m, a i ) and on what specified in the Action table: 1 action[s m, a i ] = shift s j. The parser executes a shift entering the configuration: s 0 X 1 s 1... X m s m a i s j, a i+1... a n $. 2 action[s m, a i ] = reduce A β. The parser executes a reduce entering the configuration: s 0 X 1 s 1... X m r s m r As, a i a i+1... a n $ ; where s = goto(s m r, A) and r = β. The parser pops 2r symbols from the stack (r states and the r Grammar symbols β) and then pushes both A and s. The production A β is in the output. 3 action[s m, a i ] = error. 4 action[s m, $] = accept. The parser stops successfully.

Example: LR Parser on id*id+id Grammar r1. E E + T r2. E T r3. T T F r4. T F r5. F (E ) r6. F id

Summary Parsing Algorithm: An Intro Automata and Bottom-up Parsing SLR Parsing Closure and Goto Operations, Canonical Collection; SLR Parsing Tables

Automata and LR Parsing Definition 1. Right-Sentential Form: A string α derived from the scope of the language, S rm α, by means of right-most derivations. Definition 2. Handle: Substring of a right-sentential form that matches a right hand side of a production. The Handle will always appear on top of the stack, never inside.

Automata and LR Parsing (Cont.) The Action and Goto tables define the transition function of an Automaton that recognizes handles on top of the stack. The automaton does not need to read the stack every time: The state on top of the stack is the state the automaton would be after reading the symbols of the stack. This is why an LR parser has full control on the content of the stack just knowing the state on top of the stack.

Summary Parsing Algorithm: An Intro Automata and Bottom-up Parsing SLR Parsing Closure and Goto Operations, Canonical Collection; SLR Parsing Tables

SLR Parsers Simple LR (SLR) is the simplest LR parsing Grammar. Definition. An LR(0) Item of a Grammar, G, is a production with a dot at some position in the right side. Example. The production A XYZ gives rise to four items: A. XYZ A X. YZ A XY. Z A XYZ. The production A ϵ generates the item A. Intuition. An item indicates how much of a production we have seen in the parsing process, and can be represented by a pair of integers: Number of Production, Dot Position

Constructing SLR Parsing Tables Items are useful to build the transition function of the Automaton recognizing handles. Items representing the same situation are grouped together into sets. Each of these sets represents a state of the DFA recognizing handles. The Canonical Collection of LR(0) Items provides the basis to construct the SLR parsing tables (implementing the DFA). The canonical collection is defined in terms of two operations, Closure and Goto, and an Augmented Grammar, i.e., a Grammar with a new scope S and a new production S S. The production S S indicates acceptance, i.e., the parser accepts iff it is about to reduce by S S.

Summary Parsing Algorithm: An Intro Automata and Bottom-up Parsing SLR Parsing Closure and Goto Operations, Canonical Collection; SLR Parsing Tables

The Closure Operation Algorithm. Closure(I). If I is a set of items for an augmented Grammar G, then closure(i) is the set of items such that: 1 Initially every item in I is added to closure(i); 2 If A α. Bβ closure(i) and B γ, then we add the item B. γ to closure(i). Go to step 1 until no more items can be added to closure(i). Intuition. A α. Bβ closure(i) indicates that: 1 We expect to see something derivable from A, and 2 α is already on top of the stack, thus 3 we expect to see something derivable from Bβ, and then 4 if B γ we could also expect something derivable from γ.

The Closure Operation: An Example Example. Consider the augmented grammar on the left, then, closure({e. E}) contains the items shown on the right: Augmented Grammar E E E E + T E T T T F T F F (E) F id closure({e. E}) E. E E. E + T E. T T. T F T. F F. (E) F. id

The Goto Operation Definition. If I is a set of items and X V N V T, then, goto(i, X ) is the closure of the set of all items A αx. β such that A α. X β is in I. Intuition 1. goto(i, X ) represents the transition of the automaton from state I and input X. Intuition 2. If I is a set of items valid for a prefix α of a right-sentential form, then, goto(i, X ) is valid for the prefix αx.

The Goto Operation: An example Example. If I = {E E., E E. +T }, then: goto(i,+) = closure({e E+. T }), is the set: E E+. T T. T F T. F F. (E) F. id

Canonical Collection Algorithm. Canonical Collection for an Augmented Grammar G 1 Initially, C = {closure({s. S})}; 2 For each set of items I in C and each Grammar symbol X If goto(i, X ) and goto(i, X ) C, then add goto(i, X ) to C; 3 Go to step 2 if new items have been added, otherwise stop.

Example: Canonical Collection for Arithmetic Expressions I 0 : E. E E. E + T E. T T. T F T. F F. (E) F. id I 1 : E E. E E. +T I 2 : E T. T T. F I 3 : T F. I 4 : F (. E) E. E + T E. T T. T F T. F F. (E) F. id I 5 : F id. I 6 : E E+. T T. T F T. F F. (E) F. id I 7 : T T. F F F. (E). id I 8 : F (E. ) E E. +T I 9 : E E + T. T T. F I 10 : T T F. I 11 : F (E).

Example: Goto Function for Arithmetic Expressions The above figure represents the transition function of the DFA recognizing viable prefixes of the Grammar for Arithmetic Expressions. Viable Prefix: Prefix of right-sentential form that could be on top of the stack of an SLR Parser.

Summary LR Parsing Algorithm: An Intro Automata and Bottom-up Parsing SLR Parsing Closure and Goto Operations, Canonical Collection; SLR Parsing Tables

SLR Parsing Tables Algorithm. SLR Parsing Tables Action and Goto. 1 Construct C = {I 0, I 1,..., I n }, the canonical collection for the augmented grammar G. 2 To each item set I k we create a new state s k. Then the action table is: action[s k, a] = shift s j ", if A α. aβ I k, and goto(i k, a) = I j. action[s k, a] = reduce A α", for all A α. I k, and for all a in FOLLOW(A). Here A S. action[s k, $] = accept", if S S. I k. 3 goto[s k, A] = s j, if goto(i k, A) = I j. 4 All the entries not defined by rules (2) and (3) are made error. 5 The initial state, I 0, is the one constructed from the closure of S. S Note. The Parsing table does not contains multiple entries if and only if the Grammar is SLR.

Summary of Lecture VII Part 3 LR Parsing Algorithm: An Intro Automata and Bottom-up Parsing SLR Parsing Closure and Goto Operations, Canonical Collection; SLR Parsing Tables