Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

= | + | - = | * | / = | () = A | B | C | D | ... | Z

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Experts Guide To Database 2

Authors: Bruce L. Larson

1st Edition

0070232679, 978-0070232679

More Books

Students also viewed these Databases questions