title: How to avoid post-token code execution in ANTLR, when token is absent?
body: <p>This is my ANTLR script:</p>
  
  <pre><code>greeting:
    'hello'
    NAME
    { /* do something with $NAME.text */ }
    ;
  </code></pre>
  
  <p>This input causes an NPE because <code>NAME</code> is null:</p>
  
  <pre><code>hello
  </code></pre>
  
  <p>I understand that I can check whether <code>$NAME</code> is not null and only then do my manipulations with <code>$NAME.text</code>, but I have that problem in many places in the script. Should I always do <code>if ($NAME != null)</code> or maybe there is some other more elegant mechanism?</p>