Parser Delimiter Question

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

  1. utopia14

    utopia14

    Joined:
    Sep 30, 2008
    Messages:
    1
    Likes Received:
    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 (Text):

                                                                                                           
    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. Advertisements

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. Bernd Oninger
    Replies:
    0
    Views:
    928
    Bernd Oninger
    Jun 9, 2004
  2. ZOCOR

    XML Parser VS HTML Parser

    ZOCOR, Oct 3, 2004, in forum: Java
    Replies:
    11
    Views:
    1,058
    Paul King
    Oct 5, 2004
  3. Bernd Oninger
    Replies:
    0
    Views:
    947
    Bernd Oninger
    Jun 9, 2004
  4. Joel Hedlund
    Replies:
    2
    Views:
    747
    Joel Hedlund
    Nov 11, 2006
  5. Joel Hedlund
    Replies:
    0
    Views:
    436
    Joel Hedlund
    Nov 11, 2006
  6. Robert
    Replies:
    1
    Views:
    690
    Puppet_Sock
    Apr 14, 2008
  7. Zach Dennis

    HTML-Parser / SGML-Parser

    Zach Dennis, Oct 1, 2003, in forum: Ruby
    Replies:
    5
    Views:
    733
    Bernard Delmée
    Oct 1, 2003
  8. arne
    Replies:
    0
    Views:
    492
Loading...