7. Context Free Languages
Jan 27, 2022
-
context free languages is a more general class of languages:
-
contains all regular languages (strict subset), and others
-
e.g.
and are context free but not regular
-
-
can be described with context free grammars or pushdown automata
-
primarily interesting for parsers
A language
Grammars
-
grammars express languages using (a list of) production rules
-
productions replace variables with other variables or terminals
-
example: grammar for
|
-
example: even length palindromes
| |
-
odd length palindromes (different language)
| | | |
-
notation hiding intermediate derivation steps
-
means in zero or more derivation steps -
trivially
-
Formal definition
Grammar
-
is s set of variables -
is a set of terminal symbols -
is a start variable ( ) -
is a set of productions- all in form
where is string of variables and terminals
- all in form
Language of grammar
-
-
is string of terminals of
Derivation order and derivation trees
Example
Consider the grammar with 5 productions, and derivation of string
Leftmost derivation:
Rightmost derivation:
Sometimes derivation order does not matter, both give the same derivation tree:
(Take the leaf nodes to get
1 2 3 4 5 6 7 |
|
Ambiguity
-
a context free grammar
is ambiguous is there is a string which has two different derivation trees or two leftmost derivations -
ambiguity may cause problems in applications using derivation trees e.g. evaluating expressions; in general compilers
-
Two ways to handle ambiguity:
-
(1) write non-ambiguous grammar (difficult)
-
(2) add rules to the parser to handle priority (practical solution)
-
-
It is not possible to write all grammars as non-ambiguous
- e.g.
is inherently ambiguous
the string always has two different derivation trees for any grammar
- e.g.
Example
(A) leftmost derivation of
1 2 3 4 5 6 7 |
|
(B) another leftmost derivation of
1 2 3 4 5 6 7 |
|
Bottom up evaluation for a=2: (A) gives correct result 2+2*2=6 but (B) does not 2+2*2=8.
This grammar is ambiguous because there are two different leftmost derivations.