ContextFree Languages Prof Busch LSU 1 ContextFree Languages
Context-Free Languages Prof. Busch - LSU 1
Context-Free Languages Regular Languages Prof. Busch - LSU 2
Context-Free Languages Context-Free Grammars Pushdown Automata stack automaton Prof. Busch - LSU 3
Context-Free Grammars Prof. Busch - LSU 4
Grammars express languages Example: the English language grammar Prof. Busch - LSU 5
Prof. Busch - LSU 6
Derivation of string “the dog sleeps”: Prof. Busch - LSU 7
Derivation of string “a cat runs”: Prof. Busch - LSU 8
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 9
Productions Variables Sequence of Terminals (symbols) Sequence of Variables Prof. Busch - LSU 10
Another Example Sequence of terminals and variables Grammar: Variable The right side may be Prof. Busch - LSU 11
Grammar: Derivation of string : Prof. Busch - LSU 12
Grammar: Derivation of string Prof. Busch - LSU : 13
Grammar: Other derivations: Prof. Busch - LSU 14
Grammar: Language of the grammar: Prof. Busch - LSU 15
A Convenient Notation We write: for zero or more derivation steps Instead of: Prof. Busch - LSU 16
In general we write: If: in zero or more derivation steps Trivially: Prof. Busch - LSU 17
Example Grammar Possible Derivations Prof. Busch - LSU 18
Another convenient notation: Prof. Busch - LSU 19
Formal Definitions Grammar: Set of variables Set of terminal symbols Start variable Prof. Busch - LSU Set of productions 20
Context-Free Grammar: All productions in are of the form Variable String of variables and terminals Prof. Busch - LSU 21
Example of Context-Free Grammar productions variables terminals Prof. Busch - LSU start variable 22
Language of a Grammar: For a grammar with start variable String of terminals or Prof. Busch - LSU 23
Example: context-free grammar : Since, there is derivation for any Prof. Busch - LSU 24
Context-Free Language definition: A language is context-free if there is a context-free grammar with Prof. Busch - LSU 25
Example: is a context-free language since context-free grammar : generates Prof. Busch - LSU 26
Another Example Context-free grammar : Example derivations: Palindromes of even length Prof. Busch - LSU 27
Another Example Context-free grammar : Example derivations: Describes matched parentheses: () ((( ))) (( )) Prof. Busch - LSU 28
Derivation Order and Derivation Trees Prof. Busch - LSU 29
Derivation Order Consider the following example grammar with 5 productions: Prof. Busch - LSU 30
Leftmost derivation order of string : At each step, we substitute the leftmost variable Prof. Busch - LSU 31
Rightmost derivation order of string : At each step, we substitute the rightmost variable Prof. Busch - LSU 32
Leftmost derivation of Rightmost derivation of Prof. Busch - LSU : : 33
Derivation Trees Consider the same example grammar: And a derivation of : Prof. Busch - LSU 34
yield Prof. Busch - LSU 35
yield Prof. Busch - LSU 36
yield Prof. Busch - LSU 37
yield Prof. Busch - LSU 38
Derivation Tree (parse tree) yield Prof. Busch - LSU 39
Sometimes, derivation order doesn’t matter Leftmost derivation: Rightmost derivation: Give same derivation tree Prof. Busch - LSU 40
Ambiguity Prof. Busch - LSU 41
Grammar for mathematical expressions Example strings: Denotes any number Prof. Busch - LSU 42
A leftmost derivation for Prof. Busch - LSU 43
Another leftmost derivation for Prof. Busch - LSU 44
Two derivation trees for Prof. Busch - LSU 45
take Prof. Busch - LSU 46
Good Tree Bad Tree Compute expression result using the tree Prof. Busch - LSU 47
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 48
Ambiguous Grammar: A context-free grammar if there is a string is ambiguous 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 49
Example: this grammar is ambiguous since string has two derivation trees Prof. Busch - LSU 50
this grammar is ambiguous also because string has two leftmost derivations Prof. Busch - LSU 51
A successful example: Equivalent Ambiguous Grammar Non-Ambiguous Grammar generates the same language Prof. Busch - LSU 52
Unique derivation tree for Prof. Busch - LSU 53
An un-successful example: is inherently ambiguous: every grammar that generates this language is ambiguous Prof. Busch - LSU 54
Example (ambiguous) grammar for Prof. Busch - LSU : 55
The string has always two different derivation trees (for any grammar) For example Prof. Busch - LSU 56
- Slides: 56