Compiling Techniques

Similar documents
Compiling Techniques

Compiling Techniques

Administrivia. Test I during class on 10 March. Bottom-Up Parsing. Lecture An Introductory Example

Syntax Analysis - Part 1. Syntax Analysis

CA Compiler Construction

CONTEXT FREE GRAMMAR AND

Lecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS Berkley University 1

Bottom up parsing. General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing);

Syntactic Analysis. Top-Down Parsing

Parsing Algorithms. CS 4447/CS Stephen Watt University of Western Ontario

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

Syntax Analysis Part I. Position of a Parser in the Compiler Model. The Parser. Chapter 4

Syntax Analysis (Part 2)

Syntax Analysis Part I

Syntax Analysis Part I

Top-Down Parsing and Intro to Bottom-Up Parsing

Syntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis

Computer Science 160 Translation of Programming Languages

CS415 Compilers Syntax Analysis Bottom-up Parsing

Syntax Analysis Part III

Syntax Analysis, VI Examples from LR Parsing. Comp 412

Lecture 11 Context-Free Languages

Context free languages

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

Computer Science 160 Translation of Programming Languages

Predictive parsing as a specific subclass of recursive descent parsing complexity comparisons with general parsing

Introduction to Bottom-Up Parsing

CSE302: Compiler Design

Syntax Analysis, VII The Canonical LR(1) Table Construction. Comp 412

Bottom-Up Parsing. Ÿ rm E + F *idÿ rm E +id*idÿ rm T +id*id. Ÿ rm F +id*id Ÿ rm id + id * id

Talen en Compilers. Johan Jeuring , period 2. October 29, Department of Information and Computing Sciences Utrecht University

Introduction to Bottom-Up Parsing

CS415 Compilers Syntax Analysis Top-down Parsing

Languages. Languages. An Example Grammar. Grammars. Suppose we have an alphabet V. Then we can write:

CS 406: Bottom-Up Parsing

Introduction to Bottom-Up Parsing

n Top-down parsing vs. bottom-up parsing n Top-down parsing n Introduction n A top-down depth-first parser (with backtracking)

Parsing -3. A View During TD Parsing

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Lecture Notes on Inductive Definitions

Introduction to Bottom-Up Parsing

Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x where

Compiler Design 1. LR Parsing. Goutam Biswas. Lect 7

LR2: LR(0) Parsing. LR Parsing. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

Context-Free Grammars and Languages

Creating a Recursive Descent Parse Table

Bottom-Up Syntax Analysis

This lecture covers Chapter 5 of HMU: Context-free Grammars

Context-free Grammars and Languages

Harvard CS 121 and CSCI E-207 Lecture 10: Ambiguity, Pushdown Automata

EXAM. Please read all instructions, including these, carefully NAME : Problem Max points Points 1 10 TOTAL 100

Announcement: Midterm Prep

Compiler Design. Spring Syntactic Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Context-Free Grammars (and Languages) Lecture 7

Context-Free Grammars and Languages. Reading: Chapter 5

Compiler Construction Lent Term 2015 Lectures (of 16)

CFLs and Regular Languages. CFLs and Regular Languages. CFLs and Regular Languages. Will show that all Regular Languages are CFLs. Union.

Context Free Grammars

Compiler Construction Lent Term 2015 Lectures (of 16)

Theory of Computation - Module 3

5 Context-Free Languages

Parsing VI LR(1) Parsers

CS 314 Principles of Programming Languages

Lecture Notes on Inductive Definitions

CS5371 Theory of Computation. Lecture 7: Automata Theory V (CFG, CFL, CNF)

Compiling Techniques

CISC4090: Theory of Computation

Compiler Principles, PS4

THEORY OF COMPILATION

Compiler Design Spring 2017

Why augment the grammar?

Recursive descent for grammars with contexts

Chapter 4: Bottom-up Analysis 106 / 338

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

[4203] Compiler Theory Sheet # 3-Answers

CSC 4181Compiler Construction. Context-Free Grammars Using grammars in parsers. Parsing Process. Context-Free Grammar

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

INF5110 Compiler Construction

CS20a: summary (Oct 24, 2002)

UNIT II REGULAR LANGUAGES

Compiling Techniques

CPS 220 Theory of Computation

CS153: Compilers Lecture 5: LL Parsing

Context-free Languages and Pushdown Automata

LR(1) Parsers Part III Last Parsing Lecture. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.

Harvard CS 121 and CSCI E-207 Lecture 9: Regular Languages Wrap-Up, Context-Free Grammars

