Context-Free Grammars

advertisement
CDT314
FABER
Formal Languages, Automata
and Models of Computation
Lecture 5
Mälardalen University
2010
1
Content
Midterm solutions
Regular vs. Nonregular Languages
Context-Free Languages
Context-Free Grammars
Derivation Trees. Ambiguity
Push-Down Automata, PDA
Applications
2
A Nonregular Language
L  {a b : i  n}
i i
a0
a
a a a  a a a
n 1
2
a1
b
b0
b
b
b1

b
b
an
b
b
bn  2
bn1
b
DFA must have infinite number of states.
States
ai , a j
are distinct for each
i j
3
n l n l
{a b c
: n, l  0}{a : n  0}
n!
Non-regular languages
Context-Free Languages
n n
R
{a b }
{ww }
Regular Languages
4
Automata theory: formal languages and
formal grammars
Grammar
Languages
Automaton
Type-0
Recursively
enumerable
Turing machine
Type-1
Contextsensitive
Linear-bounded nondeterministic Turing
machine
Type-2
Context-free
Non-deterministic pushdown
automaton
Production
rules
No restrictions
and
Type-3
Regular
Finite state automaton
5
Context-Free Languages
6
Context-Free Languages
Context-Free
Grammars
Pushdown
Automata
7
Context-Free Grammars
8
Example
A context-free grammar G
S  aSb
S 
A derivation
S  aSb  aaSbb  aabb
9
A context-free grammar G
S  aSb
S 
Another derivation
S  aSb  aaSbb  aaaSbbb  aaabbb
10
S  aSb
S 
L(G )  {a b : n  0}
n n
( ( ( ( ) ) ) )
11
Example
A context-free grammar
G
S  aSa
S  bSb
S 
A derivation
S  aSa  abSba  abba
12
A context-free grammar
G
S  aSa
S  bSb
S 
Another derivation
S  aSa  aaSaa  aaaSaaa
 aaabSbaaa  aaabbaaa
13
S  aSa
S  bSb
S 
L(G)  {ww : w {a, b}*}
R
14
Example
A context-free grammar
G
S  aSb
S  SS
S 
A derivation
S  SS  aSbS  abS  ab
15
A context-free grammar
G
S  aSb
S  SS
S 
A derivation
S  SS  aSbS  abS  abaSb  abab
16
S  aSb
S  SS
S 
L(G )  {w : na ( w)  nb ( w),
and na (v)  nb (v)
in any prefix v}
( )( ( ( ) ) ) ( ( ) ) 
17
Definition: Context-Free Grammars
Grammar
Variables
G  (V ,T , S , P)
Terminal
symbols
Start
variables
Productions of the form:
A x
x is string of variables and terminals
18
Definition: Context-Free Languages
A language
L
is context-free
if and only if there is a grammar
G with
L  L(G )
19
Derivation Order
1. S  AB
2. A  aaA
3. A  
4. B  Bb
5. B  
Leftmost derivation
1
2
3
4
5
S  AB  aaAB  aaB  aaBb  aab
20
Derivation Order
1. S  AB
2. A  aaA
3. A  
4. B  Bb
5. B  
Rightmost derivation
1
4
5
2
3
S  AB  ABb  Ab  aaAb  aab
21
S  aAB
A  bBb
B  A| 
Leftmost derivation
S  aAB  abBbB  abAbB  abbBbbB
 abbbbB  abbbb
22
S  aAB
A  bBb
B  A| 
Rightmost derivation
S  aAB  aA  abBb  abAb
 abbBbb  abbbb
23
Derivation Trees
24
S  AB
A  aaA | 
B  Bb | 
S  AB
S
A
B
25
S  AB
A  aaA | 
B  Bb | 
S  AB  aaAB
S
A
a
a
B
A
26
A  aaA | 
S  AB
B  Bb | 
S  AB  aaAB  aaABb
S
A
a
a
B
A
B
b
27
A  aaA | 
S  AB
B  Bb | 
S  AB  aaAB  aaABb  aaBb
S
A
a
a
B
A

B
b
28
S  AB
A  aaA | 
B  Bb | 
S  AB  aaAB  aaABb  aaBb  aab
S
Derivation Tree
B
A
a
a
A
B


b
29
A  aaA | 
S  AB
B  Bb | 
S  AB  aaAB  aaABb  aaBb  aab
S
Derivation Tree
A
a
a
B
A
B


yield
b
aab
 aab
30
Partial Derivation Trees
S  AB
A  aaA | 
B  Bb | 
S  AB
Partial derivation tree
S
A
B
31
S  AB  aaAB
Partial derivation tree
S
A
a
a
B
A
32
S  AB  aaAB
sentential form
Partial derivation tree
S
yield
A
a
a
B
aaAB
A
33
Sometimes, derivation order doesn’t matter
Leftmost:
S  AB  aaAB  aaB  aaBb  aab
Rightmost:
S  AB  ABb  Ab  aaAb  aab
S
Same derivation tree
A
a
a
B
A
B


b
34
Ambiguity
35
E  E  E | E  E | (E) | a
a  a a
E
E
a

E
a
derivation
(* denotes multiplication)
E  E  E  a  E  a  E E
 a  a  E  a  a a
E

E
a
leftmost derivation
36
E  E  E | E  E | (E) | a
a  a a
derivation
E
E
E


E
E  E E  E  E E
 a  E E  a  aE
E
 a  a a
a
leftmost derivation
a
a
37
E  E  E | E  E | (E) | a
a  aa
E
E
a
E

E
E
a

E
E
a
a
E

E

E
a
a
38
E  E  E | E  E | (E) | a
a  aa
Two derivation trees
E
E
a

E
E
a

E
E
a
a
E
E

E

E
a
a
39
The grammar
E  E  E | E  E | (E) | a
is ambiguous!
String a  a  a has two derivation trees
E
E
a
E

E
E
a

E
E
a
a
E

E

E
a
a
40
The grammar
E  E  E | E  E | (E) | a
is ambiguous:
string
a  a  a has two leftmost derivations
E  E  E  a E  a EE
 a  a E  a  a*a
E  EE  E  EE  a EE
 a  aE  a  aa
41
Definition
A context-free grammar
G is ambiguous
if some string w L(G ) has
two or more derivation trees
(two or more leftmost/rightmost derivations)
42
Why do we care about ambiguity?
a  aa
a2
E
E
a

E
E
E
a

E
E
a
a
E

E

E
a
a
43
Why do we care about ambiguity?
2  22
E
E
2
E

E
E
2

E
E
2
2
E

E

E
2
2
44
Why do we care about ambiguity?
2  22
6
E
2
E
2
8
E
4
E

2
E

2
2  22  6
2
E
2
E
2
2
4
E

2
E

2
E
2
2
2  22  8
45
Correct result:
2  22  6
6
E
2
E
2
4
E

2
E
2

2
E
2
46
Ambiguity is bad
for programming languages
We want to remove ambiguity!
47
We fix the ambiguous grammar…
E  E  E | E  E | (E) | a
E  E T
…by introducing parentheses ()
to indicate grouping, (precedence)
E T
T T F
Non-ambiguous grammar
T F
F  (E)
F a
48
E  E T T T  F T  a T  a T F
 a  F F  a  aF  a  aa
E
E  E T
a  aa

E T
E
T T F
T
T
F
F
T F
F  (E)
F a
a
T
a

F
a
49
Unique derivation tree
a  aa
E
E

T
T
T
F
F
a
a

F
a
50
The grammar G :
E  E T
E T
T T F
T F
F  (E)
is non-ambiguous
F a
Every string w L(G ) has a unique
derivation tree
51
Inherent Ambiguity
Some context free languages
have only ambiguous grammars!
Example:
S  S1 | S2
L  {a b c }  {a b c }
n n m
n m m
S1  S1c | A
S2  aS2 | B
A  aAb | 
B  bBc | 
52
The string
n n n
a b c
has two derivation trees
S1
S
S
S1
S2
c
a
S2
53
Definition: Context-Free Grammars
Grammar
Variables
G  (V ,T , S , P)
Terminal
symbols
Start
variables
Productions of the form:
A x
x is string of variables and terminals
54
Definition: Regular Grammars
Grammar
Variables
G  (V ,T , S , P)
Terminal
symbols
Start
variables
Right or Left Linear Grammars. Productions of the form:
A  xB
A  Bx
or
Cx
x is string of terminals
55
n l n l
{a b c
: n, l  0}{a : n  0}
n!
Non-regular languages
Context-Free Languages
n n
R
{a b }
{ww }
Regular Languages
56
Context-Free Languages
Context-Free
Grammars
Pushdown
Automata
stack
automaton
57
Pushdown Automata
PDAs
58
Pushdown Automaton - PDA
Input String
Stack
States
59
The Stack
A PDA can write symbols on stack
and read them later on.
POP reading symbol
PUSH writing symbol

y
x
z
All access to the stack only on the top!
(Stack top is written leftmost in the string, e.g. yxz)
A stack is valuable as it can hold an unlimited
amount of information.
The stack allows pushdown automata to
recognize some non-regular languages.
60
The States
Input
symbol
Pop old
reading
stack symbol
q1 a, b / c
Push new
writing
stack symbol
q2
61
q1 a, b / c
q2
input

