Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Yes, dynamic tokens match IDENTIFIER rule</p> </blockquote> <p>In that case, simply do a check <em>after</em> the <code>Id</code> has matched completely to see if the text the <code>Id</code> matched is in a predefined collection. If it is in the collection (a <code>Set</code> in my example) change the type of the token.</p> <p>A small demo:</p> <pre class="lang-java prettyprint-override"><code>grammar T; @lexer::members { private java.util.Set&lt;String&gt; special; public TLexer(ANTLRStringStream input, java.util.Set&lt;String&gt; special) { super(input); this.special = special; } } parse : (t=. {System.out.printf("\%-10s'\%s'\n", tokenNames[$t.type], $t.text);})* EOF ; Id : ('a'..'z' | 'A'..'Z' | '_') ('a'..'z' | 'A'..'Z' | '_' | '0'..'9')* {if(special.contains($text)) $type=Special;} ; Int : '0'..'9'+ ; Space : (' ' | '\t' | '\r' | '\n') {skip();} ; fragment Special : ; </code></pre> <p>And if you now run the following demo:</p> <pre class="lang-java prettyprint-override"><code>import org.antlr.runtime.*; public class Main { public static void main(String[] args) throws Exception { String source = "foo bar baz Mu"; java.util.Set&lt;String&gt; set = new java.util.HashSet&lt;String&gt;(); set.add("Mu"); set.add("bar"); TLexer lexer = new TLexer(new ANTLRStringStream(source), set); TParser parser = new TParser(new CommonTokenStream(lexer)); parser.parse(); } } </code></pre> <p>You will see the following being printed:</p> <pre class="lang-none prettyprint-override"><code>Id 'foo' Special 'bar' Id 'baz' Special 'Mu' </code></pre> <h1>ANTLR4</h1> <p>For ANTLR4, you can do something like this:</p> <pre class="lang-antlr prettyprint-override"><code>grammar T; @lexer::members { private java.util.Set&lt;String&gt; special = new java.util.HashSet&lt;&gt;(); public TLexer(CharStream input, java.util.Set&lt;String&gt; special) { this(input); this.special = special; } } tokens { Special } parse : .*? EOF ; Id : [a-zA-Z_] [a-zA-Z_0-9]* {if(special.contains(getText())) setType(TParser.Special);} ; Int : [0-9]+ ; Space : [ \t\r\n] -&gt; skip ; </code></pre> <p>test it with the class:</p> <pre class="lang-java prettyprint-override"><code>import org.antlr.v4.runtime.*; import java.util.HashSet; import java.util.Set; public class Main { public static void main(String[] args) { String source = "foo bar baz Mu"; Set&lt;String&gt; set = new HashSet&lt;String&gt;(){{ add("Mu"); add("bar"); }}; TLexer lexer = new TLexer(CharStreams.fromString(source), set); CommonTokenStream tokenStream = new CommonTokenStream(lexer); tokenStream.fill(); for (Token t : tokenStream.getTokens()) { System.out.printf("%-10s '%s'\n", TParser.VOCABULARY.getSymbolicName(t.getType()), t.getText()); } } } </code></pre> <p>which will print:</p> <pre class="lang-none prettyprint-override"><code>Id 'foo' Special 'bar' Id 'baz' Special 'Mu' EOF '&lt;EOF&gt;' </code></pre>
 

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