Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Re-enter Chomsky More about grammars

2 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 b ’s) Consider the string “aaabb” which is a valid string in L and can be derived from the grammar. Left-most derivation S  A B  a A B  aa A B  aaa B  aaa bB  aaa bb Right-most derivation S  A B  A bB  A bb  aA bb  aaA bb  aaa bb A Parse Tree (for the same string) S B A Bb b Aa aA a The leaves of the tree form the input string. This is a “common” representation where the order of derivation is not explicit—there is no such thing as “left parse tree” or “right parse tree”!

3 3 Parse trees (any combination of a ’s & b’s that contains at least one b somewhere) Parse Tree 1 Consider L = { w є { a,b } * | w contains a ‘ b’ } S  L b L L  aL | bL | ε S Consider the string “abba” which is a valid string in L and can be derived/generated from/by the grammar. LbL aL ε Parse Tree 2 S LbL ε aLa L bL ε The grammar can generate the input string in two different ways. In other words, there are two different parse trees for the string. Since it’s unclear as to how exactly the grammar should generate the string, the grammar is said to be ambiguous *. Note that the grammar on the previous slide is not ambiguous. abba LL bL a ε L abba LL * This example is based on an observation by Mr. Hui Zhang, a COMPSCI 220 student.

4 4 Parse trees S  X b Y has only zero/more a ’sfirst occurrence of b zero/more a ’s and b ’s Consider the string “abba” which is a valid string in L and can be derived/generated from/by the grammar. S XbY aX ε bY a ε Y abba XY There is only one way in which you can “group” the input string this time! An unambiguous grammar for L = { w є { a,b } * | w contains a ‘ b’ } X  a X | ε Y  a Y | b Y | ε

5 5 Ambiguous grammars A grammar is said to be ambiguous if it generates some string w є Σ* in more than one way, i.e. if the string has more than one parse tree.

6 6 What is wrong with ambiguity? Ambiguous grammars can be undesirable, for instance, in Compiler Design*, where the code generated by the compiler might depend on the particular way in which the input string (a statement in a programming language) is generated. This will be demonstrated in the examples that follow. * Grammars are used to describe the syntax of statements in a programming language.

7 7 Grammar for IF-ELSE statement (i) IF (condition) Statement 1; Statement 2; (ii) IF (condition) Statement 1; ELSE Statement 2; Statement 3; Statement 1 Statement 2 yes no Is condition true? Statement 2 Statement 3 no Is condition true? yes Statement 1 Statement  IF_statement | … IF_statement  if (Cond) Statement else Statement

8 8 Statement  IF_statement | … IF_statement  if (Cond) Statement else Statement IF ( C 1 ) S 1 ; ELSE IF ( C 2 ) S 2 ; ELSE S 3 ; Statement IF_Statement if(Cond)StatementelseStatement C1C1 IF_Statement if(Cond)StatementelseStatement S1S1 C2C2 S2S2 S3S3 Grammar for IF-ELSE statement Consider the statement: Parse tree for the statement

9 9 IF ( C 1 ) IF ( C 2 ) S 2; ELSE S 3 ; Statement IF_Statement if ( Cond ) Statement C1C1 IF_Statement if(Cond)Statementelse Statement C2C2 S2S2 S3S3 IF ( C 1 ) IF ( C 2 ) S 2; ELSE S 3 ;  ? Statement IF_Statement C1C1 if (Cond)Statement C2C2 S2S2 S3S3 AMBIGUITY ! (the same expression can be generated in a different way) Statement if ( Cond ) Statement else Consider the statement: Parse tree 1Parse tree 2 Grammar for IF-ELSE statement

10 10 Grammar for arithmetic expressions VERSION I Consider arithmetic expressions with only one or two variables (that use +, -, * only). e.g. a, a + b, a – b, a * b E  Var E  Var + Var E  Var – Var E  Var * Var

11 11 VERSION II Consider arithmetic expressions with any number of variables (more realistic!). e.g. a + b – c * d E  Var E  Var + E E  Var – E E  Var * E Grammar for arithmetic expressions Try generating the expression: a * b + c E b Var a Wrong grouping! (wrong order of precedence) a * b + c 1 2 + EVar * E c

12 12 VERSION III Try to generate arithmetic expressions—preserving the order of precedence. E  Var E  E + E E  E – E E  E * E Grammar for arithmetic expressions Try generating the same expression again: a * b + c E E + E E * E Var ab c looks OK! E E * E E + E Var bc a AMBIGUITY ! (the same expression can be generated in a different way) Note: Each parse tree conveys a different “meaning”; each of them corresponds to a different code (therefore possibly different results) generated by the compiler.

13 13 VERSION IV Try to generate arithmetic expressions—preserving the order of precedence and also avoiding ambiguity. E  E + Term E  E – Term E  Term Term  Term * Var Term  Var Grammar for arithmetic expressions Try generating the same expression again: a * b + c E E + Term * Var a b c Term

14 14 What Context Free Grammars (CFGs) can’t express { a n b n c n | n, m > 0 } { a n b m c n d m | n, m > 0 } {w c w | w є Σ* } Examples of languages that can’t be generated by CFGs:

15 15 Four classes of grammar Type 3 (Regular grammar) A  a A  a B Right side: (i) a single terminal symbol OR (ii) a single terminal followed by a single non-terminal Left side: a single non-terminal symbol Type 2 (Context-free grammar) A  α Right side: no restriction (any string of terminals and non-terminals). Left side: a single non-terminal symbol Type 1 (Context-sensitive grammar) α  βα  β Right, left sides: no restriction except that length( α ) <= length ( β ) Type 0 (Phrase-structure grammar) α  βα  β Right, left sides: no restriction at all! A language is said to be type i ( i = 0, 1, 2, 3) if it can be specified by type i grammar and cannot specified by type ( i +1) grammar.

16 16 Converting FSA into equivalent grammar i b a j a b L = {strings of a’s and b’s—with at least one ‘a’} (i) For an a -transition from state i to state j, generate the production rule: A i  a A j Rules: (i) For the final state f, generate the production rule: A f  ε A i  a A j A j  ε A i  b A i A j  a A j A j  b A j Grammar rules that generate L Any given FSA can be mechanically converted into grammar rules that generate the exactly the same language recognized by the FSA.


Download ppt "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."

Similar presentations


Ads by Google