a


a

stack
b
h
e
$
top
Replace
c
h
e
$
(An alternative is to start and finish with empty stack)
62
q1 a,  / c
q2
input

a


stack
b
h
e
$
top
Push
a

c
b
h
e
$
63
q1 a,b / 
q2
input

a


a

stack
b
h
e
$
top
Pop
h
e
$
64
q1 a,
/
q2
input

a


a

stack
b
h
e
$
top
No Change
b
h
e
$
65
Time 0
Example 3.7 Salling:
A PDA for simple nested parenthesis strings
(
(
(
)
)

)
Input
(, / (
start
s
Stack
), ( /
), (/ 
q
end
66
Example 3.7
Time 1
Input
(
(
(
)
)
(, / (
start
s
(
)

Stack
), ( /
), (/ 
q
end
67
Example 3.7
Time 2
Input
(
(
(
(
)
)
)
(

(, / (
start
s
Stack
), ( /
), (/ 
q
end
68
Example 3.7
Time 3
Input
(
(
(
)
)
(
(
)
(

(, / (
start
s
), ( /
), (/ 
q
Stack
end
69
Example 3.7
Time 4
Input
(
(
(
)
)
(
(
)
(
(, / (

), ( /
Stack
start
s
), (/ 
q
end
70
Example 3.7
Time 5
Input
(
(
(
)
)
(
)
(

(, / (
start
s
), ( /
), (/ 
q
Stack
end
71
Example 3.7
Time 6
Input
(
(
(
)
)
(
)

), ( /
(, / (
start
s
Stack
), (/ 
q
end
72
Example 3.7
Time 7
Input
(
(
(
)
)

)
Stack
(, / (
start
s
), ( /
), (/ 
q
end
73
Applications:
Compilers
74
Machine Code
Program
v = 5;
if (v>5)
x = 12 + v;
while (x !=3) {
x = x - 3;
v = 10;
}
......
Compiler
Add v,v,0
cmp v,5
jmplt ELSE
THEN:
add x, 12,v
ELSE:
WHILE:
cmp x,3
...
75
Compiler
Lexical
analyzer
input
program
parser
output
machine
code 76
A parser “knows” the grammar
of the programming language
77
Parser
PROGRAM  STMT_LIST
STMT_LIST STMT; STMT_LIST | STMT;
STMT EXPR | IF_STMT | WHILE_STMT
| { STMT_LIST }
EXPR  EXPR + EXPR | EXPR - EXPR | ID
IF_STMT  if (EXPR) then STMT
| if (EXPR) then STMT else STMT
WHILE_STMT while (EXPR) do STMT
78
The parser finds the derivation
of a particular input
derivation
Parser
input
10 + 2 * 5
EE+E
|E*E
| INT
EE+E
E+E*E
 10 + E*E
 10 + 2 * E
 10 + 2 * 5
79
derivation
EE+E
E+E*E
 10 + E*E
 10 + 2 * E
 10 + 2 * 5
derivation tree
E
E
+
E
10
E
*
E
5
2
80
derivation tree
E
E
machine code
+
E
mult a, 2, 5
add b, 10, a
10
E
2
*
E
5
81
Parsing
82
Parser
input
string
grammar
derivation
83
Example:
Parser
input
aabb
S  SS
derivation
S  aSb
S  bSa
?
S 
84
Exhaustive Search
S  SS | aSb | bSa | 
Phase 1:
S  SS
S  aSb
Find derivation of
aabb
S  bSa
S 
All possible derivations of length 1
85
S  SS
aabb
S  aSb
S  bSa
S 
86
Phase 2
S  SS | aSb | bSa | 
S  SS  SSS
S  SS  aSbS
Phase 1
S  SS  bSaS
S  SS
S  SS  S
S  aSb
S  aSb  aSSb
aabb
S  aSb  aaSbb
S  aSb  abSab
S  aSb  ab
87
S  SS | aSb | bSa | 
Phase 2
S  SS  SSS
S  SS  aSbS
aabb
S  SS  S
S  aSb  aSSb
S  aSb  aaSbb
Phase 3
S  aSb  aaSbb  aabb
88
Final result of exhaustive search
(top-down parsing)
Parser
input
aabb
S  SS
S  aSb
S  bSa
S 
derivation
S  aSb  aaSbb  aabb
89
Context Free Art
http://www.contextfreeart.org/
90
Context Free Art
http://www.contextfreeart.org/
91
Download