Module 29 Parse/Derivation Trees Ambiguous Grammars

Slides:



Advertisements
Similar presentations
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Advertisements

CFGs and PDAs Sipser 2 (pages ). Long long ago…
Grammars, constituency and order A grammar describes the legal strings of a language in terms of constituency and order. For example, a grammar for a fragment.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
CS5371 Theory of Computation
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.
Module 28 Context Free Grammars Definition of a grammar G
1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition.
1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language.
Context-Free Grammars Lecture 7
Prof. Bodik CS 164 Lecture 81 Grammars and ambiguity CS164 3:30-5:00 TT 10 Evans.
Balanced Parentheses G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\}
CS 536 Spring Ambiguity Lecture 8. CS 536 Spring Announcement Reading Assignment –“Context-Free Grammars” (Sections 4.1, 4.2) Programming.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Specifying Languages CS 480/680 – Comparative Languages.
COP4020 Programming Languages
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
1 Lecture 27 Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions If-then-else Statements.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
CMSC 330: Organization of Programming Languages
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
Syntax Analysis The recognition problem: given a grammar G and a string w, is w  L(G)? The parsing problem: if G is a grammar and w  L(G), how can w.
Context-free Grammars [Section 2.1] - more powerful than regular languages - originally developed by linguists - important for compilation of programming.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2010.
1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n  0} is not regular. L 2 = {(), (()), ((())),...} is not regular.  some properties.
Classification of grammars Definition: A grammar G is said to be 1)Right-linear if each production in P is of the form A  xB or A  x where A and B are.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
Chapter 5 Context-Free Grammars
PART I: overview material
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lecture 15 Naveen Z Quazilbash Ambiguity. Overview S-Grammars Ambiguity in Grammars Ambiguous grammars and Unambiguous Grammars.
CS 3240: Languages and Computation Context-Free Languages.
ISBN Chapter 3 Describing Syntax and Semantics.
Context Free Grammars. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
Syntax Context-Free Grammars, Syntax Trees. CFG for Arithmetic Expressions E::= E op E| (E) | num op ::= + | * num ::= 0 | 1 | 2 | … Backus-Naur Form.
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
作者 : 陳鍾誠 單位 : 金門技術學院資管系 URL : 日期 : 2016/6/4 程式語言的語法 Grammar.
CMSC 330: Organization of Programming Languages
Copyright © 2006 Addison-Wesley. All rights reserved. Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has.
Chapter 3 Describing Syntax and Semantics
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
Context Free Grammars 1. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
1 Parse Trees Definitions Relationship to Left- and Rightmost Derivations Ambiguity in Grammars.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Re-enter Chomsky More about grammars. 2 Parse trees S  A B A  aA | a B  bB | b Consider L = { a m b n | m, n > 0 } (one/more a ’s followed by one/more.
Grammars Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
Chapter 3 Context-Free Grammars Dr. Frank Lee. 3.1 CFG Definition The next phase of compilation after lexical analysis is syntax analysis. This phase.
Syntax Analysis – Part I EECS 483 – Lecture 4 University of Michigan Monday, September 17, 2006.
Top-Down Parsing.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Chapter 5 Context-Free Languages Regular language are effective in describing certain simple patterns. But it is not enough to describe programming languages.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Syntax(1). 2 Syntax  The syntax of a programming language is a precise description of all its grammatically correct programs.  Levels of syntax Lexical.
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
5. Context-Free Grammars and Languages
CS510 Compiler Lecture 4.
Fall Compiler Principles Context-free Grammars Refresher
Syntax (1).
Compiler Construction (CS-636)
CSE 3302 Programming Languages
5. Context-Free Grammars and Languages
Relationship to Left- and Rightmost Derivations
Theory of Computation Lecture #
Fall Compiler Principles Context-free Grammars Refresher
Presentation transcript:

Module 29 Parse/Derivation Trees Ambiguous Grammars Leftmost derivations, rightmost derivations Ambiguous Grammars Examples Arithmetic expressions If-then-else Statements Inherently ambiguous CFL’s

Context-Free Grammars Parse Trees Leftmost/rightmost derivations Ambiguous grammars

Parse Tree Parse/derivation trees are structured derivations The structure graphically illustrates semantic information about the string Formalization of concept we encountered in regular languages unit Note, what we saw before were not exactly parse trees as we define them now, but they were close

Parse Tree Example Parse tree for string ( )(( )) and grammar BALG BALG = (V, Σ, S, P) V = {S}, Σ = {(, )}, S = S P = S → SS | (S) | l One derivation of ( )(( )) S ==> SS ==> (S)S ==> ( )S ==> ( )(S) ==> ( )((S)) ==> ( )(( )) Parse tree l S ( )

Comments about Example * Syntax: draw a unique arrow from each variable to each character that is a direct child of that variable A line instead of an arrow is ok The derived string can be read in a left to right traversal of the leaves Semantics The tree graphically illustrates the nesting structure of the string of parentheses l S ( )

Leftmost/Rightmost Derivations There is more than one derivation of the string ( )(( )). S ==> SS ==> (S)S ==>( )S ==> ( )(S) ==> ( )((S)) ==> ( )(( )) S ==> SS ==> (S)S ==> (S)(S) ==> ( )(S) S ==> SS ==> S(S) ==> S((S)) ==> S(( )) ==> (S)(( )) ==>( )(( )) Leftmost derivation Leftmost variable is always expanded Which one of the above is leftmost? Rightmost derivation Rightmost variable is always expanded Which one of the above is rightmost? l S ( )

Comments Fix a string and a grammar Example Unique mappings ( ) Fix a string and a grammar Any derivation corresponds to a unique parse tree Any parse tree can correspond to many different derivations Example The one parse tree corresponds to all three derivations Unique mappings For any parse tree, there is a unique leftmost/rightmost derivation that it corresponds to S ==> SS ==> (S)S ==>( )S ==> ( )(S) ==> ( )((S)) ==> ( )(( )) S ==> SS ==> (S)S ==> (S)(S) ==> ( )(S) S ==> SS ==> S(S) ==> S((S)) ==> S(( )) ==> (S)(( )) ==>( )(( ))

Example * S ==> SS ==> SSS ==> (S)SS ==> ( )SS ==> ( )S ==> ( ) The above is a leftmost derivation of the string ( ) from the grammar BALG Draw the corresponding parse tree Draw the corresponding rightmost derivation S ==> (S) ==> (SS) ==> (S(S)) ==> (S( )) ==> (( )) The above is a rightmost derivation of the string (( )) from the grammar BALG Draw the corresponding leftmost derivation

Ambiguous Grammars Examples: Arithmetic Expressions If-then-else statements Inherently ambiguous grammars

Ambiguous Grammars A grammar G is ambiguous if there exists a string x in L(G) with two or more distinct parse trees (2 or more distinct leftmost/rightmost derivations) Example Grammar AG is ambiguous String aaa in L(AG) has 2 rightmost derivations S ==> SS ==> SSS ==> SSa ==> Saa ==> aaa S ==> SS ==> Sa ==> SSa ==> Saa ==> aaa

2 Simple Examples Grammar BALG is ambiguous String ( ) in L(BALG) has more than 1 leftmost derivation S ==> (S) ==> ( ) S ==> (S) ==> (SS) ==>(S) ==>( ) Give another leftmost derivation of ( ) from BALG Grammar ABG is NOT ambiguous Consider any string x in {aibi | i > 0} There is a unique parse tree for x

Legal Arithmetic Expressions Develop a grammar MATHG = (V, Σ, S, P) for the language of legal arithmetic expressions S = {0, 1, +, *, -, /, (, )} Strings in the language include 10 10*11111+100 10*(11111+100) Strings not in the language include 10+ 11++101 )(

Grammar MATHG1 V = {E, N} Σ = {0, 1, +, *, -, /, (, )} S = E P: E → N | E+E | E*E | E/E | E-E | (E) N → N0 | N1 | 0 | 1

MATHG1 is ambiguous E → N | E+E | E*E | E/E | E-E | (E) N → N0 | N1 | 0 | 1 Come up with two distinct leftmost derivations of the string 11+0*11 E ==> E+E ==> N+E ==> N1+E ==> 11+E ==> 11+E*E ==> 11+N*E ==> 11+0*E ==> 11+0*N ==> 11+0*N1 ==> 11+0*11 E ==> E*E ==> E+E*E ==> N+E*E ==> N1+E*E ==> 11+E*E ==> 11+N*E ==> 11+0*E ==> 11+0*N ==> 11+0*N1 ==>11+0*11 Draw the corresponding parse trees

Corresponding Parse Trees E ==> E+E ==> N+E ==> N1+E ==> 11+E ==> 11+E*E ==> 11+N*E ==> 11+0*E ==> 11+0*N ==> 11+0*N1 ==> 11+0*11 E ==> E*E ==> E+E*E ==> N+E*E ==> N1+E*E ==> 11+E*E ==> 11+N*E ==> 11+0*E ==> 11+0*N ==> 11+0*N1 ==>11+0*11 E E * E E + + E N 1 N 1 E * N 1 N N N 1

Parse Tree Meanings E + * N 1 Note how the parse trees captures the semantic meaning of string 11+0*11. More specifically, what number does the first parse tree represent? What number does the second parse tree represent?

Implications Two interpretations of string 11+0*11 11+(0*11) = 11 (11+0)*11 = 1001 What if a line in a program is MSU_Tuition = 11+0*11; What is MSU_Tuition? Depends on how the expression 11+0*11 is parsed. This is not good. Ambiguity in grammars is undesirable, particularly if the grammar is used to develop a compiler for a programming language like C++. In this case, there is an unambiguous grammar for the language of arithmetic expressions

If-Then-Else Statements A grammar ITEG = (V, S, S, P) for the language of legal If-Then-Else statements V = (S, BOOL) Σ = {D<85, D>50, grade=3.5, grade=3.0, if, then, else} S = S P: S → if BOOL then S else S | if BOOL then S |grade=3.5 | grade=3.0 BOOL → D<85 | D>50

ITEG is ambiguous S → if BOOL then S |grade=3.5 | grade=3.0 | if BOOL then S else S BOOL → D<85 | D>50 Come up with two distinct leftmost derivations of the string if D<85 then if D>50 then grade=3.5 else grade=3.0 S ==>if BOOL then S else S ==> if D<85 then S else S ==> if D<85 then if BOOL then S else S ==> if D<85 then if D>50 then S else S ==> if D<85 then if D>50 then grade=3.5 else S ==> if D<85 then if D>50 then grade=3.5 else grade=3.0 S ==>if BOOL then S ==> if D<85 then S ==> if D<85 then if BOOL then S else S ==> if D<85 then if D>50 then S else S ==> if D<85 then if D>50 then grade=3.5 else S ==> if D<85 then if D>50 then grade=3.5 else grade=3.0 Draw the corresponding parse trees

Corresponding Parse Trees S ==>if BOOL then S else S ==> if D<85 then S else S ==> if D<85 then if BOOL then S else S ==> if D<85 then if D>50 then S else S ==> if D<85 then if D>50 then grade=3.5 else S ==> if D<85 then if D>50 then grade=3.5 else grade=3.0 S ==>if BOOL then S ==> if D<85 then S ==> if D<85 then if BOOL then S else S ==> if D<85 then if D>50 then S else S ==> if D<85 then if D>50 then grade=3.5 else S ==> if D<85 then if D>50 then grade=3.5 else grade=3.0 S S if B then S else if B then S D<85 if B then S grade=3.0 D<85 else S if B then D>50 grade=3.5 D>50 grade=3.5 grade=3.0

Parse Tree Meanings S S if B then S if B then S else S D<85 if B then S else S D<85 if B then S grade=3.0 D>50 grade=3.5 grade=3.0 D>50 grade=3.5 If you receive a 90 on type D points, what is your grade? By parse tree 1 By parse tree 2

Implications Two interpretations of string if D<85 then if D>50 then grade=3.5 else grade=3.0 Issue is which if-then does the last ELSE attach to? This phenomenon is known as the “dangling else” Answer: Typically, else binds to NEAREST if-then In this case, there is an unambiguous grammar for handling if-then’s as well as if-then-else’s

Inherently ambiguous CFL’s A CFL L is inherently ambiguous iff for all CFG’s G such that L(G) = L, G is ambiguous Examples so far None of the CFL’s we’ve seen so far are inherently ambiguous While the CFG’s we’ve seen ambiguous, there do exist unambiguous CFG’s for those CFL’s. There exist inherently ambiguous CFL’s Example: {aibjck | i=j or j=k or i=j=k} Note i=j=k is unnecessary, but I added it here for clarity

Summary Parse trees illustrate “semantic” information about strings Ambiguous grammars are undesirable This means there are multiple parse trees for some string These strings can be interpreted in multiple ways There are some heuristics people use for taking an ambiguous grammar and making it unambiguous, but this is not the focus of this course There are some inherently ambiguous CFL’s Thus, the above heuristics do not always work