Question: It is a simple program that that parses a simple expression grammar. The grammar: P ---> E '$' E ---> T { ('+' '-') T

It is a simple program that that parses a simple expression grammar.

The grammar:

P ---> E '$'

E ---> T { ('+' '-') T }

T ---> F { ('/' '*') F }

F ---> '(' E ')' | digit | letter or in BNF

P ---> E '$'

E ---> E + T | E - T | T

T ---> T * F | T / F | F

F ---> '(' E ')' | digit | letter

Your task is to modify the code to add exponentiation and modulus operations

To add these functions we will change the Grammar to be the following:

P ---> E '$'

E ---> E + T | E - T | T

T ---> T * F | T / F | F

F ---> P ^ F | P

P ---> '(' E ')' | digit | letter

The grammars are very similar but the new grammar but has a a minor change to TERM for modulus a rewrite of FACTOR to handle exponent operation and a completely new method called PRIMARY that does what the old FACTOR did

To add the modulus you will need to add the modulus operator '%' character

To add the power (exponentiation) operation you will need to again add a character ('^') and then rewrite the FACTOR NEW method for PRIMARY Comment your code. You should add a comment ALL the line you add or modify (required) You must use the given code and you are to change as little as possible (don't rewrite the whole thing or add any additional functionality)

______________________________________________________________________________________________________________________________________

/* super simple recursive descent parser for the following grammar P ---> E '$' E ---> T { ('+' '-') T } T ---> F { ('/' '*') F } F ---> '(' E ')' | digit | letter Note, this parser does not calculate values or generate any code It only parses the expression - arises that it is grammatical */ import java.util.*; import java.io.*; public class rd { inputBuffer buffer = new inputBuffer(); char c;; public static void main(String argv[]){ System.out.print(" Enter expression with + - * / % () "); System.out.print(" ending an expression with ';' "); System.out.print(" Terminate program with End of File \t"); rd go = new rd(); while (true) go.P(); } void P(){ scan(); E(); if(c == ';') System.out.print(" accepted \t"); } void E(){ T(); while ( isin(c, "+-" )) { scan(); T(); } } void T(){ F(); while ( isin(c, "*/" ) ) { scan(); F(); } } void F() { if(Character.isDigit(c) || Character.isLetter(c)) scan(); else if (c == '(') { scan(); E(); if(c == ')') scan(); else error("expected )"); } else error("found '"+c+"' expected letter or digit"); } void error(String msg){ System.out.println(msg); System.exit(1); } void scan(){ c = buffer.get( ); while (Character.isWhitespace(c)) { c = buffer.get( ); } } boolean isin(char c, String set){ return set.indexOf(c)!=-1? true : false; } class inputBuffer { private String line = ""; private int column = 0,lineNo = 0; private BufferedReader in; public inputBuffer () { in = new BufferedReader(new InputStreamReader(System.in)); } public char get ( ) { if (++column >= line.length()) { try { line = in.readLine( ); } catch (Exception e) { error("Invalid read operation"); } if (line == null) error("bye"); column = 0; lineNo++; line = line + " "; } return line.charAt(column); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!