Hey all, I am attempting to implement a recursive descent parser. The problem is that when I enter an equation (i.e. 2 ? 3), I have to put spaces between each of the characters. I tried to use a delimiter to get rid of spaces, but have had issues with that. I would like to be able to ignore spaces (i.e. 2? 3), so if you guys have any suggestions that would be awesome.
By the way, new to the forums...... thanks for the help 8)
By the way, new to the forums...... thanks for the help 8)
Code:
import java.io.*;
import java.util.*;
public class SimpleParser1 {
static StringTokenizer st;
static String curr;
/** read the next token into curr */
static void next() {
try {
curr=st.nextToken().intern();
// use of intern() allows us to check equality with ==.
} catch( NoSuchElementException e) {
curr=null;
}
}
static void error(String msg) {
System.err.println(msg);
System.exit(-1);
}
static int parseD() {
int x=parseE();
return parseD1(x);
}
static int parseD1(int x) {
if (curr=="@") {
next();
return parseD1((x * x +11)%(x+3));
} else if(curr==")" || curr=="$") {
return x;
} else {
error("Unexpected :"+curr);
return x; // to make compiler happy
}
}
static int parseE() {
// E -> T E1
int x=parseT();
return parseE1(x);
}
static int parseE1(int x) {
// E1 -> T E1 | epsilon
if (curr=="?") {
next();
int y = parseT();
return parseE1(max(x,y));
} else if(curr==")" || curr=="$" || curr=="@" ) {
return x;
} else {
error("Unexpected :"+curr);
return x; // to make compiler happy
}
}
static int parseT() {
// T -> F T1
int x=parseF();
return parseT1(x);
}
static int parseT1(int x) {
// T1 -> * F T1 | epsilon
if (curr=="@@") {
next();
int y=parseF();
x = x + 27;
y = y*6;
return parseT1(gcd(x,y));
} else if(curr=="?" || curr==")" || curr=="$" || curr=="@") {
return x;
} else {
error("Unexpected :"+curr);
return x; // to make compiler happy
}
}
static int parseF() {
// F -> ( E ) | a
if( curr=="(") {
next();
int x=parseD();
if(curr==")") {
next();
return x;
} else {
error (") expected.");
return -1; // to make compiler happy
}
} else try {
int x=Integer.valueOf(curr).intValue();
next();
return x;
} catch(NumberFormatException e) {
error("Number expected.");
return -1; // to make compiler happy
}
}
static int max(int x, int y)
{
if((x+19) > (2 * y + 6))
return (x+19);
else
return (2 * y + 6);
}
static int gcd(int x, int y)
{
if (y==0)
{
return x;
}
else
return gcd(y, x%y);
}
public static void main(String args []) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader (System.in));
String line=in.readLine();
st = new StringTokenizer(line, " ");
next();
int x=parseD();
if(curr=="$") {
System.out.println("OK "+x);
} else {
error("End expected");
}
}
}