Parser Delimiter Question

Joined
Sep 30, 2008
Messages
1
Reaction score
0
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)

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");
		} 
	    }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top