title: Understanding the context data structure in Antlr4
body: <p>I'm trying to write a code translator in Java with the help of Antlr4 and had great success with the grammar part so far. However I'm now banging my head against a wall wrapping my mind around the parse tree data structure that I need to work on after my input has been parsed. </p>
  
  <p>I'm trying to use the visitor template to go over my parse tree. I'll show you an example to illustrate the points of my confusion.</p>
  
  <p>My grammar:</p>
  
  <pre><code>grammar pqlc;
  
  // Lexer
  
  //Schlüsselwörter
  EXISTS: 'exists';
  REDUCE: 'reduce';
  QUERY: 'query';
  INT: 'int';
  DOUBLE: 'double';
  CONST: 'const';
  STDVECTOR: 'std::vector';
  STDMAP: 'std::map';
  STDSET: 'std::set';
  C_EXPR: 'c_expr';
  
  INTEGER_LITERAL  : (DIGIT)+ ;
  fragment DIGIT: '0'..'9';
  DOUBLE_LITERAL : DIGIT '.' DIGIT+;
  
  LPAREN          : '(';
  RPAREN          : ')';
  LBRACK          : '[';
  RBRACK          : ']';
  DOT             : '.';
  EQUAL           : '==';
  LE              : '&lt;=';
  GE              : '&gt;=';
  GT              : '&gt;';
  LT              : '&lt;';
  ADD             : '+';
  MUL             : '*';
  AND             : '&amp;&amp;';
  COLON           : ':';
  
  IDENTIFIER    :   JavaLetter JavaLetterOrDigit*;
  fragment JavaLetter    :   [a-zA-Z$_]; // these are the "java letters" below 0xFF
  fragment JavaLetterOrDigit    :   [a-zA-Z0-9$_]; // these are the "java letters or digits" below 0xFF
  WS  
      :  [ \t\r\n\u000C]+ -&gt; skip  
      ;
  COMMENT
      :   '/*' .*? '*/' -&gt; skip
      ;
  
  LINE_COMMENT
      :   '//' ~[\r\n]* -&gt; skip
      ;
  
  
  // Parser
  
  //start_rule: query;
  
  query :
        quant_expr
        | qexpr+
        | IDENTIFIER // order IDENTIFIER and qexpr+?
        | numeral
        | c_expr //TODO
  
        ;
  
  c_type : INT | DOUBLE | CONST;
  bin_op: AND | ADD | MUL | EQUAL | LT | GT | LE| GE;
  
  
  qexpr:
           LPAREN query RPAREN bin_op_query? 
           // query bin_op query
           | IDENTIFIER  bin_op_query? // copied from query to resolve left recursion problem
           | numeral bin_op_query?  // ^
           | quant_expr bin_op_query? // ^
             |c_expr bin_op_query?
             // query.find(query)
           | IDENTIFIER  find_query? // copied from query to resolve left recursion problem
           | numeral find_query?  // ^
           | quant_expr find_query?
             |c_expr find_query?
             // query[query]
            | IDENTIFIER  array_query? // copied from query to resolve left recursion problem
           | numeral array_query?  // ^
           | quant_expr array_query?
             |c_expr array_query?
  
       // | qexpr bin_op_query // bad, resolved by quexpr+ in query 
       ;
  
  bin_op_query: bin_op query bin_op_query?; // resolve left recursion of query bin_op query
  
  find_query: '.''find' LPAREN query RPAREN;
  array_query: LBRACK query RBRACK;
  
  quant_expr:
      quant id ':' query
            | QUERY LPAREN match RPAREN ':' query
            | REDUCE LPAREN IDENTIFIER RPAREN id ':' query
      ;
  
  match:
           STDVECTOR LBRACK id RBRACK EQUAL cm
       | STDMAP '.''find' LPAREN cm RPAREN EQUAL cm
       | STDSET '.''find' LPAREN cm RPAREN
       ;
  
  cm:
      IDENTIFIER
    | numeral
     | c_expr //TODO
    ;
  
  quant :
            EXISTS;
  
  id :
       c_type IDENTIFIER
       | IDENTIFIER // Nach Seite 2 aber nicht der Übersicht. Laut übersicht id -&gt; aber dann wäre Regel 1 ohne +
     ;
  
  numeral :
              INTEGER_LITERAL
          | DOUBLE_LITERAL
          ;
  c_expr:
            C_EXPR
        ;
  </code></pre>
  
  <p>Now let's parse the following string:</p>
  
  <pre><code>double x: x &gt;= c_expr
  </code></pre>
  
  <p>Visually I'll get this tree: 
  <img src="https://puu.sh/9qeIP/d90aee077e.png" alt="tree"></p>
  
  <p>Let's say my visitor is in the <code>visitQexpr(@NotNull pqlcParser.QexprContext ctx)</code> routine when it hits the branch Qexpr(x bin_op_query).</p>
  
  <p>My question is, how can I tell that the left children ("x" in the tree) is a terminal node, or more specifically an "IDENTIFIER"? There are no visiting rules for Terminal nodes since they aren't rules. 
  <code>ctx.getChild(0)</code> has no RuleIndex. I guess I could use that to check if I'm in a terminal or not, but that still wouldn't tell me if I was in IDENTIFIER or another kind of terminal token. I need to be able to tell the difference somehow.</p>
  
  <p>I had more questions but in the time it took me to write the explanation I forgot them :&lt; 
  Thanks in advance.</p>