1. grammar G (S):
(1)S -> AB
(2)A ->Da|ε
(3)B -> cC
(4)C -> aADC |ε
(5)D -> b|ε
Verify grammar G (S) is not LL (1) grammar?
FIRST集
FIRST(AB)={b,a,c}
FIRST(Da)={b,a}
FIRST (e) = {e}
FIRST(cC)={c}
FIRST(aADC)={a}
FIRST(b)={b}
Follow集
Follow(S)={c,b,a}
Follow(A)={a,b,c,#}
Follow(B)={a,b,c}
Follow(C)={#}
Follow(D)={#,a}
Select Set
Sellect(A->Da)={b,a}
Sellect(A->ε)={a,b,c,#}
2. (last job) after the elimination of left recursion whether the expression grammar is LL (1) grammar?
1. SELECT (E '→ + TE') ∩SELECT (E '→ ε) = ∅
SELECT(T'→*FT')∩SELECT(T'→ε)=∅
SELECT(F→(E))∩SELECT(F→i)=∅
So is LL (1) grammar
2. SELECT(A'→ABe)∩SELECT(A'→ε)=∅
SELECT(B'→bB')∩SELECT(B'→ε)=∅
So is LL (1) grammar
3. SELECT(S'→BaS')∩SELECT(S'→ε)=∅
So is LL (1) grammar
4. SELECT(A→a)∩SELECT(A→ε)∩SELECT(A→cA)∩SELECT(A→aA)≠∅
So instead of LL (1) grammar
5. SELECT(S->Ap)∩SELECT(S->Bq)=∅
SELECT(A->a)∩SELECT(A->cA)=∅
SELECT(B->b)∩SELECT(B->dB)=∅
So is LL (1) grammar
3. The connection 2, if it is LL (1) grammar, write its recursive descent parser code.
E()
{T();
E'();
}
E'()
T()
T'()
F()
Source code as follows:
void ParseE(){
switch(lookahead){
case '(','i':
Frset ();
ParseE'();
break;
default:
print("syntax error \n");
exit(0);
}
}
void ParseE'(){
switch(lookahead){
case '+':
Match Token ( '+');
Frset ();
ParseE'();
break;
case ')','#':
break;
default:
print("syntax error \n");
exit(0);
}
}
void ParseT(){
switch(lookahead){
case '(','i':
ParseF();
Frset '();
break;
default:
print("syntax error \n");
exit(0);
}
}
void ParseT'(){
switch(lookahead){
case '*':
Match Token ( '*');
ParseF();
Frset '();
break;
case '+',')','#':
break;
default:
print("syntax error \n");
exit(0);
}
}
void ParseF(){
switch(lookahead){
case '(':
Match Token ( '(');
ParseE();
Match Token ( ')');
break;
case 'i':
Match Token ( 'in');
break;
default:
print("syntax error \n");
exit(0);
}
}