1 Lecture 27 Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions If-then-else Statements.

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

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.
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
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 | /\}
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
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.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
COP4020 Programming Languages
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
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
Context-Free Grammars and Parsing
PART I: overview material
Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007.
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.
Syntax Context-Free Grammars, Syntax Trees. CFG for Arithmetic Expressions E::= E op E| (E) | num op ::= + | * num ::= 0 | 1 | 2 | … Backus-Naur Form.
作者 : 陳鍾誠 單位 : 金門技術學院資管系 URL : 日期 : 2016/6/4 程式語言的語法 Grammar.
CMSC 330: Organization of Programming Languages
Introduction to Parsing
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.
Module 29 Parse/Derivation Trees Ambiguous Grammars
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.
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
Theory of Computation Lecture #
Fall Compiler Principles Context-free Grammars Refresher
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

1 Lecture 27 Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions If-then-else Statements –Inherently ambiguous CFL’s

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

3 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

4 Parse Tree Example Parse tree for string ( )(( )) and grammar BALG –BALG = (V, , S, P) V = {S},  = {(, )}, S = S P = S --> SS | (S) | –One derivation of ( )(( )) S ==> SS ==> (S)S ==> ( )S ==> ( )(S) ==> ( )((S)) ==> ( )(( )) –Parse tree S SS SS(()) S()

5 Comments about Example Syntax: –draw a unique arrow from each variable to each character that is a direct child of that variable –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 S SS SS(()) S()

6 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)) ==> ( )(( )) –S ==> SS ==> S(S) ==> S((S)) ==> S(( )) ==> (S)(( )) ==>( )(( )) Leftmost derivation –Leftmost variable is always expanded –first one above is a leftmost derivation Rightmost derivation –Rightmost variable is always expanded –third one above is a rightmost derivation S SS SS(()) S()

7 Comments 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 SS(()) S() –S ==> SS ==> (S)S ==>( )S ==> ( )(S) ==> ( )((S)) ==> ( )(( )) –S ==> SS ==> (S)S ==> (S)(S) ==> ( )(S) ==> ( )((S)) ==> ( )(( )) –S ==> SS ==> S(S) ==> S((S)) ==> S(( )) ==> (S)(( )) ==>( )(( ))

8 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 ==> SS ==> S ==> SS ==> S ==> (S) ==> ( ) S ==> (S) ==> (SS) ==> (S(S)) ==> (S( )) ==> (( )) –The above is a rightmost derivation of the string (( )) from the grammar BALG –Draw the corresponding parse tree –Draw the corresponding leftmost derivation S ==> (S) ==>(SS) ==> (S) ==> ((S)) ==> (( ))

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

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

11 2 Simple Examples Grammar BALG is ambiguous –String ( ) has >2 leftmost derivations S ==> (S) ==> ( ) S ==> (S) ==> (SS) ==>(S) ==>( ) S ==> SS ==> (S)S ==> ( )S ==> ( ) –Draw the corresponding parse trees –Draw the corresponding rightmost derivations Grammar ABG is NOT ambiguous –Consider any string x in {a i b i | i > 0} There is a unique parse tree for x

12 Legal Arithmetic Expressions Develop a grammar MATHG = (V, , S, P) for the language of legal arithmetic expressions –  = {0, 1, +, *, -, /, (, )} –Strings in the language include * *( ) –Strings not in the language include )(

13 Grammar MATHG 1 V = {E, N}  = {0, 1, +, *, -, /, (, )} S = E P: –E --> N | E+E | E*E | E/E | E-E | (E) –N --> N0 | N1 | 0 | 1

14 MATHG 1 is ambiguous 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 E --> N | E+E | E*E | E/E | E-E | (E) N --> N0 | N1 | 0 | 1

15 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 N 1 1 N 0 N N1 1 E N N 1 1 * E E N 0 + E E N N1 1

16 Parse Tree Meanings E E E+ E E*N N 1 1 N 0 N N1 1 E N N 1 1 * E E N 0 + E E N N1 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? 11+(0*11) = 11 What number does the second parse tree represent? (11+0)*11 = 1001 (in binary notation)

17 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. In this case, there is an unambiguous grammar for the language of arithmetic expressions

18 If-Then-Else Statements A grammar ITEG = (V, , S, P) for the language of legal If-Then-Else statements –V = (S, BOOL) –  = {adv 75, 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 --> adv 75

19 ITEG is ambiguous Come up with two distinct leftmost derivations of the string –if adv 75 then grade=3.5 else grade=3.0 –S ==>if BOOL then S else S ==> if adv if adv if adv 75 then S else S ==> if adv 75 then grade=3.5 else S ==> if adv 75 then grade=3.5 else grade=3.0 –S ==>if BOOL then S ==> if adv if adv if adv 75 then S else S ==> if adv 75 then grade=3.5 else S ==> if adv 75 then grade=3.5 else grade=3.0 Draw the corresponding parse trees S --> if BOOL then S |grade=3.5 | grade=3.0 | if BOOL then S else S BOOL --> adv 75

20 Corresponding Parse Trees S ==>if BOOL then S else S ==> if adv if adv if adv 75 then S else S ==> if adv 75 then grade=3.5 else S ==> if adv 75 then grade=3.5 else grade=3.0 S ==>if BOOL then S ==> if adv if adv if adv 75 then S else S ==> if adv 75 then grade=3.5 else S ==> if adv 75 then grade=3.5 else grade=3.0 SS if BthenSelse S adv<90if BthenSgrade=3.0 adv>75 grade=3.5 if BthenS adv<90else S if BthenS adv>75 grade=3.5 grade=3.0

21 Parse Tree Meanings S if BthenSelse S if BthenS adv<90 adv>75 grade=3.5 grade=3.0 S if BthenS else S if BthenS adv<90 adv>75 grade=3.5grade=3.0 If you receive a 90 on advanced points, what is your grade? By parse tree 1 grade = 3.0 By parse tree 2 grade is undefined (as it should be)

22 Implications Two interpretations of string –if adv 75 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

23 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. Later result –There exist inherently ambiguous CFL’s –Example: {a i b j c k | i=j or j=k or i=j=k} Note i=j=k is unnecessary, but I added it here for clarity

24 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 grammars –Thus, the above heuristics do not always work