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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:02:37+00:00 2026-05-28T15:02:37+00:00

How do you do something like this with ANTLR? Example input: title: hello world

  • 0

How do you do something like this with ANTLR?

Example input:

title: hello world

Grammar:

header : IDENT ':' REST_OF_LINE ;
IDENT : 'a'..'z'+ ;
REST_OF_LINE : ~'\n'* '\n' ;

It fails, with line 1:0 mismatched input 'title: hello world\n' expecting IDENT

(I know ANTLR is overkill for parsing MIME-like headers, but this is just at the top of a more complex file.)

  • 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-28T15:02:38+00:00Added an answer on May 28, 2026 at 3:02 pm

    It fails, with line 1:0 mismatched input ‘title: hello world\n’ expecting IDENT

    You must understand that the lexer operates independently from the parser. No matter what the parser would “like” to match at a certain time, the lexer simply creates tokens following some strict rules:

    1. try to match tokens from top to bottom in the lexer rules (rules defined first are tried first);
    2. match as much text as possible. In case 2 rules match the same amount of text, the rule defined first will be matched.

    Because of rule 2, your REST_OF_LINE will always “win” from the IDENT rule. The only time an IDENT token will be created is when there’s no more \n at the end. That is what’s going wrong with your grammars: the error messages states that it expects a IDENT token, which isn’t found (but a REST_OF_LINE token is produced).

    I know ANTLR is overkill for parsing MIME-like headers, but this is just at the top of a more complex file.

    You can’t just define tokens (lexer rules) you want to apply to the header of a file. These tokens will also apply to the rest of the more complex file. Perhaps you should pre-process the header separately from the rest of the file?

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

Sidebar

Related Questions

With ANTLR2, you could define something like this in grammar definition file: options {
Something like this, would make the widget appear normally: Label(self, text = 'hello', visible
Something like this might had worked <%= Html.RouteLink(, new { controller = Article, action
Something like this: if self.__class__ == User: logging.debug(%s non_pks were found % (str(len(non_pks))) )
Using something like this: try: # Something... except Exception as excep: logger = logging.getLogger(component)
Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a
Tried something like this: HttpApplication app = s as HttpApplication; //s is sender of
Does something like this affect performance badly? var myQuery = from c in Customers
Is something like this possible: function FooClass() { var barsArray=new Array(); var index=0; function
Does something like this make any sense at all in Java? class A<T extends

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.