Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you only want a lexer, start your grammar with:</p> <pre><code>lexer grammar FooLexer; // creates: FooLexer.java </code></pre> <p><code>LT(int): Token</code> can only be used inside parser rules (on a <a href="http://www.antlr.org/api/Java/interfaceorg_1_1antlr_1_1runtime_1_1_token_stream.html" rel="nofollow"><code>TokenStream</code></a>). Inside lexer rules, you can only use <code>LA(int): int</code> that gets the next <code>int</code> (character) from the <a href="http://www.antlr.org/api/Java/interfaceorg_1_1antlr_1_1runtime_1_1_int_stream.html" rel="nofollow"><code>IntStream</code></a>. But there is no need for all the manual look ahead. Just do something like this:</p> <pre class="lang-java prettyprint-override"><code>lexer grammar FooLexer; BEGIN : 'BEGIN' ; END : 'END' ; FLOAT : DIGIT+ '.' DIGIT+ ; INT : DIGIT+ ; IDENTIFIER : LETTER (LETTER | DIGIT)* ; WS : (' ' | '\t' | '\n' | '\r' | '\f')+ {$channel = HIDDEN;} ; fragment LETTER : ('a'..'z' | 'A'..'Z'); fragment DIGIT : ('0'..'9'); </code></pre> <p>I don't see the need to create a token called <code>KEYWORD</code> that matches all keywords: you'll want to make a distinction between a <code>BEGIN</code> and <code>END</code> token, right? But if you really want this, simply do:</p> <pre class="lang-java prettyprint-override"><code>KEYWORD : 'BEGIN' | 'END' ; </code></pre> <p>and remove the <code>BEGIN</code> and <code>END</code> rules. Just make sure <code>KEYWORD</code> is defined before <code>IDENTIFIER</code>.</p> <h2>EDIT</h2> <p>Test the lexer with the following class:</p> <pre class="lang-java prettyprint-override"><code>import org.antlr.runtime.*; public class Main { public static void main(String[] args) throws Exception { String src = "BEGIN END 3.14159 42 FOO"; FooLexer lexer = new FooLexer(new ANTLRStringStream(src)); while(true) { Token token = lexer.nextToken(); if(token.getType() == FooLexer.EOF) { break; } System.out.println(token.getType() + " :: " + token.getText()); } } } </code></pre> <p>If you generate a lexer, compile the .java source files and run the Main class like this:</p> <pre class="lang-none prettyprint-override"><code>java -cp antlr-3.3.jar org.antlr.Tool FooLexer.g javac -cp antlr-3.3.jar *.java java -cp .:antlr-3.3.jar Main </code></pre> <p>the following output will be printed to the console:</p> <pre class="lang-none prettyprint-override"><code>4 :: BEGIN 11 :: 5 :: END 11 :: 7 :: 3.14159 11 :: 8 :: 42 11 :: 10 :: FOO </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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