I have an assignment in school which tells us to create a Top-Down Parser in Java which follows this grammar:
assign = id , '=' , expr , ';' ;
expr = term , [ ( ’+’ | ’-’ ) , expr ] ;
term = factor , [ ( ’*’ | ’/’) , term] ;
factor = int | ’(’ , expr , ’)’ ;
I think I have understood the basics concepts of parsing, such as "if we have an id, check if the next token is a '=' and the next is an expr, and if the next after that is a ';'". Correct?
Now, if I want to check if the incoming input is an expression:
I check the tokens to see if a term is there, then if a "+ token" OR "- token" is there, and finally if an "expr" is there. But, if I check if there's an "expr" there, it's going to loop, and eventually check again if there's an "expr", and again, and again.
I don't see how I can get that too work? Can someone help me?
Kind regards,