Title: Languages and Finite Automata
1Formal Languages Context-Free Languages Hinrich
Schütze IMS, Uni Stuttgart, WS 2006/07 Slides
based on RPI CSCI 2400 Thanks to Costas Busch
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
13Language?
14- Grammar
- Derivation of sentence
15 16 17More Notation
Set of variables
Set of terminal symbols
Start variable
Set of Production rules
18Example
19More Notation
- Sentential Form
- A sentence that contains
- variables and terminals
- Example
Sentential Forms
sentence
20 21 22 23Example
Grammar
Derivations
24Example
Grammar
Derivations
25Another Grammar Example
Derivations
26Language?
27More Derivations
28Language of a Grammar
- For a grammar
- with start variable
String of terminals
29Example
Since
30A Convenient Notation
31Example
A context-free grammar
A derivation
32 A context-free grammar
Another derivation
33(((( ))))
Describes parentheses
34Example
A context-free grammar
A derivation
35Language?
36A context-free grammar
Another derivation
37(No Transcript)
38Example
A context-free grammar
A derivation
39Language?
40A context-free grammar
A derivation
41Interpretation?
42Describes matched parentheses
() ((( ))) (( ))
43Definition Context-Free Grammars
Grammar
Variables
Terminal symbols
Start variable
Productions of the form
String of variables and terminals
Variable
44(No Transcript)
45Definition Context-Free Languages
- A language is context-free
- if and only if
- there is a context-free grammar
- with
46Derivation Order
47Language?
48(No Transcript)
49Language?
50Derivation Trees
51 52(No Transcript)
53(No Transcript)
54(No Transcript)
55Derivation Tree
56Derivation Tree
yield
57Partial Derivation Trees
Partial derivation tree
58Partial derivation tree
59sentential form
Partial derivation tree
yield
60Sometimes, derivation order doesnt matter
Leftmost
Rightmost
Same derivation tree
61Ambiguity
62leftmost derivation
63leftmost derivation
64(No Transcript)
65The grammar
is ambiguous
string
has two derivation trees
66The grammar
is ambiguous
string
has two leftmost derivations
67Definition
A context-free grammar is ambiguous if
some string has two or
more derivation trees
68In other words
A context-free grammar is ambiguous if
some string has two or
more leftmost derivations
(or rightmost)
69Why do we care about ambiguity?
take
70(No Transcript)
71(No Transcript)
72Correct result
73- Ambiguity is bad for programming languages
- We want to remove ambiguity
74We fix the ambiguous grammar
New non-ambiguous grammar
75(No Transcript)
76Unique derivation tree
77The grammar
is non-ambiguous
Every string has a unique
derivation tree
78Another Ambiguous Grammar
IF_STMT
if EXPR then STMT
if EXPR then STMT else STMT
79If 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
80Inherent Ambiguity
- Some context free languages
- have only ambiguous grammars
Example
81The string
has two derivation trees
82Ambiguity in natural language?