Consider the following BNF grammar: EXP ::= EXP + TERM | EXP - TERM | TERM TERM
ID: 3847470 • Letter: C
Question
Consider the following BNF grammar:
EXP ::= EXP + TERM | EXP - TERM | TERM
TERM ::= TERM * FACTOR | TERM / FACTOR | FACTOR
FACTOR ::= ( EXP ) | DIGIT
DIGIT ::= 0 | 1 | 2 | 3
Write a recursive descent recognizer pseudocode with:
- Ask the user for an input stream.
- Report "legal" or "errors found", not both.
- Assume the input stream is the token stream.
- Assume the input stream terminates with a $.
- Assume there is no white space.
- Use a form to collect input and return the output.
- Test your recognizer with illegal and legal strings.
- Give a brief description of this recognizer for the user on how to use and what method that it based on.
Explanation / Answer
procedure exp() term() if token == ‘+’ match(‘+’) term() elseif token == ‘-‘ match(‘-‘) term() else break procedure term() factor() if token == ‘*’ match(‘*’) factor() elseif token == ‘/’ match(‘/’) factor() else break procedure factor() if token == ‘(‘ match(‘(‘) exp() match(‘)’) else digit() procedure digit() if token == ‘0’ match(‘0’) elseif token == ‘1’ match(‘1’) elseif token == ‘2’ match(‘2’) else match(‘3’) match(t) if token == t advancetokenpointer else error
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.