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

  • SEARCH
  • Home
  • 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 8049121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:41:51+00:00 2026-06-05T06:41:51+00:00

I use the following Java-code to instantiate a parser generated with ANTLR. package foo;

  • 0

I use the following Java-code to instantiate a parser generated with ANTLR.

package foo;    
public class Test1 {
public static void main(String[] args) throws RecognitionException {
       CharStream stream = new ANTLRStringStream("foo ");
       BugLexer lexer = new BugLexer(stream);
       CommonTokenStream tokenStream = new CommonTokenStream(lexer);
       BugParser parser = new BugParser(tokenStream);
       parser.specification();
    }
}

My grammar:

grammar Bug;
options {
  language = Java;
}
@header {
  package foo;
}
@lexer::header {
  package foo;
}
specification : 
   'foo' EOF 
;  
WS 
   : (' ' | '\t' | '\n' | '\r')+ {$channel = HIDDEN;} 
;
SCOLON
   : (~ ';')+
;

And the error I get:
line 1:0 mismatched input ‘foo ‘ expecting ‘foo’
I would expect the space in the input to be ignored, but its not.. The antlr interpreter in eclipse says its fine so I suppose my Java code is wrong somehow, but I just don’t see it…
Note: If I remove the rule for SCOLON then theres not bug for the input.

  • 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-05T06:41:53+00:00Added an answer on June 5, 2026 at 6:41 am

    ANTLR’s lexer tries to match as much as possible for each token. Therefor "foo " is being tokenized as a single SCOLON token and not as a 'foo'– and WS token.

    Note that your SCOLON rule:

    SCOLON
     : (~ ';')+
     ;
    

    suggests by its name to match just a single semi-colon, but in fact matches one ore more characters other than a semi-colon. Perhaps it should have been this instead:

    SCOLON
     : ';'
     ;
    

    ?

    EDIT

    Heinrich Ody wrote:

    I somehow thought there is a priority (given by order of declaration) on which token ANTLR attempts to match the input. Thanks for your response.

    That is correct: whenever two (or more) rules match the same amount of characters, the rule defined first will “win”. But if a rule defined last matches the most characters, it “wins”.

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

Sidebar

Related Questions

I have the following code in Java: public static<T> void doIt(Class<T> t) { T[]
In the following Java code: public static void main(String[] args) { String largeText =
The following snippet uses simple Java code. package pkg; final public class Main {
Say I have the following code: import java.lang.InterruptedException; import javax.swing.SwingWorker; public class Test {
I have the following Java code: public static BufferedImage createImage(byte[] data, int width, int
With the following source code: package util.abc; public class Test{ public String out(){ return
I have the following Java code: public void myMethod (final Map pFeatureGroupsFromPackage) { final
Let's assume we've got the following Java code: public class Maintainer { private Map<Enum,
Is it reliable to use the following Java code: TreeSet<String> ts = new TreeSet<String>();
I'm trying to use the following code of java to read image data on

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.