Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone help me add the code to make my project work? I don't understand how to make it work. The information I have posted

Can someone help me add the code to make my project work? I don't understand how to make it work. The information I have posted is the code that my professor gave us. We just have to add the code in the two sections I've labled below to make it work. I labled it FIX MODULO OPERATOR HERE and FIX POWER OPERATOR HERE I've add the global constants down below the code import java.io.*; import java.util.*; /***************************************************************************** * COURSE CSC 321 * * Spring 2021 * * Recursive descent parsing for arithmetic operations * * Driver for a very simple Recursive Desent Parser for Arthmetic statements * * This program illustrates recursive descent parsing for arithmetic operations * * The grammar: * * S => { A; } . * A => I = E * E => T | T (+|-) E * T => F | F (*|/) T * F => P | P * P => I | N |( E ) * I => letter{letter|digit} * N => digit {digit} * * note: it does not correctly handle '%' or '^' * that needs to be fixed! * */ public class Assignment extends GlobalConstants { public static void main(String args[]) { // command line arguments (args) if (args.length != 0) { System.out.println("Usage: java "); System.exit(1); } System.out.println("This program illustrates recursive descent parsing"); System.out.println(" for simple arithmetic expressions "); System.out.println(" S => { A ; } . "); System.out.println(" A => I = E "); System.out.println(" E => T | T (+|-) E "); System.out.println(" T => F | F (*|/|%) T "); System.out.println(" F => P | P ^ F "); System.out.println(" P => I | N | ( E ) "); System.out.println(" I => letter{letter|digit} "); System.out.println(" N => digit {digit} "); System.out.println(" missing mod(%) and pow(^) functionality - you need to fix this"); System.out.println(" Enter a statement: "); // Create a parser object parser parser = new parser(new BufferedReader(new InputStreamReader(System.in))); // start the parsing with STATEMENT parser.start(); // call's statement(); } // main } // class assignment /* A Simple Recursive Descent Syntactical Analyzer uses class Scanner */ class parser extends GlobalConstants { private Scanner scanner; public parser(BufferedReader br) { scanner = new Scanner(br); } public parser(Scanner scanner) { this.scanner = scanner; } public void start() { scanner.getToken(); // get first token S(); // call statement } // // S -> { A ; } . public void S() { while (token.type != PERIOD) { int value = 0; Token id = token; // save current indentifer if (token.type != IDENT) { scanner.error("Expecting Identifier or period found: " + token.toString()); System.exit(1); } value = A(); // call Expression System.out.println(" " + id.name + " = " + value); if (token.type == PERIOD) break; // if we have a PERIOD we done if (token.type == SEMICOLON) { scanner.getToken(); // else get next token } else { scanner.error("Expecting operator, 'semicolon', or 'period' found: " + token.toString()); break; } } // while } // statement // => = // A -> I = E private int A() { Token lhs = token; scanner.getToken(); if (token.type != ASSIGN_OP) { scanner.error("Expecting assignment operator '=' found: " + token.toString()); } scanner.getToken(); int rhs = E(); lhs.value = rhs; // update token in SymbolTable symtab.update(lhs); /** debug * */ // symtab.print(); return lhs.value; } // assignment // // E => T | T (+|-) E private int E() { int left = 0; left = T(); // call Term save valeu (left-hand-side of operation) // if we have an addition operation - we have work to do if (token.type == ADD_OP || token.type == SUB_OP) { int saveOp = token.type; // save operation - well need it later scanner.getToken(); // get next token switch (saveOp) { // + or - case ADD_OP: left += E(); // call Expression (get right-hadn-side) and add it left break; case SUB_OP: left -= E(); // call Expression (get right-hadn-side) and sub it from left break; default: // if current token is not + or - syntax error? scanner.error("Expecting a '+' or '-' operator"); } // switch } // if return left; // return current value } // expression // // T => F | T (*|/|%) T private int T() { int left = 0; // left-hand-side or operation left = F(); // call Factor and save return value // if token is * | / | % then if (token.type == MULT_OP || token.type == DIV_OP || token.type == MOD_OP) { int saveOp = token.type; // save operation - we'll need it scanner.getToken(); // get next token switch (saveOp) { // do operation case MULT_OP: left *= T(); // left = left * return value from Term break; case DIV_OP: int divisor = T(); // tmporary kluge if (divisor == 0) { // - divisor can't be zero divisor = 1; // - System.err.println("divide by zero avoided"); } // left /= divisor; // left = left / return value from Term break; case MOD_OP: /* FIX MODULO OPERATOR HERE */ System.out.println(" MODULO operator NOT implemented - maybe you could do that "); left = 42; // MEANIG OF LIFE - hitchhiker's guide to the galaxy break; default: scanner.error("Expecting a '*' or '/' operator"); } // switch } // if return left; } // term // // F => P | P ^ F private int F() { int left = 0, value = 0; left = value = P(); // left-hand-side set to be return value from Primary if (token.type == POW_OP) { // if token is ^ (power) scanner.getToken(); // get next token /* FIX POWER OPERATOR HERE */ System.out.println(" POWER operator NOT implemented - maybe you could do that "); left = 42; // // MEANIG OF LIFE - hitchhiker's guide to the galaxy // use java's (int) Math.pow( (double) foo, (double) bar ); } return value; } // primary // // P = I | N |( E ) private int P() { int value = 0; switch (token.type) { case INT_LIT: // if token integer literal value = scanner.number(); // save its value scanner.getToken(); // get next token break; case IDENT: value = token.value; // value set to id's value (if any) Token save = token; // if identifier - save token scanner.getToken(); // get next token break; case LEFT_PAREN: // if current token is ( we have ( ) scanner.getToken(); // call Expression save return value value = E(); if (token.type != RIGHT_PAREN) // if token not ) - syntac error scanner.error("Missing ')'"); scanner.getToken(); break; default: // token was not or ) scanner.error("Expecting number, identifier, or ( found: " + token.toString()); break; // printout message and exit } // switch return value; } // primary } // class Parser
import java.io.*; import java.util.*; /***************************************************************************** * COURSE CSC 321 * Spring 2021 * * Recursive descent parsing for arithmetic operations * * Globals for a simple parser (recursive descent) * * There are better ways of making these avalible but they are * (but that seem cumbersome to an old C programmer or maybe I am just lazy) * * This program illustrates recursive descent parsing for arithmetic operations * * The grammar: * * S => { A; } . * A => I = E * E => T | T (+|-) E * T => F | F (*|/) T * F => P | P * P => I | N |( E ) * I => letter{letter|digit} * N => digit {digit} * * note: it does not correctly handle '%' or '^' * that needs to be fixed! * */ class GlobalConstants { public static Token token; // global token - everone uses // table of symbols store indent public static SymbolTable symtab = new SymbolTable(); // array of strings used in displaying static String spelling[] = { "unknown", "Int", "Ident", "Assign", "Add", "Sub", "Mod", "Mul", "Div", "Pow", "LParn", "RParn", "Period", "Semicolon" }; static String abbreviation[] = { "0", "int", "ID", "=", "+", "-", "%", "*", "/", "^", "(", ")", ".", ";" }; // int values for each token static final int UNKNOWN = 0, INT_LIT = 1, IDENT = 2, ASSIGN_OP = 3, ADD_OP = 4, SUB_OP = 5, MOD_OP = 6, MULT_OP = 7, DIV_OP = 8, POW_OP = 9, LEFT_PAREN = 10, RIGHT_PAREN = 11, PERIOD = 12, SEMICOLON = 13; // enumeration for all the types of tokens // C enums would be better enum Type { UNKNOWN, INT_LIT, IDENT, ASSIGN_OP, ADD_OP, SUB_OP, MOD_OP, MULT_OP, DIV_OP, POW_OP, LEFT_PAREN, RIGHT_PAREN, PERIOD, SEMICOLON; public static Type fromInt(int x) { switch (x) { case 0: return UNKNOWN; case 1: return INT_LIT; case 2: return IDENT; case 3: return ASSIGN_OP; case 4: return ADD_OP; case 5: return SUB_OP; case 6: return MOD_OP; case 7: return MULT_OP; case 8: return DIV_OP; case 9: return POW_OP; case 10: return LEFT_PAREN; case 11: return RIGHT_PAREN; case 12: return PERIOD; case 13: return SEMICOLON; } return null; } } }

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

Students also viewed these Databases questions

Question

2. What are your challenges in the creative process?

Answered: 1 week ago