title: writing parser of a simple language
body: <p>I'm trying to design a simple language is similar to lips, schema. I have written its lexer(tokenizer). I can seperate into operators, identifiers etc. But I try now writing parser. For this, just one example is enough for me. <em>Can someone give me an example in java code just for the example?</em> Besides, everyone mentions of usage antlr. Can someone who attempts to use antlr show an example using my example in this question?</p>
  
  <ul>
  <li>– <code>EXP -&gt; EXPI | EXPB</code> </li>
  <li>– <code>EXPI -&gt; (+ EXPI EXPI) | (- EXPI EXPI) | (* EXPI EXPI) | (/ EXPI EXPI) | Id | IntegerValue | (Id EXPLIST)</code> </li>
  <li>–  <code>EXPB -&gt; (and EXPB EXPB)</code></li>
  </ul>
  
  <p>if my input is <code>(+ 1 2)</code>, I hope to get as output</p>
  
  <pre><code> - START-&gt; INPUT
   - -&gt; EXP
   - -&gt; EXPI
   - -&gt; (+ EXPI EXPI)
   - -&gt; (+ EXPI Id)
   - -&gt; (+ Id Id)
  </code></pre>
  
  <p>It is about LR(shift-reduce parsing). I have a simple example that I don't know whether I can modify the code or not for the parsing. But, a point catches my attention, stack usage is proper for LR algorithm. Doesn't it ?</p>
  
  <pre><code>import java.util.Stack;
  
  public class SmallLisp {
  
      Stack&lt;String&gt; stack;
  
      public SmallLisp(){
          String[] tokens = new String[]{"(","+","2","(","+","3","2",")",")",};
          stack = new Stack&lt;String&gt;();
          for (int i=0;i&lt;tokens.length;i++){
              stack.push(tokens[i]);
              if(tokens[i].equals(")")) Interprete();
          }
      }
  
      public void Interprete(){
          String tok;
          Stack&lt;String&gt; callStack = new Stack&lt;String&gt;();
          tok = stack.pop(); /* This is the ) character */
          while(!(tok=stack.pop()).equals("(")){
              callStack.push(tok);
          }
          Call(callStack);
      }
  
      public void Call(Stack&lt;String&gt; callStack){
          String func = callStack.pop(); /* This is the operator or function */
          if(func.equals("+")) {
              double result = Plus(callStack);
              stack.push(String.valueOf(result));
          }
          //if(func.equals("-")) Minus(callStack);
      }
  
      public double Plus(Stack&lt;String&gt; callStack){
          double a = Double.parseDouble(callStack.pop());
          double b = Double.parseDouble(callStack.pop());
          System.out.println("Answer is "+(a+b));
          return(a+b);
      }
  
      public static void main(String[] args) {
          new SmallLisp();
      }
  }
  </code></pre>