title: How to implement error handling in ANTLR4
body: <p>I have the following grammar for parsing first-order logic formulas applied over graphs:</p>
  
  <pre><code>grammar Graph;
  
  
  /*------------------------------------------------------------------
  * PARSER RULES
  *------------------------------------------------------------------*/
  
  input
  : 
  formula EOF
  ;
  
  formula
  :
  TRUE 
  | FALSE 
  | formula AND formula
  | formula OR formula
  | quantifier formula
  | ST condition
  ;
  
  
  condition
  :
  atom EQUALS QUOTE? (assignment | atom) QUOTE?
  ;
  
  quantifier 
  :
  (FOREACH | EXISTS) variable IN domain
  ;
  
  domain
  :
  (GRAPH_A | GRAPH_B)
  ;
  
  atom
  :
  variable DOT property
  ;
  
  
  variable
  :   
  (nodev | edgev)
  ;
  
  
  nodev
  :
  (NODE | NODE1)
  ;
  
  edgev
  :
  (EDGE | EDGE1)
  ;
  
  property
  :
  (COLOR | VALUE)
  ;
  
  assignment
  :
  (COLORTYPE | NUMBER)
  ;
  
  
  /*------------------------------------------------------------------
  * LEXER RULES
  *------------------------------------------------------------------*/
  
  TRUE : 'True' ;
  
  FALSE : 'False' ;
  
  AND : '&amp;' ;
  
  OR : '|' ;
  
  ST : '-&gt;' ; 
  
  EXISTS  : 'Exists' ;
  
  FOREACH : 'Foreach' ;
  
  NODE : 'node' ;
  
  NODE1 : 'node1' ;
  
  EDGE : 'edge' ;
  
  EDGE1 : 'edge1' ;
  
  IN : 'in' ;
  
  GRAPH_A : 'GraphA' ;
  
  GRAPH_B : 'GraphB' ;
  
  EQUALS : '=' ;
  
  DOT : '.' ;
  
  COLOR : 'color' ;
  
  VALUE : 'value' ;
  
  NUMBER : ('0'..'9')+ (DOT ('0'..'9')+)? ;
  
  QUOTE : '\'' ;
  
  COLORTYPE : ('a'..'z')+ ;
  
  WS : [ \t\r\n]+ -&gt; skip ;
  </code></pre>
  
  <p>I believe that this is the final version of my grammar so now I want to specify some error handling for the input. The problem is that I don't know how. What I know is that after I parse the input I can iterate over the generated AST and this is the place to add the error handling.</p>
  
  <p>If the parse fails, return the parse exception; otherwise, I have specified the following cases for returning error message.</p>
  
  <ol>
  <li><p>There can't be 1 quantifier followed by <code>-&gt; condition</code> (this is a formula element) where   condition is equal to <code>atom=atom</code>. In other words, if there is only <code>quantifier</code> then <code>condition</code> should be equal to <code>atom EQUALS assignment</code>.</p></li>
  <li><p>If there are 2 quantifiers the first should start with <code>FOREACH</code></p></li>
  <li><p>The variable(s) in the quantifiers should be used in the <code>condition</code> statement</p></li>
  <li><p>There can't be more than two quantifiers on the left hand-side of the expression (because in the app I am developing there are only two graphs). So if the number of quantifiers is greater then two return error as well</p></li>
  <li><p>If there are 2 quantifiers then they should have different variables bound on them</p></li>
  </ol>
  
  <p>For example, the first case should be raised when as an input we have </p>
  
  <pre><code>Exists node in GraphA -&gt; node.color = node1.color 
  </code></pre>
  
  <p>because <code>node1</code> isn't specified in the left hand-side of the expression.</p>
  
  <p>An example for the second case would be the following input </p>
  
  <pre><code>Exists node in GraphA Exists node1 in GraphB -&gt; node.color = node1.color
  </code></pre>
  
  <p>So my question is do I have to implement all the error checking over the generated parse tree or I can specify some of it in the grammar using some java code. If the error handling should happen after the input is parsed what functionality of ANTLR 4 can I use in order to implement the error cases? Any help or advice would be much appreciated!       </p>