Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing different case keywords in ANTLR grammar
    primarykey
    data
    text
    <p>Some keywords (string constant) in my grammar contain capital letters e.g. </p> <pre><code>PREV_VALUE : 'PreviousValue'; </code></pre> <p>This causes strange parsing behavior: other tokens that contain same capital letters ('P','V') are parsed incorrectly.</p> <p>Here's a simplified version of the lexer grammar:</p> <pre><code>lexer grammar ExpressionLexer; COMMA : ','; LPAREN : '('; RPAREN : ')'; LBRACK : '['; RBRACK : ']'; PLUS : '+'; MINUS : '-'; MULT : '*'; DIV : '/'; PREV_VALUE : 'PreviousValue'; fragment DIGIT : ('0'..'9'); fragment LETTER : ('a'..'z'|'A'..'Z'|'_'); fragment TAB : ('\t') ; fragment NEWLINE : ('\r'|'\n') ; fragment SPACE : (' ') ; </code></pre> <p>When I try parsing such expression:</p> <pre><code>var expression = "P"; //Capital 'P' which included to the keyword 'PreviousValue' var stringReader = new StringReader(expression); var input = new ANTLRReaderStream(stringReader); var expressionLexer = new ExpressionLexer(input); var tokens = new CommonTokenStream(expressionLexer); </code></pre> <p><code>tokens._tokens</code> collection contains one value</p> <pre><code>[0] = {[@0,1:1='&lt;EOF&gt;',&lt;-1&gt;,1:1]} </code></pre> <p>It's incorrect.</p> <p>If I change <code>expression</code> to 'p' (lowercase letter) <code>tokens._tokens</code> collection contains two values</p> <pre><code>[0] = {[@0,0:0='p',&lt;0&gt;,1:0]} [1] = {[@1,1:1='&lt;EOF&gt;',&lt;-1&gt;,1:1]} </code></pre> <p>It's correct.</p> <p>When string <code>PREV_VALUE : 'PreviousValue';</code> is removed from grammar, both expressions are parsed correctly.</p> <p>Is it possible to use different case in keywords? Is there any example of using such keywords in ANTLR grammar?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload