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

Similar documents
Configuration Sets for CSX- Lite. Parser Action Table

Error Detection in LALR Parsers. LALR is More Powerful. { b + c = a; } Eof. Expr Expr + id Expr id we can first match an id:

CSX-lite Example. LL(1) Parse Tables. LL(1) Parser Driver. Example of LL(1) Parsing. An LL(1) parse table, T, is a twodimensional

Table-Driven Top-Down Parsers

How do LL(1) Parsers Build Syntax Trees?

LL(1) Grammars. Example. Recursive Descent Parsers. S A a {b,d,a} A B D {b, d, a} B b { b } B λ {d, a} D d { d } D λ { a }

Building A Recursive Descent Parser. Example: CSX-Lite. match terminals, and calling parsing procedures to match nonterminals.

UNIVERSITY OF CALIFORNIA

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

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

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

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

A Bison Manual. You build a text file of the production (format in the next section); traditionally this file ends in.y, although bison doesn t care.

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

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

Properties of Regular Expressions and Finite Automata

Optimizing Finite Automata

UNIT III & IV. Bottom up parsing

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

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

Bottom-Up Parsing. Lecture 11-12

Wednesday, September 9, 15. Parsers

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

Bottom-Up Parsing. Lecture 11-12

Lecture 7: Deterministic Bottom-Up Parsing

Context-free grammars

CS415 Compilers. LR Parsing & Error Recovery

Lecture 8: Deterministic Bottom-Up Parsing

Compiler Construction: Parsing

Wednesday, August 31, Parsers

Parsing Techniques. CS152. Chris Pollett. Sep. 24, 2008.

Monday, September 13, Parsers

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

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

Conflicts in LR Parsing and More LR Parsing Types

shift-reduce parsing

Building a Parser Part III

LR Parsing Techniques

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

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

Syntax-Directed Translation

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

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

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

Part III : Parsing. From Regular to Context-Free Grammars. Deriving a Parser from a Context-Free Grammar. Scanners and Parsers.

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

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

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

Lexical and Syntax Analysis (2)

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

LALR Parsing. What Yacc and most compilers employ.

4. Lexical and Syntax Analysis

Downloaded from Page 1. LR Parsing

LR Parsing LALR Parser Generators

CS 314 Principles of Programming Languages

CSE 582 Autumn 2002 Exam Sample Solution

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

In One Slide. Outline. LR Parsing. Table Construction

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

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

Class Information ANNOUCEMENTS

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

a. Determine the EPS, FIRST and FOLLOWs set for this grammar.


4. Lexical and Syntax Analysis

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

Lexical and Syntax Analysis. Bottom-Up Parsing

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

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

Context-Free Grammars

Compilers and Language Processing Tools


A clarification on terminology: Recognizer: accepts or rejects strings in a language. Parser: recognizes and generates parse trees (imminent topic)

LR Parsing - Conflicts

Chapter 4. Lexical and Syntax Analysis

LR Parsing LALR Parser Generators

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

Programming Languages (CS 550) Lecture 4 Summary Scanner and Parser Generators. Jeremy R. Johnson

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

CA Compiler Construction

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

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

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

CS 4120 Introduction to Compilers

CSE431 Translation of Computer Languages

CSE 582 Autumn 2002 Exam 11/26/02

Syntax Analysis, VI Examples from LR Parsing. Comp 412

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

Note that for recursive descent to work, if A ::= B1 B2 is a grammar rule we need First k (B1) disjoint from First k (B2).

CS 164 Handout 11. Midterm Examination. There are seven questions on the exam, each worth between 10 and 20 points.

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

Compiler Construction 2016/2017 Syntax Analysis

Chapter 4: LR Parsing

Syntax Analysis, VII One more LR(1) example, plus some more stuff. Comp 412 COMP 412 FALL Chapter 3 in EaC2e. target code.

LR Parsing. Table Construction

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

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

MODULE 14 SLR PARSER LR(0) ITEMS

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

Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.

Transcription:

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){ //Let S = Top state on parse stack //Let CT = current token to match switch (A[S][CT]) { case error: SyntaxError(CT);return; case accept: return; case shift: push(goto[s][ct]); CT= Scanner(); break; case reduce i: //Let prod i = A Y...Y m pop m states; //Let S = new top state push(goto[s ][A]); break; } } } ( S ) R S R R id S S S S R S S S = S + S R S R R - S R S R R ; S R R R R5 eof A CS 5 Fall 9 CS 5 Fall 9 GoTo Table for CSX-Lite Example of LALR() Parsing 5 9 { } 5 9 We ll again parse { a = b + c; } Eof We start by pushing state on the parse stack. if 5 5 5 ( 9 ) Prog { Stmts } Eof Shift {a=b + c; } Eof id 9 = + 5 5 - ; eof Prog { Stmts } Eof Stmts Stmt Stmts Stmts λ Shift a = b + c; } Eof Stmt id = Expr ; = b + c; } Eof stmts stmt expr Stmt id = Expr ; Expr Expr + id Expr Expr - id Expr id Shift b + c; } Eof CS 5 Fall 9 CS 5 Fall 99

5 Expr id Reduce + c; } Eof Stmt id = Expr ; Expr Expr - id Shift + c; } Eof Expr Expr + id Shift c; } Eof 5 Expr Expr + id Reduce ; } Eof Stmt id = Expr ; Expr Expr - id Shift ; } Eof Stmt id = Expr ; Reduce } Eof CS 5 Fall CS 5 Fall Stmts Stmt Stmts Reduce } Eof Stmts Stmt Stmts Stmts λ Stmt Stmts Stmt Stmts Reduce } Eof Prog { Stmts } Eof Shift } Eof Prog { Stmts } Eof Accept Eof Error Detection in LALR rs 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 program is parsed: { b + c = a; } Eof CS 5 Fall CS 5 Fall

