title: ANTLR4 Semantic Predicates that is Context Dependent Does Not Work
body: <p>I am parsing a C++ like declaration with this scaled down grammar (many details removed to make it a fully working example). It fails to work mysteriously (at least to me). Is it related to the use of context dependent predicate? If yes, what is the proper way to implement the "counting the number of child nodes logic"?</p>
  
  <pre><code>grammar CPPProcessor;
  
  cppCompilationUnit : decl_specifier_seq? init_declarator* ';'  EOF;
  
  init_declarator:     declarator initializer?;
  declarator:  identifier;
  initializer: '=0';
  
  decl_specifier_seq
    locals [int cnt=0]
      @init {  $cnt=0;    }
  : decl_specifier+ ;
  decl_specifier :   @init {    System.out.println($decl_specifier_seq::cnt);  }
      'const'
    | {$decl_specifier_seq::cnt &lt; 1}? type_specifier {$decl_specifier_seq::cnt += 1;}  ;
  type_specifier:  identifier ; 
  identifier:IDENTIFIER;
  CRLF: '\r'? '\n' -&gt; channel(2);
  WS: [ \t\f]+    -&gt; channel(1);
  IDENTIFIER:[_a-zA-Z] [0-9_a-zA-Z]* ;
  </code></pre>
  
  <p><strong>I need to implement the standard C++ rule that no more than 1 <code>type_specifier</code> is allowed under an <code>decl_specifier_seq</code></strong>. </p>
  
  <p>Semantic predicate before <code>type_specifier</code> seems to be the solution.  And the count is naturally declared as a local variable in <code>decl_specifier_seq</code> since nested <code>decl_specifier_seq</code> are possible.</p>
  
  <p>But it seems that a context dependent semantic predicate like the one I used will produce incorrect parsing i.e. a semantic predicate that references $attributes. First an input file with correct result (to illustrate what a normal parse tree looks like):</p>
  
  <pre><code>int t=0;
  </code></pre>
  
  <p>and the parse tree:</p>
  
  <p><img src="https://i.stack.imgur.com/0x1bO.png" alt="enter image description here"></p>
  
  <p>But, an input without the '=0' to aid the parsing</p>
  
  <pre><code>int t;
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/EoVCT.png" alt="enter image description here"></p>
  
  <pre><code>0
  1
  line 1:4 no viable alternative at input 't'
  1
  </code></pre>
  
  <p>the parsing failed with the 'no viable alternative' error (the numbers printed in the console is debug print of the <code>$decl_specifier_cnt::cnt</code> value as a verification of the test condition). i.e. the semantic predicate cannot prevent the <code>t</code> from being parsed as <code>type_specifier</code> and <code>t</code> is no longer considered a <code>init_declarator</code>. What is the problem here? Is it because a context dependent predicate having <code>$decl_specifier_seq::cnt</code> is used?</p>
  
  <p>Does it mean context dependent predicate cannot be used to implement "counting the number of child nodes" logic?</p>
  
  <p>EDIT</p>
  
  <p>I tried new versions whose predicate uses member variable instead of the <code>$decl_specifier_seq::cnt</code> and surprisingly the grammar now works proving that the Context Dependent predicate did cause the previous grammar to fail:</p>
  
  <pre><code>....
  @parser::members {
    public int cnt=0;
  }
  decl_specifier
    @init {System.out.println("cnt:"+cnt);  }
  : 
      'const'
    | {cnt&lt;1 }? type_specifier {cnt++;}  ;
  </code></pre>
  
  <p>A normal parse tree is resulted:</p>
  
  <p><img src="https://i.stack.imgur.com/rSQGe.png" alt="enter image description here"></p>
  
  <p>This gives rise to the question of how to support nested rule if we must use member variables to replace the local variables to avoid context sensitive predicates?</p>
  
  <p>And a weird result is that if I add a <code>/*$ctx*/</code> after the predicate, it fails again:</p>
  
  <pre><code>decl_specifier
    @init {System.out.println("cnt:"+cnt);  }
  : 
      'const'
    | {cnt&lt;1 /*$ctx*/ }? type_specifier {cnt++;}  ;
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/rsMbO.png" alt="enter image description here"></p>
  
  <pre><code>line 1:4 no viable alternative at input 't'
  </code></pre>
  
  <p>The parsing failed with <code>no viable alternative</code>. Why the <code>/*$ctx*/</code> causes the parsing to fail like when <code>$decl_specifier_seq::cnt</code> is used although the actual logic uses a member variable only?
  And, without the <code>/*$ctx*/</code>, another issue related to the predicate called before @init block appears(<a href="https://stackoverflow.com/questions/23571210/antlr4-semantic-predicate-called-before-init">described here</a>)</p>