title: Parsing Cool Language with antlr, cant print the desired out put
body: <p>I'm a writing a parser/lexer for COOL ( classroom object-oriented language ). u can see the grammar in the following link: ( LAST PAGE OF THE MANUAL )</p>
  
  <p><a href="http://theory.stanford.edu/~aiken/software/cool/cool-manual.pdf" rel="nofollow">http://theory.stanford.edu/~aiken/software/cool/cool-manual.pdf</a></p>
  
  <p>I am using ANTLR to write this program and with the following input, I desire the following output :</p>
  
  <p>input :</p>
  
  <pre><code>class Main inherits IO {
    main(): Object {{
      x &lt;- 2 + 3 *4;
    }};
  };
  </code></pre>
  
  <p>output : </p>
  
  <pre><code>1
  2
  3
  11
  6
  16
  27
  18
  27
  27
  </code></pre>
  
  <p>but the output I am getting is:</p>
  
  <pre><code>1
  2
  3
  11
  6
  27
  16
  27
  18
  27
  </code></pre>
  
  <p>and here is my parser/lexer code:</p>
  
  <pre><code>// parser
  grammar CA2;
  
  program : {System.out.println("1");} (classdef';')+ ;
  classdef : {System.out.println("2");} CLASS ID (INHERITS ID)? '{' (feature';')* '}' ;
  feature : {System.out.println("3");} ID OPENP (formal (','formal)*)? CLOSEP ':' ID '{' expr '}' 
          | {System.out.println("4");} ID ':' ID ( POINTTOLEFT expr )? ;
  formal : {System.out.println("5");} ID ':' ID ;
  expr : {System.out.println("6");} ID POINTTOLEFT expr exprprime
       | {System.out.println("8");} ID OPENP ( expr (','expr)* )? CLOSEP exprprime
       | {System.out.println("9");} IF expr THEN expr ELSE expr FI exprprime
       | {System.out.println("10");} WHILE expr LOOP expr POOL exprprime 
       | {System.out.println("11");} '{' (expr';')+ '}' exprprime 
       | {System.out.println("12");} LET ID ':' ID (POINTTOLEFT expr)? (','ID ':' ID (POINTTOLEFT expr)?)* IN expr exprprime
       | {System.out.println("13");} CASE expr OF (ID POINTTORIGHT expr ';')+ ESAC exprprime
       | {System.out.println("14");} NEW ID exprprime
       | {System.out.println("15");} ISVOID expr exprprime
       /*| {System.out.println("16");} expr ADD expr
       | {System.out.println("17");} expr SUB expr
       | {System.out.println("18");} expr MULTIPLY expr
       | {System.out.println("19");} expr DIV expr
       | {System.out.println("20");} TILDA expr
       | {System.out.println("21");} expr LARGERTHAN expr
       | {System.out.println("22");} expr LARGEREQ expr
       | {System.out.println("23");} expr EQUALS expr
       | {System.out.println("24");} NOT expr
       | {System.out.println("25");} OPENP expr CLOSEP
       | {System.out.println("26");} ID
       | {System.out.println("27");} INTEGER*/
       | {System.out.println("28");} STRING exprprime | mathex exprprime ;
       /*| {System.out.println("29");} TRUE
       | {System.out.println("30");} FALSE ;*/
  exprprime : {System.out.println("7");} (('@'ID)?)'.'ID OPENP (expr (','expr)*)? CLOSEP exprprime | ;
  mathex : b ;
  b : {System.out.println("24");} NOT b | c ;
  cprime : {System.out.println("21");} LARGERTHAN d cprime 
         | {System.out.println("22");} LARGEREQ d cprime 
         | {System.out.println("23");} EQUALS d cprime | ;
  c : d cprime ;
  dprime : {System.out.println("16");} ADD e dprime 
         | {System.out.println("17");} SUB e dprime | ;
  d : e dprime ;
  eprime : {System.out.println("18");} MULTIPLY f eprime 
         | {System.out.println("19");} DIV f eprime | ;
  e : f eprime ;
  f : {System.out.println("20");} TILDA f | g ;
  g : {System.out.println("25");} OPENP mathex CLOSEP 
    | {System.out.println("26");} ID 
    | {System.out.println("27");} INTEGER 
    | {System.out.println("29");} TRUE 
    | {System.out.println("30");} FALSE ;
  
  //lexer
  TRUE : 'true' ;
  FALSE : 'false' ;
  INHERITS : 'inherits' ;
  CLASS : 'class' ;
  IF : 'if' ;
  THEN : 'then' ;
  ELSE : 'else' ;
  FI : 'fi' ;
  WHILE : 'while' ;
  LOOP : 'loop' ;
  POOL : 'pool' ;
  LET : 'let' ;
  IN : 'in' ;
  CASE : 'case' ;
  OF : 'of' ;
  ESAC : 'esac' ;
  NEW : 'new' ;
  ISVOID : 'isvoid' ;
  NOT : 'not' ;
  TILDA : '~' ;
  WHITESPACE : [ ' '|'\r'|'\n'|'\t']+ -&gt;skip ;
  INTEGER : [0-9]+ ;
  ID : ['_'a-zA-Z][a-zA-Z0-9'_']* ;
  ADD : '+' ;
  MULTIPLY : '*' ;
  SUB : '-' ;
  DIV : '/' ;
  OPENP : '(' ;
  CLOSEP : ')' ;
  EQUALS : '=' ;
  LARGERTHAN : '&lt;' ;
  LARGEREQ : '&lt;=' ;
  POINTTOLEFT : '&lt;-' ;
  POINTTORIGHT : '=&gt;' ;
  STRING : '"'(~[\r\n])*'"' ;
  </code></pre>
  
  <p>This is the code version of the COOL grammar in ANTLR. the parts that are commented in the main code are disambiguated ( means are ridden of ambiguity ! ) and left-recursion-freed in the second part ( mathex rule ).</p>
  
  <p>could anyone point out where is this going wrong and why am i not getting the desired output?</p>
  
  <p>thanks in advance!</p>