LALR is More Powerful Prog { Stmts } Eof Shift {b+c=a;}eof Prog { Stmts } Eof Stmts Stmt Stmts Stmts λ Stmt id = Expr ; Shift Error (blank) b + c = a; } Eof +c=a;}eof Essentially all LL() grammars are LALR() plus many more. Grammar constructs that confuse LL() are readily handled. Common prefixes are no problem. Since sets of configurations are tracked, more than one prefix can be followed. For example, in Stmt id = Expr ; Stmt id ( Args ) ; after we match an id we have Stmt id = Expr ; Stmt id ( Args ) ; The next token will tell us which production to use. CS 5 Fall CS 5 Fall 5 Left recursion is also not a problem. Since sets of configurations are tracked, we can follow a left-recursive production and all others it might use. For example, in Expr Expr + id Expr id we can first match an id: But ambiguity will still block construction of an LALR parser. Some shift/reduce or reduce/ reduce conflict must appear. (Since two or more distinct parses are possible for some input). Consider our original productions for if-then and if-then-else statements: Stmt if ( Expr ) Stmt Expr id Stmt if ( Expr ) Stmt else Stmt Then the Expr is recognized: Since else can follow Stmt, we have an unresolvable shift/reduce conflict. The left-recursion is handled! CS 5 Fall CS 5 Fall

Grammar Engineering Though LALR grammars are very general and inclusive, sometimes a reasonable set of productions is rejected due to shift/reduce or reduce/reduce conflicts. In such cases, the grammar may need to be engineered to allow the parser to operate. A good example of this is the definition of MemberDecls in CSX. A straightforward definition is MemberDecls FieldDecls MethodDecls FieldDecls FieldDecl FieldDecls FieldDecls λ MethodDecls MethodDecl MethodDecls MethodDecls λ FieldDecl int id ; MethodDecl int id ( ) ; Body When we predict MemberDecls we get: MemberDecls FieldDecls MethodDecls FieldDecls FieldDecl FieldDecls FieldDecls λ FieldDecl int id ; Now int follows FieldDecls since MethodDecls + int... Thus an unresolvable shift/reduce conflict exists. The problem is that int is derivable from both FieldDecls and MethodDecls, so when we see an int, we can t tell which way to parse it (and FieldDecls λ requires we make an immediate decision!). CS 5 Fall CS 5 Fall 9 If we rewrite the grammar so that we can delay deciding from where the int was generated, a valid LALR parser can be built: MemberDecls FieldDecl MemberDecls MemberDecls MethodDecls MethodDecls MethodDecl MethodDecls MethodDecls λ FieldDecl int id ; MethodDecl int id ( ) ; Body When MemberDecls is predicted we have Now Follow(MethodDecls) = Follow(MemberDecls) = }, so we have no shift/reduce conflict. After int id is matched, the next token (a ; or a ( ) will tell us whether a FieldDecl or a MethodDecl is being matched. MemberDecls FieldDecl MemberDecls MemberDecls MethodDecls MethodDecls MethodDecl MethodDecls MethodDecls λ FieldDecl int id ; MethodDecl int id ( ) ; Body CS 5 Fall CS 5 Fall

Properties of LL and LALR rs Each prediction or reduce action is guaranteed correct. Hence the entire parse (built from LL predictions or LALR reductions) must be correct. This follows from the fact that LL parsers allow only one valid prediction per step. Similarly, an LALR parser never skips a reduction if it is consistent with the current token (and all possible reductions are tracked). LL and LALR parsers detect an syntax error as soon as the first invalid token is seen. Neither parser can match an invalid program prefix. If a token is matched it must be part of a valid program prefix. In fact, the prediction made or the stacked configuration sets show a possible derivation of the token accepted so far. All LL and LALR grammars are unambiguous. LL predictions are always unique and LALR shift/reduce or reduce/reduce conflicts are disallowed. Hence only one valid derivation of any token sequence is possible. CS 5 Fall CS 5 Fall All LL and LALR parsers require only linear time and space (in terms of the number of tokens parsed). The parsers do only fixed work per node of the concrete parse tree, and the size of this tree is linear in terms of the number of leaves in it (even with λ-productions included!). CS 5 Fall