fall 2006costas busch - rpi1 context-free languages

Download Fall 2006Costas Busch - RPI1 Context-Free Languages

Post on 31-Mar-2015

216 views

Category:

Documents

2 download

Embed Size (px)

TRANSCRIPT

  • Slide 1

Fall 2006Costas Busch - RPI1 Context-Free Languages Slide 2 Fall 2006Costas Busch - RPI2 Regular Languages Context-Free Languages Slide 3 Fall 2006Costas Busch - RPI3 Context-Free Languages Pushdown Automata Context-Free Grammars stack automaton Slide 4 Fall 2006Costas Busch - RPI4 Context-Free Grammars Slide 5 Fall 2006Costas Busch - RPI5 Grammars Grammars express languages Example: the English language grammar Slide 6 Fall 2006Costas Busch - RPI6 Slide 7 Fall 2006Costas Busch - RPI7 Derivation of string the dog walks: Slide 8 Fall 2006Costas Busch - RPI8 Derivation of string a cat runs: Slide 9 Fall 2006Costas Busch - RPI9 Language of the grammar: L = { a cat runs, a cat sleeps, the cat runs, the cat sleeps, a dog runs, a dog sleeps, the dog runs, the dog sleeps } Slide 10 Fall 2006Costas Busch - RPI10 Variables Sequence of Terminals (symbols) Productions Sequence of Variables Slide 11 Fall 2006Costas Busch - RPI11 Another Example Grammar: Variable Sequence of terminals and variables The right side may be Slide 12 Fall 2006Costas Busch - RPI12 Grammar: Derivation of string : Slide 13 Fall 2006Costas Busch - RPI13 Grammar: Derivation of string : Slide 14 Fall 2006Costas Busch - RPI14 Other derivations: Grammar: Slide 15 Fall 2006Costas Busch - RPI15 Grammar: Language of the grammar: Slide 16 Fall 2006Costas Busch - RPI16 We write: Instead of: for zero or more derivation steps A Convenient Notation Slide 17 Fall 2006Costas Busch - RPI17 in zero or more derivation steps In general we write: If: Trivially: Slide 18 Fall 2006Costas Busch - RPI18 Example Grammar Possible Derivations Slide 19 Fall 2006Costas Busch - RPI19 Another convenient notation: Slide 20 Fall 2006Costas Busch - RPI20 Formal Definitions Set of variables Set of terminal symbols Start variable Set of productions Grammar: Slide 21 Fall 2006Costas Busch - RPI21 Context-Free Grammar: All productions in are of the form String of variables and terminals Variable Slide 22 Fall 2006Costas Busch - RPI22 variables terminals productions start variable Example of Context-Free Grammar Slide 23 Fall 2006Costas Busch - RPI23 For a grammar with start variable String of terminals or Language of a Grammar: Slide 24 Fall 2006Costas Busch - RPI24 context-free grammar : Example: Since, there is derivation for any Slide 25 Fall 2006Costas Busch - RPI25 A language is context-free if there is a context-free grammar with Context-Free Language: Slide 26 Fall 2006Costas Busch - RPI26 since context-free grammar : Example: is a context-free language generates Slide 27 Fall 2006Costas Busch - RPI27 Context-free grammar : Example derivations: Palindromes of even length Another Example Slide 28 Fall 2006Costas Busch - RPI28 Context-free grammar : Example derivations: () ((( ))) (( )) Describes matched parentheses: Another Example Slide 29 Fall 2006Costas Busch - RPI29 Derivation Order and Derivation Trees Slide 30 Fall 2006Costas Busch - RPI30 Derivation Order Consider the following example grammar with 5 productions: Slide 31 Fall 2006Costas Busch - RPI31 Leftmost derivation order of string : At each step, we substitute the leftmost variable Slide 32 Fall 2006Costas Busch - RPI32 Rightmost derivation order of string : At each step, we substitute the rightmost variable Slide 33 Fall 2006Costas Busch - RPI33 Rightmost derivation of : Leftmost derivation of : Slide 34 Fall 2006Costas Busch - RPI34 Derivation Trees Consider the same example grammar: And a derivation of : Slide 35 Fall 2006Costas Busch - RPI35 yield Slide 36 Fall 2006Costas Busch - RPI36 yield Slide 37 Fall 2006Costas Busch - RPI37 yield Slide 38 Fall 2006Costas Busch - RPI38 yield Slide 39 Fall 2006Costas Busch - RPI39 yield Derivation Tree (parse tree) Slide 40 Fall 2006Costas Busch - RPI40 Give same derivation tree Sometimes, derivation order doesnt matter Leftmost derivation: Rightmost derivation: Slide 41 Fall 2006Costas Busch - RPI41 Ambiguity Slide 42 Fall 2006Costas Busch - RPI42 Grammar for mathematical expressions Example strings: Denotes any number Slide 43 Fall 2006Costas Busch - RPI43 A leftmost derivation for Slide 44 Fall 2006Costas Busch - RPI44 Another leftmost derivation for Slide 45 Fall 2006Costas Busch - RPI45 Two derivation trees for Slide 46 Fall 2006Costas Busch - RPI46 take Slide 47 Fall 2006Costas Busch - RPI47 Good TreeBad Tree Compute expression result using the tree Slide 48 Fall 2006Costas Busch - RPI48 Two different derivation trees may cause problems in applications which use the derivation trees: Evaluating expressions In general, in compilers for programming languages Slide 49 Fall 2006Costas Busch - RPI49 Ambiguous Grammar: A context-free grammar is ambiguous if there is a string which has: two different derivation trees or two leftmost derivations (Two different derivation trees give two different leftmost derivations and vice-versa) Slide 50 Fall 2006Costas Busch - RPI50 stringhas two derivation trees this grammar is ambiguous since Example: Slide 51 Fall 2006Costas Busch - RPI51 stringhas two leftmost derivations this grammar is ambiguous also because Slide 52 Fall 2006Costas Busch - RPI52 IF_STMTif EXPR then STMT if EXPR then STMT else STMT Another ambiguous grammar: VariablesTerminals Very common piece of grammar in programming languages Slide 53 Fall 2006Costas Busch - RPI53 If expr1 then if expr2 then stmt1 else stmt2 IF_STMT expr1then elseifexpr2then STMT stmt1 if IF_STMT expr1thenelse ifexpr2then STMTstmt2 if stmt1 stmt2 Two derivation trees Slide 54 Fall 2006Costas Busch - RPI54 In general, ambiguity is bad and we want to remove it Sometimes it is possible to find a non-ambiguous grammar for a language But, in general we cannot do so Slide 55 Fall 2006Costas Busch - RPI55 Ambiguous Grammar Non-Ambiguous Grammar Equivalent generates the same language A successful example: Slide 56 Fall 2006Costas Busch - RPI56 Unique derivation tree for Slide 57 Fall 2006Costas Busch - RPI57 An un-successful example: every grammar that generates this language is ambiguous is inherently ambiguous: Slide 58 Fall 2006Costas Busch - RPI58 Example (ambiguous) grammar for : Slide 59 Fall 2006Costas Busch - RPI59 The string has always two different derivation trees (for any grammar) For example

Recommended

View more >