Midterm2 Spring2021 AnthonyLe
Midterm2 Spring2021 AnthonyLe
2. Given the following productions, and the string w = ‘abab’. Is this grammar ambiguous
and if so, explain ambiguity and show why? (30)
3. Remove all left recursions and backtracking in the following productions if needed. (30)
1)E −> EaE 2)E−> F 3)F−> mE 4)F−> Tde 5) F −> Tfg 6) T −> id
E->FE’ | F
E’-> aEE’ | ε
F-> mE | TF’
F’ -> de | fg
T-> id
4. Define the First and Follow sets for each non-terminal symbol (30)
D −> ONE
O −> xy | b | ε
N −> Ez | cd | ε
E −> m | fg | ε
5. Given the following productions, construct the parsing table for table driven predictive
parser - it is a top-down parser. (30)
1) S −> S & C 2) S −> S @ C 3) S −> C 4) C −> x
S-> CS’
S’-> &CS’ | @CS’ | ε
C->x
6. Given the following production rules, write a recursive descent function that returns a
boolean value for the productions Q. Write the function using syntactically correct C, C++,
C#, python (for 20 points maximum) or pseudo code (15 points). Use any of the pre-existing
functions lexer(), getNextChar(), currentChar(), first(), follow(), token(), backup(), error()
or match() only if needed. (20 points total)
while (true){
if (tokens[i] == qStack.top()) {
i++; //the lexer iteration
qStack.pop();
}
else if (qStack.top == “$”){
return true;
}
else if (qStack.top == “E”) {
qStack.pop();
qStack.push(Q);
qStack.push(T);
}
else if (qStack.top == “Q”) {
qStack.pop();
if (tokens[i] == “#”) {
qStack.push(Q);
qStack.push(T);
qStack.push(#);
}
else {
//epsilon, do nothing
}
}
else if (qStack.top == “T”) {
qStack.pop();
if (type(tokens[i]) == id) {
qStack.push(tokens[i]);
}
else {
return false;
}
}
else {
return false;
}
}
}
7. Given the following predictive parsing table, parse the string “ {xyx} ”
to see if it accepted. (30 points)
x y { } $
S CB CB
B yCB ε ε
C x {S}