CMSC 330: Organization of Programming Languages. Context-Free Grammars Ambiguity

Similar documents
Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Context Free Grammars

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Context Free Grammars

CMSC 330: Organization of Programming Languages

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

Syntax Analysis Check syntax and construct abstract syntax tree

CS 314 Principles of Programming Languages

Context-Free Grammars

Introduction to Syntax Analysis. The Second Phase of Front-End

Introduction to Syntax Analysis

Defining syntax using CFGs

Compiler Construction

A Simple Syntax-Directed Translator

Syntax Analysis. Prof. James L. Frankel Harvard University. Version of 6:43 PM 6-Feb-2018 Copyright 2018, 2015 James L. Frankel. All rights reserved.

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5

Compiler Design Concepts. Syntax Analysis

Context-Free Languages and Parse Trees

Compilers Course Lecture 4: Context Free Grammars

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Parsing Part II. (Ambiguity, Top-down parsing, Left-recursion Removal)

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Models of Computation II: Grammars and Pushdown Automata

Syntax Analysis Part I

Fall Compiler Principles Context-free Grammars Refresher. Roman Manevich Ben-Gurion University of the Negev

Eng. Maha Talaat Page 1

MA513: Formal Languages and Automata Theory Topic: Context-free Grammars (CFG) Lecture Number 18 Date: September 12, 2011

CS 315 Programming Languages Syntax. Parser. (Alternatively hand-built) (Alternatively hand-built)

Homework. Context Free Languages. Before We Start. Announcements. Plan for today. Languages. Any questions? Recall. 1st half. 2nd half.

UNIT I PART A PART B

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Optimizing Finite Automata

Types of parsing. CMSC 430 Lecture 4, Page 1

1. Which of the following regular expressions over {0, 1} denotes the set of all strings not containing 100 as a sub-string?

Multiple Choice Questions

Dr. D.M. Akbar Hussain

Ambiguous Grammars and Compactification

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

Introduction to Parsing

Introduction to Parsing. Lecture 5

EECS 6083 Intro to Parsing Context Free Grammars

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Languages and Compilers

Principles of Programming Languages COMP251: Syntax and Grammars

CS 321 Programming Languages and Compilers. VI. Parsing

QUESTION BANK. Formal Languages and Automata Theory(10CS56)

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Part 5 Program Analysis Principles and Techniques

COP 3402 Systems Software Syntax Analysis (Parser)

University of Technology Department of Computer Sciences. Final Examination st Term. Subject:Compilers Design

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Context-Free Grammars

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

Parsing II Top-down parsing. Comp 412

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Context-Free Grammars and Languages (2015/11)

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

Limits of Computation p.1/?? Limits of Computation p.2/??

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Chapter Seven: Regular Expressions

Properties of Regular Expressions and Finite Automata

JNTUWORLD. Code No: R

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

Administrativia. PA2 assigned today. WA1 assigned today. Building a Parser II. CS164 3:30-5:00 TT 10 Evans. First midterm. Grammars.

LALR Parsing. What Yacc and most compilers employ.

CT32 COMPUTER NETWORKS DEC 2015

Parsing. For a given CFG G, parsing a string w is to check if w L(G) and, if it is, to find a sequence of production rules which derive w.

Principles of Programming Languages COMP251: Syntax and Grammars

CS 406/534 Compiler Construction Parsing Part I

CS143 Midterm Fall 2008

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

Lecture 8: Context Free Grammars

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

CPS 506 Comparative Programming Languages. Syntax Specification

CMPT 755 Compilers. Anoop Sarkar.

Introduction to Parsing. Lecture 5

Syntax Analysis. Martin Sulzmann. Martin Sulzmann Syntax Analysis 1 / 38

Syntax. In Text: Chapter 3

Compilerconstructie. najaar Rudy van Vliet kamer 140 Snellius, tel rvvliet(at)liacs(dot)nl. college 3, vrijdag 22 september 2017

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1

Defining syntax using CFGs

