title: Antlr4 parsing jsp scriptlets expression directive
body: <p>I want to parse jsp files to identify the directive tags, jsp action elements, scriptlet expression etc. For that I am trying to extend the existing html <a href="https://github.com/antlr/grammars-v4/tree/master/html" rel="nofollow noreferrer">grammar</a>.
  I am facing problem in differentiating normal html tag opening and jsp tag opening. The existing grammar has the following lexer rules:</p>
  
  <pre><code>TAG_OPEN
      : '&lt;' -&gt; pushMode(TAG)
      ;
  //
  // tag declarations
  //
  mode TAG;
  
  TAG_CLOSE
      : '&gt;' -&gt; popMode
      ;
  
  TAG_SLASH_CLOSE
      : '/&gt;' -&gt; popMode
      ;
  
  TAG_SLASH
      : '/'
      ;
  
  //
  // lexing mode for attribute values
  //
  TAG_EQUALS
      : '=' -&gt; pushMode(ATTVALUE)
      ;
  
  TAG_NAME
      : TAG_NameStartChar TAG_NameChar*
      | TAG_NameStartChar* ':' TAG_NameStartChar*
      ;
  
  TAG_WHITESPACE
      : [ \t\r\n] -&gt; skip
      ;
  </code></pre>
  
  <p>And the correspoding parser rule for that: </p>
  
  <pre><code>htmlElement
      : TAG_OPEN htmlTagName htmlAttribute* TAG_CLOSE htmlContent TAG_OPEN TAG_SLASH htmlTagName TAG_CLOSE
      | TAG_OPEN htmlTagName htmlAttribute* TAG_SLASH_CLOSE
      | TAG_OPEN htmlTagName htmlAttribute* TAG_CLOSE
      | TAG_OPEN TAG_SLASH htmlTagName TAG_CLOSE
  //  | jspDirective
  //  | jspDeclaration
      | scriptlet
      | script
      | style
      ;
  </code></pre>
  
  <p>I am trying to extend the above rules with the following: </p>
  
  <pre><code>scriptlet
      : JSP_TAG_OPEN jspElement jspElementName jspElementAttribute* JSP_TAG_CLOSE 
      //|SCRIPTLET
      ;
  
  jspElementName
      : TAG_NAME
      ;
  
  jspElementAttribute
      : jspAttributeName TAG_EQUALS jspAttributeValue
      ;
  
  jspAttributeName
      : TAG_NAME
      ;
  
  jspAttributeValue
      : ATTVALUE_VALUE
      ;
  
  jspElement
      : jspDirective
      | jspExpression
      | jspDeclaration
      | jspFragment
      ;
  
  jspDirective    
      : JSP_DIRECTIVE_SIGN
      ;
  
  jspExpression
      : JSP_EXPRESSION_SIGN
      ;
  
  jspDeclaration
      : JSP_DECLARATION_SIGN
      ;
  
  jspFragment
      : JSP_FRAGMENT_SIGN
      ;
  </code></pre>
  
  <p>The lexer rules are :</p>
  
  <pre><code>JSP_TAG_OPEN
      : '&lt;%' ' '*
      ;
  
  JSP_DIRECTIVE_SIGN
      : '@'
      ;
  
  JSP_EXPRESSION_SIGN
      : '='
      ;
  
  JSP_DECLARATION_SIGN
      : '!'
      ;
  
  JSP_FRAGMENT_SIGN
      : [ ]+
      ;
  
  JSP_TAG_CLOSE
      : '%&gt;'  
      ;
  </code></pre>
  
  <p>But the problem I guess is that, whenever a <code>&lt;</code> character is encountered the lexer rule for <code>TAG_OPEN</code> starts running, rather than waiting to see whether that is a jsp tag <code>&lt;%</code>.</p>
  
  <p>So when I am trying to parse a small html with the following content: </p>
  
  <pre><code>&lt;html&gt;
  &lt;title&gt;Statement Load page&lt;/title&gt;
  &lt;%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
      prefix="html"%&gt;
  </code></pre>
  
  <p>I am getting this error:</p>
  
  <pre><code>line 3:2 no viable alternative at input '@ taglib uri="http://jakarta.apache.org/struts/tags-html"\r\n\tprefix="html"%&gt;\r\n'
  </code></pre>