Question
Question 1) Write a Java Recursion Descent Parser: Below is a grammar that represents a restricted form of an infix expression. The restriction is that
Question 1) Write a Java Recursion Descent Parser:
Below is a grammar that represents a restricted form of an infix expression. The restriction is that unless you use parenthesis for clarification, you can only have one '+' or '-' per expression. In addition, '*' and/or '/' may not appear consecutively (A/B*C) -- note that A/B+C/D is valid.
Thus A+B-C is not valid. Similarly, A/B*C is not valid. Note that A+(B-C) is fine, as is (A/B)*C.
Here is some pseudo-code to aid you in construction of your algorithm:
FIND AN EXPRESSION
- Find a term
- if (next symbol is a + or a -)
- Find a term
FIND A TERM
- Find a factor
- if (next symbol is a * or a /)
- Find a factor
FIND A FACTOR
- if (the first symbol is a letter)
- Done
- else if (the first symbol is a '(' )
- Find an expression starting at character after '('
- Check for ')'
- else
- No factor exists
------
Example input/output:
A+B-C A+B*C A/B+C (A+B)-C (A) (A-B-C)
Your program output should be as follows:
A+B-C is NOT Valid A+B*C is Valid A/B+C is Valid (A+B)-C is Valid (A) is Valid (A-B-C) is NOT Valid
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started