LANGUAGE PROCESSORS. Presented By: Prof. S.J. Soni, SPCE Visnagar.

Outline. Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

Plan for Today. Regular Expressions: repetition and choice. Syntax and Semantics. Context Free Grammars

Introduction to Parsing. Lecture 8

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

Building a Parser II. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

CMPS Programming Languages. Dr. Chengwei Lei CEECS California State University, Bakersfield

Announcements. Written Assignment 1 out, due Friday, July 6th at 5PM.

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

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

CS308 Compiler Principles Syntax Analyzer Li Jiang

More on Syntax. Agenda for the Day. Administrative Stuff. More on Syntax In-Class Exercise Using parse trees

CONTEXT FREE GRAMMAR. presented by Mahender reddy

15 212: Principles of Programming. Some Notes on Grammars and Parsing

Transcription:

CMSC 330: Organization of Programming Languages Context-Free Grammars Ambiguity

Review Why should we study CFGs? What are the four parts of a CFG? How do we tell if a string is accepted by a CFG? What s a parse tree? CMSC 330 2

Review A sentential form is a string of terminals and nonterminals produced from the start symbol Inductively: The start symbol If αaδ is a sentential form for a grammar, where (α and δ (V Σ)*), and A γ is a production, then αγδ is a sentential form for the grammar In this case, we say that αaδ derives αγδ in one step, which is written as αaδ αγδ CMSC 330 3

Leftmost and Rightmost Derivation Example: S a SbS String: aba Leftmost Derivation Rightmost Derivation S SbS abs aba S SbS Sba aba At every step, apply production to leftmost non-terminal At every step, apply production to rightmost non-terminal Both derivations happen to have the same parse tree A parse tree has a unique leftmost and a unique rightmost derivation Not every string has a unique parse tree Parse trees don t show the order productions are applied CMSC 330 4

More on Leftmost/Rightmost Derivations Is the following derivation leftmost or rightmost? S as at au acu ac There s at most one non-terminal in each sentential form, so there's no choice between left or right nonterminals to expand How about the following derivation? S SbS SbSbS SbabS ababs ababa CMSC 330 5

Multiple Leftmost Derivations S a SbS Can we find more than one leftmost derivation? A leftmost derivation S SbS abs absbs ababs ababa Another leftmost derivation S SbS SbSbS absbs ababs ababa CMSC 330 6

Ambiguity A string is ambiguous for a grammar if it has more than one parse tree Equivalent to more than one leftmost (or more than one rightmost) derivation A grammar is ambiguous if it generates an ambiguous string It can be hard to see this with manual inspection Exercise: can you create an unambiguous grammar for S a SbS? CMSC 330 7

Are these Grammars Ambiguous? (1) S as T T bt U U cu ε (2) S T T T Tx Tx x x (3) S SS () (S) CMSC 330 8

Ambiguity of Grammar (Example 3) 2 different parse trees for the same string: ()()() 2 distinct leftmost derivations : S SS SSS ()SS ()()S ()()() S SS ()S ()SS ()()S ()()() We need unambiguous grammars to manage programming language semantics CMSC 330 9

Tips for Designing Grammars Closures: use recursive productions to generate an arbitrary number of symbols A xa ε Zero or more x s A ya y One or more y s CMSC 330 10

Tips for Designing Grammars Concatenation: use separate non-terminals to generate disjoint parts of a language, and then combine in a production G = S AB A aa ε B bb ε L(G) = a * b * CMSC 330 11

Tips for Designing Grammars (cont d) Matching constructs: write productions which generate strings from the middle {a n b n n 0} (not a regular language!) S asb ε Example: S asb aasbb aabb {a n b 2n n 0} S asbb ε CMSC 330 12

Tips for Designing Grammars (cont d) {a n b m m 2n, n 0} S asbb B ε B bb b The following grammar also works: S asbb B B bb ε How about the following? S asbb bs ε CMSC 330 13

Tips for Designing Grammars (cont d) {a n b m a n+m n 0, m 0} Rewrite as a n b m a m a n, which now has matching superscripts (two pairs) Would this grammar work? S asa B B bba ba Doesn t allow m = 0 Corrected: S asa B B bba ε The outer a n a n are generated first, then the inner b m a m CMSC 330 14

Tips for Designing Grammars (cont d) Union: use separate nonterminals for each part of the union and then combine { a n (b m c m ) m > n 0} Can be rewritten as { a n b m m > n 0} { a n c m m > n 0} CMSC 330 15

Tips for Designing Grammars (cont d) { a n b m m > n 0} { a n c m m > n 0} S T U T atb Tb b U auc Uc c T generates the first set U generates the second set What s the parse tree for string abbb? Ambiguous! CMSC 330 16

Tips for Designing Grammars (cont d) { a n b m m > n 0} { a n c m m > n 0} Will this fix the ambiguity? S T U T atb bt b U auc cu c It's not ambiguous, but it can generate invalid strings such as babb CMSC 330 17

Tips for Designing Grammars (cont d) { a n b m m > n 0} { a n c m m > n 0} Unambiguous version S T V T atb U U Ub b V avc W W Wc c CMSC 330 18

CFGs for Languages Recall that our goal is to describe programming languages with CFGs We had the following example which describes limited arithmetic expressions E a b c E+E E-E E*E (E) What s wrong with using this grammar? It s ambiguous! CMSC 330 19

Example: a-b-c E E-E a-e a-e-e a-b-e a-b-c E E-E E-E-E a-e-e a-b-e a-b-c Corresponds to a-(b-c) Corresponds to (a-b)-c CMSC 330 20

The Issue: Associativity Ambiguity is bad here because if the compiler needs to generate code for this expression, it doesn t know what the programmer intended So what do we mean when we write a-b-c? In mathematics, this only has one possible meaning It s (a-b)-c, since subtraction is left-associative a-(b-c) would be the meaning if subtraction was rightassociative CMSC 330 21

Another Example: If-Then-Else <stmt> ::= <assignment> <if-stmt>... <if-stmt> ::= if (<expr>) <stmt> if (<expr>) <stmt> else <stmt> (Here <> s are used to denote nonterminals and ::= for productions) Consider the following program fragment: if (x > y) if (x < z) a = 1; else a = 2; Note: Ignore newlines CMSC 330 22

Parse Tree #1 Else belongs to inner if CMSC 330 23

Parse Tree Else belongs to outer if CMSC 330 24

Fixing the Expression Grammar Idea: Require that the right operand of all of the operators is not a bare expression E E+T E-T E*T T T a b c (E) Now there's only one parse tree for a-b-c Exercise: Give a derivation for the string a-(b-c) CMSC 330 25

What if We Wanted Right-Associativity? Left-recursive productions are used for leftassociative operators Right-recursive productions are used for rightassociative operators Left: Right: E E+T E-T E*T T T a b c (E) E T+E T-E T*E T T a b c (E) CMSC 330 26

Parse Tree Shape The kind of recursion/associativity determines the shape of the parse tree left recursion right recursion Exercise: draw a parse tree for a-b-c in the prior grammar in which subtraction is right-associative CMSC 330 27

A Different Problem How about the string a+b*c? E E+T E-T E*T T T a b c (E) Doesn t have correct precedence for * When a nonterminal has productions for several operators, they effectively have the same precedence How can we fix this? CMSC 330 28

Final Expression Grammar E E+T E-T T lowest precedence operators T T*P P higher precedence P a b c (E) highest precedence (parentheses) Each non-terminal represents a level of precedence At each level, operations are left-associative CMSC 330 29

Final Expression Grammar E E+T E-T T T T*P P P a b c (E) Exercises: Construct tree and left and and right derivations for a+b*c a*(b+c) a*b+c a-b-c See what happens if you change the first set of productions to E E +T E-T T P See what happens if you change the last set of productions to P a b c E (E) CMSC 330 30