I’m trying to Parse a Fully Parenthesized Exp for this grammar:
exp->(exp + exp)
|(exp - exp)
| num
num->[0-9]
…but I have a problem: when I enter “1+4” no error appears. How can I solve it ??
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is a classic problem of recursive descent parsers: your grammar does not require that the entire input is consumed. Once it finishes with the “while isdigit” loop, it considers its job done, ignoring the
+4portion of the input.Add a check that the end of line is reached after the call of the top-level expression to address this problem:
You need to modify
Matchto allow matching\nwhen no additional input is available: