Title: Formal Languages
1Formal Languages Context-Free Languages Slides
based on RPI CSCI 2400 Thanks to Petros Drineas
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
33Language?
34A context-free grammar
Another derivation
35(No Transcript)
36Example
A context-free grammar
A derivation
37A context-free grammar
A derivation
38Describes matched parentheses
() ((( ))) (( ))
39Definition Context-Free Grammars
Grammar
Variables
Terminal symbols
Start variable
Productions of the form
String of variables and terminals
Variable
40(No Transcript)
41Definition Context-Free Languages
- A language is context-free
- if and only if
- there is a context-free grammar
- with
42Derivation Order
43(No Transcript)
44Derivation Trees
45 46(No Transcript)
47(No Transcript)
48(No Transcript)
49Derivation Tree
50Derivation Tree
yield
51Partial Derivation Trees
Partial derivation tree
52Partial derivation tree
53sentential form
Partial derivation tree
yield
54Sometimes, derivation order doesnt matter
Leftmost
Rightmost
Same derivation tree
55Ambiguity
56leftmost derivation
57leftmost derivation
58(No Transcript)
59The grammar
is ambiguous
string
has two derivation trees
60The grammar
is ambiguous
string
has two leftmost derivations
61Definition
A context-free grammar is ambiguous if
some string has two or
more derivation trees
62In other words
A context-free grammar is ambiguous if
some string has two or
more leftmost derivations
(or rightmost)
63Why do we care about ambiguity?
take
64(No Transcript)
65(No Transcript)
66Correct result
67- Ambiguity is bad for programming languages
- We want to remove ambiguity
68Ambiguity in natural language?
69We fix the ambiguous grammar
New non-ambiguous grammar
70(No Transcript)
71Unique derivation tree
72The grammar
is non-ambiguous
Every string has a unique
derivation tree
73Another Ambiguous Grammar
IF_STMT
if EXPR then STMT
if EXPR then STMT else STMT
74If 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
75Inherent Ambiguity
- Some context free languages
- have only ambiguous grammars
Example
76The string
has two derivation trees