Home / Expert Answers / Computer Science / please-complete-missing-java-code-to-match-grammar-import-tokenizer-e-or-pa560

(Solved): PLEASE COMPLETE MISSING JAVA CODE TO MATCH GRAMMAR. /* */ import tokenizer.*; /** E ::= Or ( ...



PLEASE COMPLETE MISSING JAVA CODE TO MATCH GRAMMAR.

/*

*/

import tokenizer.*;


/**
E ::= Or ( '||' Or )* // left associative
Or ::= Eq ( '&&' Eq )* // left associative
Eq ::= R ( ( '==' | '!=' ) R )* // left associative
R ::= A ( ( '<' | '<=' | '>' | '>=' ) A)* // left associative
A ::= T ( [ '+' | '-' ] T )* // left associative
T ::= F ( [ '*' | '/' | '%' ] F )* // left associative
F ::= ( '+' | '-' | '!' ) F // unary plus, minus, not operators, right associative
| B [ '**' F ] // binary exponentiation operator, right associative
B ::= '(' E ')'
| N
N ::= N D
| N
D ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
*/
public class Hw2_Parse2Value
{
public static int eval(String str)
{
// Tokenize the input string.
final Tokenizer tokens = new Tokenizer(str);
// Parse the list of tokens.
final int result = getE(tokens);
// There shouldn't be any remaining tokens.
if ( ! tokens.isEmpty() )
{
throw new ParseError("unexpected tokens, found " + tokens.peek());
}
return result;
}


/**
E ::= Or ( || Or )*

This is not a short-circuited "or" operation.
*/
public static int getE(Tokenizer tokens)
{

return getOr(tokens); // stub
}


/**
Or ::= Eq ( && Eq )*

This is not a short-circuited "and" operation.
*/
private static int getOr(Tokenizer tokens)
{

return getEq(tokens); // stub
}


/**
Eq ::= R ( ( '==' | '!=' ) R )*
*/
private static int getEq(Tokenizer tokens)
{

return getR(tokens); // stub
}


/**
R ::= A ( ( '<' | '<=' | '>' | '>=' ) A)*
*/
private static int getR(Tokenizer tokens)
{

return getA(tokens); // stub
}


/**
A ::= T ( [ '+' | '-' ] T )*
*/
private static int getA(Tokenizer tokens)
{


return getT(tokens); // stub
}


/**
T ::= F ( [ '*' | '/' | '%' ] F )*
*/
private static int getT(Tokenizer tokens)
{

return getF(tokens); // stub
}


/**
F ::= ( '+' | '-' | '!' ) F // unary plus, minus, not operators
| B [ '**' F ] // binary exponentiation operator
*/
private static int getF(Tokenizer tokens)
{

return getB(tokens); // stub
}


/**
B ::= '(' E ')'
| N
*/
private static int getB(Tokenizer tokens)
{


return getN(tokens); // stub
}


/**
N ::= D ( D )*
*/
private static int getN(Tokenizer tokens)
{
int result = getD(tokens);
while ( ! tokens.isEmpty()
&& Character.isDigit(tokens.peek().lexeme.charAt(0)) )
{
int d = getD(tokens);
result = (10 * result) + d;
}
return result;
}


/**
D ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
*/
private static int getD(Tokenizer tokens)
{
final Token tk = tokens.next();

if ( ! Character.isDigit(tk.lexeme.charAt(0)) )
throw new TokenizeError("expected digit, found " + tk);

return Integer.parseInt(tk.lexeme);
}


public static void main(String[] args)
{
String[] expressions = {
"65432100",
"0 || 1",
"1 || 0 || 1",
"0 && 1",
"1 && 0 && 1",
"0 || 1",
"1 || 0 && 1 || 1",
"4 == 5",
"4 != 5",
" 1 == 2 != 3 == 4",
"4 < 5",
"4 > 5 < 6 == 7 != 0",
"2 + 3",
"2 + 3 * 4",
"6 == 2 * 3",
"!5",
"!0",
"!-15",
"-!250",
"-!0",
"2**29",
"(1 + 1)**(4 * 5)",
"(2<5) == (3-2)",
"1 + 5 || 0 == 2 - 1",
"(! 1+1)==1",
"+(-5)",
"-(+5)",
"+-(-+5)",
"(+2 + -3)",
"-(4 + 3) / -2",
"-2 * -(3 + 4)",
"(2 + 3) * -(4 / 2)",
"-10 / (1 + -(2 * -3))",
"-(-(-2 + -3) * -4) + -5",
"-2 * -(-3 + -4) / +2",
"-10 / -(+(-1 - -2)) * -3",
"2 - (3 * 4) + 5",
"2 * (3 + 4) - 5",
"2 * (3 + 4) - -5",
"-(2 + 3 + 4) - 5",
// error cases
"2 @ 3",
"2 = 3",
"(2 + 3(",
"2 + -(3 * 4)) + 5",
"2 + -(-(3 * 4) + 5"
// test for short circuiting
// "1 || (1 / 0)",
// "0 && (1 / 0)",
};
int i = 0;
for (final String str : expressions)
{
try
{
System.out.print("Parsing exp "+i+": " + str);
final int r = eval( str );
System.out.println( " ... Parsed" );
// Print the result.
System.out.println( r + " = " + str + "\n" );
}
catch (TokenizeError e)
{
System.out.println( " ... Failed (" + e + ")" );
}
catch (ParseError e)
{
System.out.println( " ... Failed (" + e + ")" );
}
++i;
}
}
}




We have an Answer from Expert

View Expert Answer

Expert Answer


We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe