Parser Delimiter Question

Discussion in 'Java' started by utopia14, Sep 30, 2008.

  1. utopia14

    utopia14

    Joined:
    Sep 30, 2008
    Messages:
    1
    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");
    		} 
    	    }
    }
     
    utopia14, Sep 30, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. john brown
    Replies:
    6
    Views:
    578
    Jim Gibson
    Oct 22, 2003
  2. Mark Fox

    Delimiter Split

    Mark Fox, Aug 11, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    3,344
    Chris R. Timmons
    Aug 11, 2003
  3. Calvin Lai

    Cookies delimiter in ASP.NET

    Calvin Lai, Jan 18, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,426
    Shiv Kumar
    Jan 19, 2004
  4. Kevin Spencer
    Replies:
    5
    Views:
    1,226
    =?Utf-8?B?UENL?=
    Jan 21, 2004
  5. chad
    Replies:
    4
    Views:
    3,829
Loading...

Share This Page