Professional Documents
Culture Documents
SENTENCE
NOUN-PHRASE VERB-PHRASE
CMPLX-VERB
PREP-PHRASE NOUN-PHRASE
E→E+E
E E*E
E→E*E
(E) * E
E → (E)
(E + E) * E
E→0
(2 + E) * E
E→1
(2 + 3) * E
…
E→9
(2 + 3) * 5
E→E+E N → 0N Variables: E, N
E→E*E N → 1N Terminals: +, *, (, ), 0, 1
E → (E) N→0 Start variable: E
E→N N→1
shorthand: conventions:
E → E + E | E * E | (E) | N Variables in UPPERCASE
N → 0N | 1N | 0 | 1 Start variable comes first
Derivation
• A derivation is a sequential application of productions:
E E*E
(E)* E E → E + E | E * E | (E) | N
(E)* N N → 0N | 1N | 0 | 1
(E + E)* 1
derivation
(E + E)* 1
(E + N)* 1
(N + N)* 1
ab b obtained from a
(N + 1N)* 1
in one production
(N + 10)* 1 * b
a b obtained from a
(1 + 10)* 1 in zero or more
* (1 + 10)* 1
E productions
Context-free languages
• The language of a CFG is the set of all strings of
terminals that can be derived from the start variable
* w}
L(G) = {w : w S* and S
A → 0A1 | B
B→#
L(G) = {0n#1n: n ≥ 0}
# A B #
A → 0A1 | B variables: A, B
B→# terminals: 0, 1, #
start variable: A
S → SS | (S) |
() (()())
Parse trees
S → SS | (S) |
S (S) S S S (S)
(SS) ( S )( S ) (SS)
((S)S) (S(S))
(()S) (S())
(()(S)) ((S)())
(()()) (()())
Analysis example 2
S → SS | (S) |
L = {0n1n | n 0}
S → 0S1|
Design example 2
S → 0|LN 1052870032
N → ND|
any number N
D → 0|L
leading digit L
L → 1|2|3|4|5|6|7|8|9
Design examples
L = {0n1n0m1m | n 0, m 0} 010011
00110011
These strings have two parts: 000111
L = L1L2
L1 = {0n1n | n 0}
L2 = {0m1m | m 0}
S → S 1S 1
rules for L1: S1 → 0S11| S1 → 0S11 |
L2 is the same as L1
Design examples
L = {0n1m0m1n | n 0, m 0} 011001
0011
These strings have nested structure: 1100
00110011
outer part: 0n1n
inner part: 1m0m
S → 0S1|I
I → 1I0 |
Design examples
10010011010010110 A: , or ends in 1
initial part middle part final part C: , or begins with 1
A B C
Design examples
10010011010010110 A: , or ends in 1
A B C C: , or begins with 1
U: any string
S → ABC B has recursive structure:
A → | U1
U → 0U | 1U | 00110100
C → | 1U D
B → 0D0 | 0B0 same number of 0s
D → 1U1 | 1 at least one 0
regular
NFA DFA
expression
From regular to context-free
regular expression CFG
S → 0S1 | L = {0n1n: n ≥ 0}
regular context-free
Ambiguity
Parsing algorithm for CFGs
Ambiguity
• A grammar is ambiguous if some strings have more
than one parse tree
E → E + E | E * E | (E) | N
N → 1N | 2N | 1 | 2
E E
E + E E * E
1+2*2
V E * E E + E V
1 V V V V 2
2 2 =5 1 2 =6
Disambiguation
• Sometimes we can rewrite the grammar to remove
the ambiguity
E → E + E | E * E | (E) | N
N → 1N | 2N | 1 | 2
same precedence!
F F
T T
Divide expression
into terms and factors F F
2 * (1 + 2 * 2)
Disambiguation
E → E + E | E * E | (E) | N
N → 1N | 2N | 1 | 2
An expression is a sum of
one or more terms E→T|E+T
S → 0S1 | 1S0S1 | T
x = 00111
T→S|
S 0S1 00S11
01S0S11
0T1
1S0S1 10S10S1 when do we stop?
...
T S
Problems
S → 0S1 | 1S0S1 | T
x = 00111
T→S|
S 0S1 00S11
01S0S11 when do we stop?
0T1
1S0S1 10S10S1
...
Problems
S → 0S1 | 1S0S1 | T
x = 01011
T→S|
S → 0S1 | 1S0S1 | T
x = 00111
T→S|
*
N
S → ACD D→C
A→ a S → AD
B→ D→B
C → ED | D→
D → BC | b S → AC
E→b S →A
C →E
nullable variables: B, C, D
• Example
S T
S → 0S1 | 1S0S1 |
T S → 0S1 | 1S0S1
T →S|R| S→R|
R → 0SR R → 0SR
R
• Example
S → 0S1 | 1S0S1 S → 0S1 | 1S0S1
|R| | 0SR |
R → 0SR R → 0SR
• Exception: S →
– We will not use this rule at all, except to check if L
• Note
-productions must be eliminated before unit productions
Example: testing membership
eliminate S → | 01 | 101 | 0S1
S → 0S1 | 1S0S1 | T
T→S| unit, -prod |10S1 | 1S01 | 1S0S1
x = 00111
S 01, 101
0S1 0011, 01011
00S11 only strings of length ≥ 6
strings of length ≥ 6
10S1 10011, strings of length ≥ 6
1S01 10101, strings of length ≥ 6
1S0S1 only strings of length ≥ 6
Algorithm 1 for testing membership
• How to check if a string x ≠ is in L(G)
A → BC or A→a
S → AB | BC SAC
A → BA | a – SAC
B → CC | b – B B
C → AB | a
SA B SC SA
B AC AC B AC
x = baaba
b a a b a
S → AB | BC SAC
A → BA | a – SAC
B → CC | b – B B
C → AB | a
SA B SC SA
B AC AC B AC
x = baaba
b a a b a
…
If there is a production A → xi
Put A in table cell ii 12 23
For cells st in other rows 11 22 kk
If there is a production A → BC x1 x2 … xk
where B is in cell sj and C is in cell jt
Put A in cell st
1 s j t k