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