title: Java Build AST from Grammar by using ANTLR
body: <p>So, I'm given an assignment of constructing an AST right from scratch. So far, I've defined the grammar by using ANTLR 4, but since it doesn't support ASTs any longer (deprecated from ANTLR 4), what is the right approach of constructing an AST? I know that I should use the visitors, listeners and actions that ANTLR provides and generates interfaces for, but HOW? Should I use for instance the <code>org.eclipse.jdt.core.dom.AST</code> class with the ASTParser object? Or what exactly? What about the <code>AST Hierarchy</code>? </p>
  
  <p>I am asking this things because in theory everything sounds perfect, but in practice, it's quite difficult.</p>
  
  <p>Here is the ANTLR grammar:</p>
  
  <pre><code>prog    : form+ EOF ;
  
  form    : 'form' ID '{' quest+ stat* decl* '}' ;
  
  quest   : 'question' STRING '=' QuestionLiteral '{' decl stat* expr* '}' ;
  
  stat : value                                // Value, e.g. value = false; value = someX * someY;
   | ifStat                               // If statement
   | ID '=' expr ';'                      // Assignment
   ;
  
  decl : ID ':' primitiveType ';' ;   // The definition of e.g. hasBoughtHouse : boolean -&gt; restricted only to primitive types
  
  expr        : expr EXP&lt;assoc=right&gt; expr                                #Exp
          | expr (MUL | DIV) expr                                     #MulDiv
          | expr (ADD | SUB) expr                                     #AddSub
          | expr (LESS | LESS_EQUAL | GREATER | GREATER_EQUAL) expr   #LessGreater
          | expr ('==' | '!=') expr                                   #Equal_NotEqual
          | expr '&amp;&amp;' expr                                            #LogAnd
          | expr '||' expr                                            #LogOr
          | '(' expr ')'                                              #Par
          | literal                                                   #ExprLit
          ;
  
  
  ifStat      : 'if' '(' expr ')' '{' quest* stat* decl* '}'
          | 'if' '(' expr ')' '{' stat '}' 'else' '(' quest* stat* decl* ')';
  
  value       : ID '.' 'value' '=' expr ';' ; // hasSoldHouse.value = true; hasSoldHouse.value = 2+3; 
  
  literal     : BooleanLiteral
          | NumberLiteral
          | ID
          ;
  
  QuestionLiteral : 'OrdinaryQuestion'
              | 'ComputableQuestion'
              ;
  
  BooleanLiteral  : 'true'
              | 'false'
              ;
  
  NumberLiteral   : (INT | ('(-'INT')'))
              | (FLOAT | ('(-'FLOAT')'))
              | (CURRENCY | ('(-'CURRENCY')'))
              ;
  
  primitiveType   : 'boolean'
              | 'float'
              | 'currency'
              | 'string'
              ;
  
  WS          : (' ' | NL | '\t') -&gt; skip;
  
  ID          : ID_LETTER (ID_LETTER | INT)* ;
  
  /* It gets form, if etc as an identifier and not as keywords */
  
  ID_LETTER   : 'a'..'z' | 'A'..'Z' | '_' ;
  
  INT         : '0' | [1-9] [0-9]*  ; // We cannot use [0-9]+ because this would mean that 01 + 3 would be acceptable
  
  FLOAT       : INT+ '.' INT* // How to set the precision to for instance 4? That it returns a value of this precision
          | '.' INT+;
  
  CURRENCY    : FLOAT;
  
  STRING      : '"'   (ESC|.)*? '"';
  fragment
  ESC         : '\\' | '\\\\' ;
  
  LINE_COMMENT: '//' .*? '\n' -&gt; skip;    // Single line comments
  COMMENT     : '/*' .*? '*/' -&gt; skip;    // Multi line comments
  </code></pre>