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