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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:20:16+00:00 2026-06-06T10:20:16+00:00

I am trying to create a very simple antlr grammar file which should parse

  • 0

I am trying to create a very simple antlr grammar file which should parse the following file:

Report (MyReport)
Begin
End

Or without report name:

Report
Begin
End

And here is my grammar file:

grammar RL;

options {
  language = Java;
}

report:
  REPORT ('(' SPACE* STRING_LITERAL SPACE* ')')?
  BEGIN
  END
  ;

REPORT
    :   'Report'
    ;     

BEGIN
    :   'Begin'
    ;

END :   'End';

NAME:   LETTER (LETTER | DIGIT | '_')*;

STRING_LITERAL :    NAME SPACE*;

fragment LETTER: LOWER | UPPER;

fragment LOWER: 'a'..'z';

fragment UPPER: 'A'..'Z';

fragment DIGIT: '0'..'9';

fragment SPACE: ' ' | '\t';

WHITESPACE: SPACE+ { $channel = HIDDEN; };

rule: ;

However when I debug in ANTLRWorks I always get the following error:

 root -> report -> MismatchedTokenException(0!=0)

What’s wrong in my Grammar file?

thanks,
Green

  • 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-06T10:20:18+00:00Added an answer on June 6, 2026 at 10:20 am

    A couple of remarks:

    • Java is the default language, so you can omit language=Java;;
    • you’re using SPACE inside a parser rule, while this SPACE token is a fragment: this causes the lexer never to create this token: remove it from your parser rule(s);
    • the input "Report " (“Report” followed by a single white-space) is being tokenized as a STRING_LITERAL, not as a REPORT! ANTLR’s lexer consumes characters greedily, only when two or more rules match the same amount of characters, the rule defined first will get precedence. The lexer does not produce tokens that the parser is trying to match (parsing and tokenization are being performed independently!).

    Try the following instead:

    grammar RL;
    
    report
     : REPORT ('(' NAME ')')? BEGIN END
     ;
    
    REPORT : 'Report';     
    BEGIN  : 'Begin';
    END    : 'End';
    NAME   : LETTER (LETTER | DIGIT | '_')*;
    
    fragment LETTER : LOWER | UPPER;
    fragment LOWER  : 'a'..'z';
    fragment UPPER  : 'A'..'Z';
    fragment DIGIT  : '0'..'9';
    
    SPACE  : (' ' | '\t' | '\r' | '\n')+ { $channel = HIDDEN; };
    

    green wrote:

    What if I want to allow “SPACE” inside Report NAME?

    I would still skip spaces in the lexer. Accepting spaces between names but ignoring them in other contexts will result in some clunky rules. Instead of accounting for spaces between a report’s name, I would do something like this:

    report
     : REPORT ('(' report_name ')')? BEGIN END
     ;
    
    report_name
     : NAME+
     ;
    

    resulting in the following parse tree:

    enter image description here

    for the input:

    Report (a name with spaces)
    Begin
    End

    green wrote:

    so is it possible to allow me use reserved words like ‘Report’ in the name?

    Sure, explicitly add them in the report_name rule:

    report_name
     : (NAME | REPORT | BEGIN | END)+
     ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a very simple grammar to learn to use ANTLR but
I'm trying to create a very simple android screen with the following content: top
Greetings all, I'm trying to create a specific layout that should be very simple
I'm am trying to create a very simple WCF client application which will send
I've been trying to create a very simple windows script which almost works (almost
I'm trying to create a very simple chat application in Flex/.Net using FluorineFX but
I'm trying to create a very simple message board (author, text, and date written)
I'm trying to create a very simple keyframe animation, whereby a graphic Rotates from
I'm trying to create a very simple REST server. I just have a test
I am trying to create a very simple chat window that simply has the

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.