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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:48:14+00:00 2026-05-25T10:48:14+00:00

I am needing to parse a small expression language (and, or, not, parens change

  • 0

I am needing to parse a small expression language (and, or, not, parens change precedence) so picked ANTLR for the task, I made good progress (ANTLRWorks is very nice for a newbie). I used some Getting Starting references from the antlr website and then found two blog posts that are perfect fit for what I am trying to accomplish:

http://www.codeproject.com/KB/recipes/sota_expression_evaluator.aspx
http://www.alittlemadness.com/2006/06/05/antlr-by-example-part-1-the-language

The problem I am having is no matter what input I put I always get the error:

line 1:29 no viable alternative at input ‘EOF’

So as part of my troubleshooting I decided to try a grammar I knew was good and generated a lexer/parser from the ECalc.g grammar found at the first link. To my surprise I got the same error when using that grammar! I am bamboozled. The only changes I made to the grammar were to make it generate Java code and took out some CSharp code in the @members section.

Here is my tester class:

public class ECalcTester {
private final static Logger logger = Logger.getLogger(ECalcTester.class);

public static void main(String[] args) {
    BasicConfigurator.configure();
    ECalcLexer lex = new ECalcLexer(new ANTLRStringStream("false || not (false and true)"));

    Token token;
    while (true) {
        token = lex.nextToken();
        if (token.getType() == Token.EOF) {
            break;
        }

        System.out.println("Token: ‘" + token.getText() + "’");
    }

    CommonTokenStream tokens = new CommonTokenStream(lex);
    lex.nextToken();

    ECalcParser parser = new ECalcParser(tokens);
    try {
        logger.debug(parser.expression().getTree());
    } catch (org.antlr.runtime.RecognitionException e) {
        logger.error("Exception ", e);
    }

}

Here is the output:

Token: ‘false’
Token: ‘ ’
Token: ‘||’
Token: ‘ ’
Token: ‘not’
Token: ‘ ’
Token: ‘(’
Token: ‘false’
Token: ‘ ’
Token: ‘and’
Token: ‘ ’
Token: ‘true’
Token: ‘)’
line 1:29 no viable alternative at input '<EOF>'
0 [main] DEBUG ECalcTester  - <unexpected: [@0,29:29='<EOF>',<-1>,1:29], resync=>

If I can figure out why this occurs in a grammar that should be good I should be able to figure out why the same thing happens in my grammar (very similar concept).

Can anyone offer any insight?

  • 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-25T10:48:15+00:00Added an answer on May 25, 2026 at 10:48 am

    After printing out your tokens, you will be at the end of the token stream. You will need to reset the token stream by calling

    lex.reset();
    

    This will make the lexer go back to the start of the token stream so you can call your parser.

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

Sidebar

Related Questions

I'm needing to check the differences between two XMLs but not blindly, Given that
I'm needing to cache some data using System.Web.Caching.Cache . Not sure if it matters,
I'm needing to make this layout editor that uses a good bit of jQuery
I am needing to parse an XML file for my app and I dont
Good afternoon. I have been making a small openGL based app for android that
Just needing clarification: W3Schools partially defines the CSS top property (not margin- or padding-top)
In a desktop application needing some serious re-factoring, I have several chunks of code
I find myself needing to store public key certificates, and a single private key
I find myself needing to generate a checksum for a string of data, for
I saw something about needing to have the assembly available for the type of

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.