I have the following context free grammar:
E = (E)
E = i | ε
Given an input String
, I have to determine whether this String is accepted by this grammar or not, with a recursive syntax analyzer. For example, if I have the input:
((i))<- this is valid
(((i))))<- this is invalid
()<- this is valid
and I have the code that is supposed to do all of these
public static boolean E() {
int pOpen;
pOpen = 0;
if (lexico.equals("(")) {
pOpen++;
E();
} else if (lexico.equals("i")) {
if (pOpen == 0)
return true; //this is valid
else
verifyParenthesis();
}
}
public static boolean verifyParenthesis() {
int pClose = 0;
while ((lexico = nextSymbol()).equals(")"))
pClose++;
}
But I am not sure how to verify that the number of open parentheses (
is the same as the number of close parentheses )
.
Do I have to use a while
on the verifyParenthesis method?