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 7042981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:13:55+00:00 2026-05-28T02:13:55+00:00

It seems that sometimes the Antlr lexer makes a bad choice on which rule

  • 0

It seems that sometimes the Antlr lexer makes a bad choice on which rule to use when tokenizing a stream of characters… I’m trying to figure out how to help Antlr make the obvious-to-a-human right choice. I want to parse text like this:

d/dt(x)=a
a=d/dt
d=3
dt=4

This is an unfortunate syntax that an existing language uses and I’m trying to write a parser for. The “d/dt(x)” is representing the left hand side of a differential equation. Ignore the lingo if you must, just know that it is not “d” divided by “dt”. However, the second occurrence of “d/dt” really is “d” divided by “dt”.

Here’s my grammar:

grammar diffeq_grammar;

program :   (statement? NEWLINE)*;

statement
    :   diffeq
    |   assignment;

diffeq  :   DDT ID ')' '=' ID;

assignment
    :   ID '=' NUMBER
    |   ID '=' ID '/' ID
    ;

DDT :   'd/dt(';
ID  :   'a'..'z'+;
NUMBER  :   '0'..'9'+;
NEWLINE :   '\r\n'|'\r'|'\n';

When using this grammar the lexer grabs the first “d/dt(” and turns it to the token DDT. Perfect! Now later the lexer sees the second “d” followed by a “/” and says “hmmm, I can match this as an ID and a ‘/’ or I can be greedy and match DDT”. The lexer chooses to be greedy… but little does it know, there is no “(” a few characters later in the input stream. When the lexer looks for the missing “(” it throws a MismatchedTokenException!

The only solution I’ve found so far, is to move all the rules into the parser with a grammar like:

grammar diffeq_grammar;

program :   (statement? NEWLINE)*;

statement
    :   diffeq
    |   assignment;

diffeq  :   ddt id ')' '=' id;

assignment
    :   id '=' number
    |   id '=' id '/' id
    ;

ddt :   'd' '/' 'd' 't' '(';
id  :   CHAR+;
number  :   DIGIT+;
CHAR    :   'a'..'z';
DIGIT   :   '0'..'9';
NEWLINE :   '\r\n'|'\r'|'\n';

This is a fine solution if I didn’t already have thousands of lines of working code that depend on the first grammar working. After spending 2 days researching this problem I have come to the conclusion that a lexer… really ought to be able to distinguish the two cases. At some point the Antlr lexer is deciding between two rules: DDT and ID. It chooses DDT because the lexer is greedy. But when matching DDT fails, I’d like the lexer to go back to using ID.

I’m okay with using predicates or other tricks as long as the grammar remains basically the same (i.e., the rules in the lexer, stay in the lexer. And most rules are left untouched.).

Ideally I can modify the lexer rule for DDT with any valid Antlr code… and be done.

My target language is Java.

Thanks!

UPDATE

Thank you guys for some great answers!! I accepted the answer that best fit my question. The actual solution I used is in my own answer (not the accepted answer), and there are more answers that could have worked. Readers, check out all the answers; some of them may suit your case better than mine.

  • 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-05-28T02:13:56+00:00Added an answer on May 28, 2026 at 2:13 am

    I’m okay with using predicates or other tricks as long as the grammar remains basically the same (i.e., the rules in the lexer, stay in the lexer. And most rules are left untouched.).

    In that case, force the lexer to look ahead in the char-stream to make sure there really is "d/dt(" using a gated syntactic predicate.

    A demo:

    grammar diffeq_grammar;
    
    @parser::members {
      public static void main(String[] args) throws Exception {
        String src = 
            "d/dt(x)=a\n" +
            "a=d/dt\n" +
            "d=3\n" +
            "dt=4\n";
        diffeq_grammarLexer lexer = new diffeq_grammarLexer(new ANTLRStringStream(src));
        diffeq_grammarParser parser = new diffeq_grammarParser(new CommonTokenStream(lexer));
        parser.program();
      }
    }
    
    @lexer::members {
      private boolean ahead(String text) {
        for(int i = 0; i < text.length(); i++) {
          if(input.LA(i + 1) != text.charAt(i)) {
            return false;
          }
        }
        return true;
      }
    }
    
    program
     : (statement? NEWLINE)* EOF
     ;
    
    statement
     : diffeq     {System.out.println("diffeq     : " + $text);}
     | assignment {System.out.println("assignment : " + $text);}
     ;
    
    diffeq
     : DDT ID ')' '=' ID
     ;
    
    assignment
     : ID '=' NUMBER
     | ID '=' ID '/' ID
     ;
    
    DDT     : {ahead("d/dt(")}?=> 'd/dt(';
    ID      : 'a'..'z'+;
    NUMBER  : '0'..'9'+;
    NEWLINE : '\r\n' | '\r' | '\n';
    

    If you now run the demo:

    java -cp antlr-3.3.jar org.antlr.Tool diffeq_grammar.g
    javac -cp antlr-3.3.jar *.java
    java -cp .:antlr-3.3.jar diffeq_grammarParser

    (when using Windows, replace the : with ; in the last command)

    you will see the following output:

    diffeq     : d/dt(x)=a
    assignment : a=d/dt
    assignment : d=3
    assignment : dt=4
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Starting to use the googletest ASSERT_THROW clause, it seems that 'sometimes' the base type
It seems that MSMQ doesn't use any Database management system to manage messages. How
It seems that when we use form_for @story do |f| then Story has to
It seems that sometimes the currency format does not work: string Amount = 11123.45;
We allow users to write code which sometimes calls jQuery.ready(function(){..}) multiple times. It seems
tried to research on that but sometimes I seem to lack some googling skills...
Seems that even after unchecking the option in the PyDev/Debug preferenecs pane to launch
It seems that when a WPF application starts, nothing has focus. This is really
It seems that in a standard Xcode project, the default target automatically updates the
It seems that using Critical Sections quite a bit in Vista/Windows Server 2008 leads

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.