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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:52:33+00:00 2026-06-15T09:52:33+00:00

For a markup language I’m trying to parse, I decided to give parser generation

  • 0

For a markup language I’m trying to parse, I decided to give parser generation a try with ANTLR. I’m new to the field, and I’m messing something up.

My grammar is

grammar Test;
DIGIT   :   ('0'..'9');
LETTER  :   ('A'..'Z');
SLASH   :   '/'; 
restriction
    :   ('E' ap)
    |   ('L' ap)
    |   'N';
ap  :   LETTER LETTER LETTER;
car :   LETTER LETTER;
fnum    :   DIGIT DIGIT DIGIT DIGIT? LETTER?;
flt :   car fnum?;
message :   'A' (SLASH flt)? (SLASH restriction)?;

which does exactly what I want, when I give it an input string A/KK543/EPOS. When I give it A/KL543/EPOS however, it fails (MismatchedTokenException(9!=5)). It seems like some sort of conflict; it wants to generate restriction on the first L, so it seems I’m doing something wrong in the language definition, but I can’t properly find out what.

  • 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-15T09:52:34+00:00Added an answer on June 15, 2026 at 9:52 am

    For the input "A/KK543/EPOS", the following tokens are created:

    'A'        'A'
    SLASH      '/'
    LETTER     'K'
    LETTER     'K'
    DIGIT      '5'
    DIGIT      '4'
    DIGIT      '3'
    SLASH      '/'
    'E'        'E'
    LETTER     'P'
    LETTER     'O'
    LETTER     'S'

    But for the input "A/KL543/EPOS", these are created:

    'A'        'A'
    SLASH      '/'
    LETTER     'K'
    'L'        'L'
    DIGIT      '5'
    DIGIT      '4'
    DIGIT      '3'
    SLASH      '/'
    'E'        'E'
    LETTER     'P'
    LETTER     'O'
    LETTER     'S'

    As you can see, the char 'L' does not get tokenized as a LETTER. For the literal tokens 'A', 'E', 'L' and 'N' inside your parser rules, ANTLR (automatically) creates separate lexer rules that are place before all other lexer rules. This causes your lexer to look like this behind the scenes:

    A      : 'A';
    E      : 'E';
    L      : 'L';
    N      : 'N';
    DIGIT  : '0'..'9';
    LETTER : 'A'..'Z';
    SLASH  : '/'; 
    

    Therefor, any single 'A', 'E', 'L' and 'N' will never become a LETTER token. This is simply how ANTLR works. If you want to match them as letters, you’ll need to create a parser rule letter and let it match these tokens too. Something like this:

    message
     : A (SLASH flt)? (SLASH restriction)?
     ;
    
    flt
     : car fnum?
     ;
    
    fnum
     : DIGIT DIGIT DIGIT DIGIT? letter?
     ;
    
    restriction
     : E ap
     | L ap
     | N
     ;
    
    ap
     : letter letter letter
     ;
    
    car
     : letter letter
     ;
    
    letter
     : A
     | E
     | L
     | N
     | LETTER
     ;
    
    A      : 'A';
    E      : 'E';
    L      : 'L';
    N      : 'N';
    DIGIT  : '0'..'9';
    LETTER : 'A'..'Z';
    SLASH  : '/'; 
    

    which will parse the input "A/KL543/EPOS" like this:

    enter image description here

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

Sidebar

Related Questions

I'm new to XACML (the eXtensible Access Control Markup Language), and I'm a bit
I'm modifying PHP Markdown (a PHP parser of the markup language which is used
I am trying to make a simple markup language, and I need my Cocoa
Ok, so I'm trying to make my own markup language [for a class, I
I'm new to parsing (obviously). I am using LEPL library to parse some markup
I'm trying to generate Java classes from the FpML (Finanial Products Markup Language) version
I'm trying to developing a code validation system for an in-house markup language but
I'm using Jison (Bison) to create a simple markup language. I'm clearly new to
I need to write a XML-based markup language. I'd like to provide an open
What is the difference between Extensible Markup Language (XML) and XML Schema Definition (XSD)?

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.