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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:29:04+00:00 2026-06-08T01:29:04+00:00

I have a grammar to parse some source code: document : header body_block* EOF

  • 0

I have a grammar to parse some source code:

document
  : header body_block* EOF
    -> body_block*
  ;

header
  : header_statement*
  ;

body_block
  : '{' block_contents '}'
  ;

block_contents
  : declaration_list
  | ... other things ....

It’s legal for a document to have a header without a body or a body without a header.

If I try to parse a document that looks like

int i;

then ANTLR complains that it found int when it was expecting EOF. This is true, but I’d like it to say that it was expecting {. That is, if the input contains something between the header and the EOF that’s not a body_block, then I’d like to suggest to the user that they meant to enclose that text inside a body_block.

I’ve made a couple almost working attempts at this that I can post if that’s illuminating, but I’m hoping that I’ve just missed something easy.

  • 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-08T01:29:06+00:00Added an answer on June 8, 2026 at 1:29 am

    Not pretty, but something like this would do it:

    body_block
     : ('{')=> '{' block_contents '}'
     | t=.
       {
         if(!$t.text.equals("{")) {
           String message = "expected a '{' on line " + $t.getLine() + " near '" + $t.text + "'";
         }
         else {
           String message = "encountered a '{' without a '}' on line " + $t.getLine();
         }
         throw new RuntimeException(message);
       }
     ;
    

    (not tested, may contain syntax errors!)

    So, whenever '{' ... '}' is not matched, it falls through to .1 and produces a more understandable error message.


    1 note that a . in a parser rule matches any token, not any character!

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

Sidebar

Related Questions

I want to parse some data, and I have a BNF grammar to parse
I have written grammar for a language (sample code below) //this is a procedure
I have some bison grammar: input: /* empty */ | input command ; command:
I have a text file that looks similar to; section header 1: some words
i have shared the header file containing class definition of a Context Free grammar
I have an idea for a hobby project which performs some code analysis and
I want to parse some C++ code, and as a guide I've been looking
I want to parse ISO 8601 dates in my ANTLR grammar. 2001-05-03 I have
I have to parse a document containing groups of variable-value-pairs which is serialized to
I have the following parser grammar (this is a small sample): expr: ident assignop

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.