Escolar Documentos
Profissional Documentos
Cultura Documentos
Kong
CSC 3130: Automata theory and formal languages
Andrej Bogdanov
http://www.cse.cuhk.edu.hk/~andrejb/cs
c3130
Testing membership and parsing
• Given a grammar
S → 0S1 | 1S0S1 | T
T→S|e
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⇒
• How to remove ε -productions (except from S)
S → ACD B C D
A→ a
B →ε
C → ED |
ε
D → BC | b
E →b
Find
all nullable variables N1, ..., Nk
Finding nullable variables
• To find nullable variables, we work backwards
– First, mark all variables A s.t. A → ε as nullable
– Then, as long as there are productions of the form
A marked
where all of A1,…, Ak are → A1…asAknullable, mark A as
nullable
Eliminating ε -productions
S → ACD D →C
A→ a S → AD
B →ε D →B
C → ED | D →ε
ε S → AC
D → BC | b S →A
E →b C →E
nullable variables: B, C, D
A1 → A2 → ... → Ak → A1
delete it and replace everything with A1
• Example
S T
S → 0S1 | 1S0S1
|T S → 0S1 | 1S0S1
|R|ε
T → S S→R|ε
R → 0SR R → 0SR
R
by productions A1 → α ,... , Ak → α
•S
Example
→ 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
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 → ε |ADDA
A →a
C →c
D → bCb
Algorithm 2 for testing
membership
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
ta
ce ble
Input: Grammar G in CNF, string x = x1…xk lls
1k
…
For cells in last row
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 1 s j t k
Put A in cell st