java - I don't know how to implement a recursive syntax analyzer -
i have next context free grammar:
e = (e) e = | ε
given input string
, have determine whether string accepted grammar or not, recursive syntax analyzer. example, if have input:
((i))<- valid (((i))))<- invalid ()<- valid
and have code supposed of these
public static boolean e() { int popen; popen = 0; if (lexico.equals("(")) { popen++; e(); } else if (lexico.equals("i")) { if (popen == 0) homecoming true; //this valid else verifyparenthesis(); } } public static boolean verifyparenthesis() { int pclose = 0; while ((lexico = nextsymbol()).equals(")")) pclose++; }
but not sure how verify number of open parentheses (
same number of close parentheses )
.
do have utilize while
on verifyparenthesis method?
recursive with. enjoy.
public static boolean expressioniscorrect(string expr) { if(!expr.contains("(") && !expr.contains(")")) { homecoming true; } int indexofleft = -1; int indexofright = -1; indexofleft = expr.indexof("("); indexofright = expr.lastindexof(")"); if (indexofleft>=indexofright) { homecoming false; } homecoming expressioniscorrect(expr.substring(indexofleft+1, indexofright)); }
don't hesitate inquire question if don't understand what's going on, seek first.
java parsing context-free-grammar
No comments:
Post a Comment