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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:52:20+00:00 2026-05-29T15:52:20+00:00

I am having problems with my Antlr grammar. I’m trying to write a parser

  • 0

I am having problems with my Antlr grammar. I’m trying to write a parser rule for ‘typedident’ which can accept the following inputs:

‘int a’ or ‘char a’

The variable name ‘a’ is from my lexer rule ‘IDENT’ which is defined as follows:

IDENT : (('a'..'z'|'A'..'Z') | '_') (('a'..'z'|'A'..'Z')|('0'..'9')| '_')*;

My ‘typedident’ parser rule is as follows:

typedident : (INT|CHAR) IDENT;

INT and CHAR having been defined as tokens.

The problem I’m having is that when I test ‘typedident’ the variable name has to be more than one character. For example:

'int a' isn’t accepted while 'int ab' is accepted.

The outputed error I get is:

“MismatchedTokenException: mismatched input ‘a’ expecting ‘$'”

Any idea why I’m getting this error? I’m fairly new to Antlr so apologies if the error is trivial.

EDIT

I literally just got it working, and I don’t know why. I also had two other lexer rules defined as follows:

ALPH : ('a'..'z'|'A'..'Z'); 
DIGIT : ('0'..'9'); 

I realised these weren’t being used at all so I deleted them and everything now works fine! My guess why this works is because ALPH and DIGIT were overriding my other Lexer rules:

NUMBER : ('0'..'9')+; 
CHARACTER : '\'' (~('\n' | '\r' |'\'')) '\''; 

Does anyone know if this is the case? I’m curious as to why this problem has now been solved.

  • 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-29T15:52:20+00:00Added an answer on May 29, 2026 at 3:52 pm

    ‘int a’ isn’t accepted while ‘int ab’ is accepted.
    …
    My guess why this works is because ALPH and DIGIT were overriding …

    Yes, it appears ALPH was defined before the IDENT rule, in which case single letters were tokenized as ALPH tokens. If IDENT was defined before ALPH, it should all go okay (in your case).

    To summarize how ANTLR’s lexer rules work:

    • lexer rules match as much characters as possible (greedy);
    • if 2 (or more) lexer rules match the same input, the rule defined first will “win”.

    You must realize that the lexer does not produce tokens based on what the parser (at that time) needs. The lexer operates independently from the parser.

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

Sidebar

Related Questions

am having problems with some Regex code can anyone help. I have the following
Having problems with a small awk script, Im trying to choose the newest of
im having problems with the following code: for (var i:Number=0; i<numFaces;i++){ var faceLoader:Loader =
Having problems with this setup what im trying to do is like so. Steps
I've written the below grammar for ANTLR parser and lexer for building trees for
Having problems figuring this out. trying to do a rescue_from NoMethodError, :with => :try_some_options
Im having problems trying to install the HtmlAgilityPack.dll ver 1.4. I've copied the files
Am having problems with the following code reporting an 'invalid argument' in IE6. function
having problems trying to run by unit test with Ant, my test class uses
Having problems with the documentmanager upload trying to upload PDF's. The problem happens when

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.