Fall 2005Costas Busch - RPI1 Context-Free Languages
Fall 2005Costas Busch - RPI2 Regular Languages
Fall 2005Costas Busch - RPI3 Regular Languages Context-Free Languages
Fall 2005Costas Busch - RPI4 Context-Free Languages Pushdown Automata Context-Free Grammars stack automaton
Fall 2005Costas Busch - RPI5 Context-Free Grammars
Fall 2005Costas Busch - RPI6 Grammars Grammars express languages Example: the English language
Fall 2005Costas Busch - RPI7
Fall 2005Costas Busch - RPI8 A derivation of “the dog walks”:
Fall 2005Costas Busch - RPI9 A derivation of “a cat runs”:
Fall 2005Costas Busch - RPI10 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” }
Fall 2005Costas Busch - RPI11 Notation VariableTerminal Production Rules
Fall 2005Costas Busch - RPI12 Another Example Grammar: Derivation of sentence :
Fall 2005Costas Busch - RPI13 Grammar: Derivation of sentence :
Fall 2005Costas Busch - RPI14 Other derivations: Grammar:
Fall 2005Costas Busch - RPI15 Language of the grammar
Fall 2005Costas Busch - RPI16 More Notation Grammar Set of variables Set of terminal symbols Start variable Set of Production rules
Fall 2005Costas Busch - RPI17 Example Grammar :
Fall 2005Costas Busch - RPI18 More Notation Sentential Form: A sentence that contains variables and terminals Example: Sentential Formssentence
Fall 2005Costas Busch - RPI19 We write: Instead of:
Fall 2005Costas Busch - RPI20 In zero or more steps In general we write: If: Trivially:
Fall 2005Costas Busch - RPI21 Example Grammar Possible Derivations
Fall 2005Costas Busch - RPI22 Another Grammar Example Grammar : Derivations:
Fall 2005Costas Busch - RPI23 More Derivations Grammar
Fall 2005Costas Busch - RPI24 Language of a Grammar For a grammar with start variable : String of terminals
Fall 2005Costas Busch - RPI25 Example For grammar : Since:
Fall 2005Costas Busch - RPI26 A Convenient Notation
Fall 2005Costas Busch - RPI27 Example Context-free grammar : Possible Derivations: (((( )))) Describes parentheses:
Fall 2005Costas Busch - RPI28 Context-free grammar : Derivations: Example Palindromes of even length
Fall 2005Costas Busch - RPI29 A context-free grammar : Derivations: Example () ((( ))) (( )) Describes matched parentheses:
Fall 2005Costas Busch - RPI30 Definition: Context-Free Grammars Grammar Productions of the form: String of variables and terminals VariablesTerminal symbols Start variable Variable
Fall 2005Costas Busch - RPI31 The language of the grammar is:
Fall 2005Costas Busch - RPI32 Definition: Context-Free Languages A language is context-free if and only if there is a context-free grammar with
Fall 2005Costas Busch - RPI33 Derivation Order and Derivation Trees
Fall 2005Costas Busch - RPI34 Derivation Order Leftmost derivation: Rightmost derivation:
Fall 2005Costas Busch - RPI35 Leftmost derivation: Rightmost derivation:
Fall 2005Costas Busch - RPI36 Derivation Trees
Fall 2005Costas Busch - RPI37
Fall 2005Costas Busch - RPI38
Fall 2005Costas Busch - RPI39
Fall 2005Costas Busch - RPI40 Derivation Tree
Fall 2005Costas Busch - RPI41 yield Derivation Tree
Fall 2005Costas Busch - RPI42 Partial Derivation Trees Partial derivation tree
Fall 2005Costas Busch - RPI43 Partial derivation tree
Fall 2005Costas Busch - RPI44 Partial derivation tree sentential form yield
Fall 2005Costas Busch - RPI45 Same derivation tree Sometimes, derivation order doesn’t matter Leftmost: Rightmost:
Fall 2005Costas Busch - RPI46 Ambiguity
Fall 2005Costas Busch - RPI47 leftmost derivation
Fall 2005Costas Busch - RPI48 leftmost derivation
Fall 2005Costas Busch - RPI49 Two derivation trees
Fall 2005Costas Busch - RPI50 The grammar is ambiguous: stringhas two derivation trees
Fall 2005Costas Busch - RPI51 stringhas two leftmost derivations The grammar is ambiguous:
Fall 2005Costas Busch - RPI52 Definition: A context-free grammar is ambiguous if some string has: two different derivation trees OR two leftmost derivations
Fall 2005Costas Busch - RPI53 Why do we care about ambiguity? take
Fall 2005Costas Busch - RPI54
Fall 2005Costas Busch - RPI55 Good derivation Tree Bad derivation Tree
Fall 2005Costas Busch - RPI56 We want to remove ambiguity Ambiguity is bad for programming languages
Fall 2005Costas Busch - RPI57 We fix the ambiguous grammar: New unambiguous grammar:
Fall 2005Costas Busch - RPI58 Unique derivation tree
Fall 2005Costas Busch - RPI59 Another Ambiguous Grammar IF_STMTif EXPR then STMT if EXPR then STMT else STMT
Fall 2005Costas Busch - RPI60 If expr1 then if expr2 then stmt1 else stmt2 IF_STMT expr1then elseifexpr2then STMT stmt1 if IF_STMT expr1thenelse ifexpr2then STMTstmt2 if stmt1 stmt2
Fall 2005Costas Busch - RPI61 Inherent Ambiguity Some context free languages have only ambiguous grammars Example:
Fall 2005Costas Busch - RPI62 The string has always two derivation trees (for any grammar) For example