Title: ContextFree Languages
1Context-Free Languages
2Regular Languages
3Context-Free Languages
Regular Languages
4Context-Free Languages
Context-Free Grammars
Pushdown Automata
stack
automaton
5Context-Free Grammars
6Grammars
- Grammars express languages
- Example the English language
7 8- A derivation of the dog walks
9- A derivation of a cat runs
10L a cat runs, a cat walks,
the cat runs, the cat walks,
a dog runs, a dog walks, the
dog runs, the dog walks
11Notation
Production Rules
Variable
Terminal
12Another Example
- Grammar
- Derivation of sentence
13- Grammar
- Derivation of sentence
14 15 16More Notation
Set of variables
Set of terminal symbols
Start variable
Set of Production rules
17Example
18More Notation
- Sentential Form
- A sentence that contains
- variables and terminals
- Example
Sentential Forms
sentence
19 20 21 22Example
Grammar
Derivations
23Example
Grammar
Derivations
24Another Grammar Example
Derivations
25More Derivations
26Language of a Grammar
- For a grammar
- with start variable
String of terminals
27Example
Since
28A Convenient Notation
29Example
A context-free grammar
A derivation
30 A context-free grammar
Another derivation
31(((( ))))
Describes parentheses
32Example
A context-free grammar
A derivation
33A context-free grammar
Another derivation
34(No Transcript)
35Example
A context-free grammar
A derivation
36A context-free grammar
A derivation
37Describes matched parentheses
() ((( ))) (( ))
38Definition Context-Free Grammars
Grammar
Variables
Terminal symbols
Start variable
Productions of the form
String of variables and terminals
Variable
39(No Transcript)
40Definition Context-Free Languages
- A language is context-free
- if and only if
- there is a context-free grammar
- with
41Derivation Order
42(No Transcript)
43Derivation Trees
44 45(No Transcript)
46(No Transcript)
47(No Transcript)
48Derivation Tree
49Derivation Tree
yield
50Ambiguity
51leftmost derivation
52leftmost derivation
53(No Transcript)
54The grammar
is ambiguous
string
has two derivation trees
55The grammar
is ambiguous
string
has two leftmost derivations
56Definition
A context-free grammar is ambiguous if
some string has two or
more derivation trees
57In other words
A context-free grammar is ambiguous if
some string has two or
more leftmost derivations
(or rightmost)
58Why do we care about ambiguity?
take
59(No Transcript)
60(No Transcript)
61Correct result
62- Ambiguity is bad for programming languages
- We want to remove ambiguity
63Another Ambiguous Grammar
IF_STMT
if EXPR then STMT
if EXPR then STMT else STMT
64If expr1 then if expr2 then stmt1 else stmt2
IF_STMT
if
expr1
then
STMT
else
if
expr2
then
stmt1
stmt2
IF_STMT
if
expr1
then
else
STMT
stmt2
if
expr2
then
stmt1
65Inherent Ambiguity
- Some context free languages
- have only ambiguous grammars
Example
66The string
has two derivation trees