Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9163825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:32:12+00:00 2026-06-17T14:32:12+00:00

With Java Grammar for ANTLR, I could read a java code and print out

  • 0

With Java Grammar for ANTLR, I could read a java code and print out the tokens sequentially.

    String filePath = JAVA_SOURCE;
    String input = readFileAsString(filePath);
    //ANTLRStringStream in = new ANTLRStringStream(input);
    InputStream inputStream = new FileInputStream(filePath);
    ANTLRInputStream in = new ANTLRInputStream(inputStream);
    Java6Lex lexer = new Java6Lex(in);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    while(true) {
        int val = tokens.LA(1);
        tokens.consume();

        if (val == -1) {
            break;
        }
        System.out.printf("%d ", val);
    }

59 54 38 54 81 61 92 59 54 38 54 96 61 92 59 54 38 54 81 54 92 90 54 92 …

How can I map each tokens back to the position in the JAVA_SOURCE? Does ANTLR have a counter or something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:32:13+00:00Added an answer on June 17, 2026 at 2:32 pm

    By default, ANTLR produces CommonTokens. Read the full API here: http://www.antlr.org/api/Java/org/antlr/runtime/CommonToken.html

    Here’s a demo to print some information about the tokens the Java6 parser encounters:

    • merge the lexer and parser rules in a single file called Java6.g (and name the grammar “Java6”, of course!)
    • copy the file antlr-3.3-complete.jar in the same folder as Java6.g

    Copy-paste the following in your Java6.g file:

    grammar Java6;
    
    // options ...
    
    @parser::members{
      public static void main(String[] args) throws Exception {
        String source = "package test;\n\npublic class Test {\n\n  int n = 42;\n}\n";
        Java6Lexer lexer = new Java6Lexer(new ANTLRStringStream(source));
        Java6Parser parser = new Java6Parser(new CommonTokenStream(lexer));
        System.out.println(source);
        parser.dumpTokens();
      }
    }
    
    dumpTokens
      :  (
           t=. {
             CommonToken ct = (CommonToken)t;
             System.out.printf("type=\%s, text='\%s', line=\%d, startIndex=\%d, charPositionInLine=\%d\n", 
                                tokenNames[ct.getType()], 
                                ct.getText(), 
                                ct.getLine(), 
                                ct.getStartIndex(), 
                                ct.getCharPositionInLine());
           }
         )* 
         EOF
      ;
    
    // the rest of the grammar rules are not changed
    

    Now run the Java6 file:

    java -cp antlr-3.3-complete.jar org.antlr.Tool Java6.g
    javac -cp antlr-3.3-complete.jar *.java
    java -cp .:antlr-3.3-complete.jar Java6Parser

    and you will see the following being printed to your console:

    package test;
    
    public class Test {
    
      int n = 42;
    }
    
    type=PACKAGE, text='package', line=1, startIndex=0, charPositionInLine=0
    type=IDENTIFIER, text='test', line=1, startIndex=8, charPositionInLine=8
    type=SEMI, text=';', line=1, startIndex=12, charPositionInLine=12
    type=PUBLIC, text='public', line=3, startIndex=15, charPositionInLine=0
    type=CLASS, text='class', line=3, startIndex=22, charPositionInLine=7
    type=IDENTIFIER, text='Test', line=3, startIndex=28, charPositionInLine=13
    type=LBRACE, text='{', line=3, startIndex=33, charPositionInLine=18
    type=INT, text='int', line=5, startIndex=38, charPositionInLine=2
    type=IDENTIFIER, text='n', line=5, startIndex=42, charPositionInLine=6
    type=EQ, text='=', line=5, startIndex=44, charPositionInLine=8
    type=INTLITERAL, text='42', line=5, startIndex=46, charPositionInLine=10
    type=SEMI, text=';', line=5, startIndex=48, charPositionInLine=12
    type=RBRACE, text='}', line=6, startIndex=50, charPositionInLine=0

    And if you’re looking for a way to get tokens from parser rules, every parser rule has a start and stop member in its ParserRuleReturnScope that can be cast to a CommonToken.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For my grammar in ANTLR, my java code can catch and print errors for
Given an ANTLR Java Grammar - what java source code would I write to
In Antlrworks I get this error: [18:21:03] Checking Grammar Grammar.g... [18:21:26] Grammar.java:12: code too
I'm parsing PHP code using an antlr Grammar and the antlr Ruby Target .
I'm in the process of creating a Java analyzer for an ANTLR grammar that
I have the following ANTLR grammar: grammar Tasks; options { language = Java; }
I generated a Lua Parser using the Lua.g grammar with antlr in java. My
I use the following Java-code to instantiate a parser generated with ANTLR. package foo;
I have the Java source code for Parser and Lexer of ANTLR 3 in
Is there a BNF or EBNF that describes the grammar for Java's annotations?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.