INF5110 Compiler Construction

Context-Free Grammars and Languages. We have seen that many languages cannot be regular. Thus we need to consider larger classes of langs.

Introduction and Motivation. Introduction and Motivation. Introduction to Computability. Introduction and Motivation. Theory. Lecture5: Context Free

Review. Earley Algorithm Chapter Left Recursion. Left-Recursion. Rule Ordering. Rule Ordering

Turing machines and linear bounded automata

INF5110 Compiler Construction

Suppose h maps number and variables to ɛ, and opening parenthesis to 0 and closing parenthesis

Grammars and Context Free Languages

FLAC Context-Free Grammars

CMPT-825 Natural Language Processing. Why are parsing algorithms important?

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

I 1 : {S S } I 2 : {S X ay, Y X } I 3 : {S Y } I 4 : {X b Y, Y X, X by, X c} I 5 : {X c } I 6 : {S Xa Y, Y X, X by, X c} I 7 : {X by } I 8 : {Y X }

Parsing with Context-Free Grammars

Compiler Principles, PS7

Transcription:

Lecture 6: 9 October 2015

Announcement New tutorial session: Friday 2pm check ct course webpage to find your allocated group

Table of contents 1 2 Ambiguity s

Bottom-Up Parser A bottom-up parser builds a derivation by working from the input sentence back to the start symbol. S γ 0 γ 1 γ n 1 γ n To reduce γ i to γ i 1, match some rhs β against γ i then replace β with its corresponding lhs, A, assuming A β

: CFG A ::= A b c A ::= b Input: abbcde abbcde

: CFG A ::= A b c A ::= b Input: abbcde abbcde aabcde

: CFG A ::= A b c A ::= b Input: abbcde abbcde aabcde aade

: CFG A ::= A b c A ::= b Input: abbcde abbcde aabcde aade aabe

: CFG A ::= A b c A ::= b Input: abbcde abbcde aabcde aade aabe Goal

: CFG A ::= A b c A ::= b Input: abbcde productions abbcde aabcde aade aabe Goal reductions Note that the production follows a rightmost derivation.

: CFG A ::= A b c b Leftmost derivation Goal aabe aabcbe abbcbe abbcde Rightmost derivation Goal aabe aade aabcde abbcde LL parsers LR parsers

Shit-reduce parser It consists of a stack and the input It uses four actions: 1 shift: next symbol is shifted onto the stack 2 reduce: pop the symbols Y n,..., Y 1 from the stack that form the right member of a production X ::= Y n,..., Y 1 3 accept: stop parsing and report success 4 error: error reporting routine How does the parser know when to shift or when to reduce? Similarly to the top-down parser, can back-track if wrong decision made or try to look ahead. Can build a DFA to decide when we should shift or reduce.

Shit-reduce parser : CFG A ::= A b c b Operation: Input abbcde Stack

Shit-reduce parser : CFG A ::= A b c b Operation: shift Input bbcde Stack a

Shit-reduce parser : CFG A ::= A b c b Operation: shift Input bcde Stack ab

Shit-reduce parser : CFG A ::= A b c b Operation: Input bcde Stack ab Choice here: shift or reduce? Can lookahead one symbol to make decision. (Knowing what to do is not explain here, need to analyse the grammar, see EaC 3.5)

Shit-reduce parser : CFG A ::= A b c b Operation: reduce Input bcde Stack aa

Shit-reduce parser : CFG A ::= A b c b Operation: shift Input cde Stack aab

Shit-reduce parser : CFG A ::= A b c b Operation: Input cde Stack aab Choice here: shift or reduce? Can lookahead one symbol to make decision. (Knowing what to do is not explain here, need to analyse the grammar, see EaC 3.5)

Shit-reduce parser : CFG A ::= A b c b Operation: shift Input de Stack aabc

Shit-reduce parser : CFG A ::= A b c b Operation: reduce Input de Stack aa

Shit-reduce parser : CFG A ::= A b c b Operation: shift Input e Stack aad

Shit-reduce parser : CFG A ::= A b c b Operation: reduce Input e Stack aab

Shit-reduce parser : CFG A ::= A b c b Operation: shift Input Stack aabe

Shit-reduce parser : CFG A ::= A b c b Operation: reduce Input Stack Goal

Top-Down vs Top-Down + Easy to write by hand + Easy to integrate with compiler - Recursion might lead to performance problems Bottom-Up + Very efficient - Requires generation tools - Rigid integration to compiler

Ambiguity s Ambiguity definition If a grammar has more than one leftmost (or rightmost) derivation for a single sentential form, the grammar is ambiguous This is a problem when interpreting an input program or when building an internal representation

Ambiguity s Ambiguous Grammar: example 1 Expr :: = Expr Op Expr num i d Op ::= + This grammar has multiple leftmost derivations for x + 2 y

Ambiguity s Ambiguous Grammar: example 1 Expr :: = Expr Op Expr num i d Op ::= + This grammar has multiple leftmost derivations for x + 2 y One possible derivation Expr Expr Op Expr i d ( x ) Op Expr i d ( x ) + Expr i d ( x ) + Expr Op Expr i d ( x ) + num ( 2 ) Op Expr i d ( x ) + num ( 2 ) Expr i d ( x ) + num ( 2 ) i d ( y ) x + (2 y)

Ambiguity s Ambiguous Grammar: example 1 Expr :: = Expr Op Expr num i d Op ::= + This grammar has multiple leftmost derivations for x + 2 y One possible derivation Expr Expr Op Expr i d ( x ) Op Expr i d ( x ) + Expr i d ( x ) + Expr Op Expr i d ( x ) + num ( 2 ) Op Expr i d ( x ) + num ( 2 ) Expr i d ( x ) + num ( 2 ) i d ( y ) x + (2 y) Another possible derivation Expr Expr Op Expr Expr Op Expr Op Expr i d ( x ) Op Expr Op Expr i d ( x ) + Expr Op Expr i d ( x ) + num ( 2 ) Op Expr i d ( x ) + num ( 2 ) Expr i d ( x ) + num ( 2 ) i d ( y ) (x + 2) y

Ambiguity s Ambiguous grammar: example 2 Stmt ::= i f Expr then Stmt i f Expr then Stmt e l s e Stmt OtherStmt input if E1 then if E2 then S1 else S2

Ambiguity s Ambiguous grammar: example 2 Stmt ::= i f Expr then Stmt i f Expr then Stmt e l s e Stmt OtherStmt input if E1 then if E2 then S1 else S2 One possible interpretation i f E1 then i f E2 then S1 e l s e S2

Ambiguity s Ambiguous grammar: example 2 Stmt ::= i f Expr then Stmt i f Expr then Stmt e l s e Stmt OtherStmt input if E1 then if E2 then S1 else S2 One possible interpretation i f E1 then i f E2 then S1 e l s e S2 Another possible interpretation i f E1 then i f E2 then S1 e l s e S2

Ambiguity s Removing Ambiguity Must rewrite the grammar to avoid generating the problem Match each else to innermost unmatched if (common sense) Unambiguous grammar Stmt ::= WithElse NoElse WithElse ::= i f Expr then WithElse e l s e WithElse OtherStmt NoElse ::= i f Expr then Stmt i f Expr then WithElse e l s e NoElse Intuition: a NoElse always has no else on its last cascaded else if statement With this grammar, the example has only one derivation

Ambiguity s Stmt ::= WithElse NoElse WithElse ::= i f Expr then WithElse e l s e WithElse OtherStmt NoElse ::= i f Expr then Stmt i f Expr then WithElse e l s e NoElse Derivation for: if E1 then if E2 then S1 else S2 Stmt NoElse i f Expr then Stmt i f E1 then Stmt i f E1 then WithElse i f E1 then i f Expr then WithElse e l s e WithElse i f E1 then i f E2 then WithElse e l s e WithElse i f E1 then i f E2 then S1 e l s e WithElse i f E1 then i f E2 then S1 e l s e S2 This binds the else controlling S2 to the inner if.

Ambiguity s Deeper ambiguity Ambiguity usually refers to confusion in the CFG (Context Free Grammar) Consider the following case: a = f(17) In Algol-like languages, f could be either a function of an array In such case, context is required Need to track declarations Really a type issue, not context-free syntax Requires en extra-grammatical solution Must handle these with a different mechanism Step outside the grammar rather than making it more complex. This will be treated during semantic analysis.

Ambiguity s Ambiguity Final Words Ambiguity arises from two distinct sources: Confusion in the context-free syntax (e.g., if then else) Confusion that requires context to be resolved (e.g., array vs function) Resolving ambiguity: To remove context-free ambiguity, rewrite the grammar To handle context-sensitive ambiguity delay the detection of such problem (semantic analysis phase) For instance, it is legal during syntactic analysis to have: void i ; i=4;

Next lecture Ambiguity s Parse tree and abstract syntax tree