600 likes | 805 Views
Context-Free Languages. Context-Free Languages. Regular Languages. Context-Free Languages. Context-Free Grammars. Pushdown Automata. stack. automaton. Context-Free Grammars. Grammars. Grammars express languages Example: the English language grammar.
E N D
Context-Free Languages Costas Busch - RPI
Context-Free Languages Regular Languages Costas Busch - RPI
Context-Free Languages Context-Free Grammars Pushdown Automata stack automaton Costas Busch - RPI
Context-Free Grammars Costas Busch - RPI
Grammars • Grammars express languages • Example: the English language grammar Costas Busch - RPI
Derivation of string “the dog walks”: Costas Busch - RPI
Derivation of string “a cat runs”: Costas Busch - RPI
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” } Costas Busch - RPI
Productions Sequence of Terminals (symbols) Variables Sequence of Variables Costas Busch - RPI
Another Example Sequence of terminals and variables Grammar: Variable The right side may be Costas Busch - RPI
Grammar: • Derivation of string : Costas Busch - RPI
Grammar: • Derivation of string : Costas Busch - RPI
Grammar: Other derivations: Costas Busch - RPI
Grammar: Language of the grammar: Costas Busch - RPI
A Convenient Notation • We write: • Instead of: for zero or more derivation steps Costas Busch - RPI
In general we write: If: in zero or more derivation steps Trivially: Costas Busch - RPI
Example Grammar Possible Derivations Costas Busch - RPI
Another convenient notation: Costas Busch - RPI
Formal Definitions Grammar: Set of variables Set of terminal symbols Start variable Set of productions Costas Busch - RPI
Context-Free Grammar: All productions in are of the form Variable String of variables and terminals Costas Busch - RPI
Example of Context-Free Grammar productions start variable variables terminals Costas Busch - RPI
Language of a Grammar: • For a grammar with start variable String of terminals or Costas Busch - RPI
Example: context-free grammar : Since, there is derivation for any Costas Busch - RPI
A language is context-free • if there is a context-free grammar • with Context-Free Language: Costas Busch - RPI
Example: is a context-free language since context-free grammar : generates Costas Busch - RPI
Another Example Context-free grammar : Example derivations: Palindromes of even length Costas Busch - RPI
Another Example Context-free grammar : Example derivations: Describes matched parentheses: () ((( ))) (( )) Costas Busch - RPI
Derivation OrderandDerivation Trees Costas Busch - RPI
Derivation Order Consider the following example grammar with 5 productions: Costas Busch - RPI
Leftmost derivation order of string : At each step, we substitute the leftmost variable Costas Busch - RPI
Rightmost derivation order of string : At each step, we substitute the rightmost variable Costas Busch - RPI
Leftmost derivation of : Rightmost derivation of : Costas Busch - RPI
Derivation Trees Consider the same example grammar: And a derivation of : Costas Busch - RPI
yield Costas Busch - RPI
yield Costas Busch - RPI
yield Costas Busch - RPI
yield Costas Busch - RPI
Derivation Tree (parse tree) yield Costas Busch - RPI
Sometimes, derivation order doesn’t matter Leftmost derivation: Rightmost derivation: Give same derivation tree Costas Busch - RPI
Ambiguity Costas Busch - RPI
Grammar for mathematical expressions Example strings: Denotes any number Costas Busch - RPI
A leftmost derivation for Costas Busch - RPI
Another leftmost derivation for Costas Busch - RPI
Two derivation trees for Costas Busch - RPI
take Costas Busch - RPI
Good Tree Bad Tree Compute expression result using the tree Costas Busch - RPI
Two different derivation trees may cause problems in applications which use the derivation trees: • Evaluating expressions • In general, in compilers • for programming languages Costas Busch - RPI
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) Costas Busch - RPI
Example: this grammar is ambiguous since string has two derivation trees Costas Busch - RPI