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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:27:54+00:00 2026-06-03T13:27:54+00:00

I’m using Antlr 3.2 and I’m having trouble writing a grammar to ignore comment

  • 0

I’m using Antlr 3.2 and I’m having trouble writing a grammar to ignore comment lines. Specifically, I’m getting an error if a comment line is the last line of the input with no newline after it.

My input is effectively assembly language, where comments start anywhere on the line with a semi-colon, and go to end of line. Everything else is parsed as commands.

A cutdown version of my grammar that exhibits the problem is:

grammar Test;

options {
    language = Java;
    output = AST;
    ASTLabelType = CommonTree;
}

@header {
    package test;
}

@lexer::header {
    package test;
}

rule
    :   instruction+ EOF!
    ;

instruction
    :   'SET' NEWLINE!*
    ;

COMMENT
    :   ';' .* NEWLINE+ { $channel=HIDDEN; }
    ;

NEWLINE
    :  '\r'? '\n'
    ;

WS
    :   (' ' | '\r' | '\n' | '\t' | '\f')+ { $channel = HIDDEN; }
    ;

If I use an input like:

; comment line 1 with blank line after it

SET ; comment after command
; comment line again

I get an error when parsing this saying line 4:11 required (...)+ loop did not match anything at character '<EOF>'.

If I add a newline to the last line of the input, it works fine as the newline is matched by the comment stripping, and the EOF matches at end of rule.

How can I better write this so it ignores comments on the final line but doesn’t give an error? I don’t want to append anything to the original input to hack it, is there a cleaner way to read comment lines? I’ve tried all kinds of combinations of NEWLINE|EOF but nothing gets rid of the error.

  • 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-03T13:27:55+00:00Added an answer on June 3, 2026 at 1:27 pm

    Something like this should do it:

    COMMENT
        :   ';' ~('\r' | '\n')* { $channel=HIDDEN; }
        ;
    

    And if you want a COMMENT to potentially have a line break at the end, do:

    COMMENT
        :   ';' ~('\r' | '\n')* NEWLINE? { $channel=HIDDEN; }
        ;
    

    However, the two rules NEWLINE and WS:

    NEWLINE
        :  '\r'? '\n'
        ;
    
    WS
        :   (' ' | '\r' | '\n' | '\t' | '\f')+ { $channel = HIDDEN; }
        ;
    

    are dangerous: ANTLR works like this: it tries to match as much as possible, so the rule that matches the most “wins”. If two (or more) rules match the same amount of characters, the one defined first “wins”.

    In other words, if the lexer sees input like "\n", a NEWLINE is created. But if the lexer sees " \n" (a space followed by a "\n"), a WS token is created (and put on the HIDDEN channel).

    I’m not sure if line breaks really are significant in your language (their not in any flavor of assembly language, AFAIK) so simply remove the NEWLINE rule. If the are significant, remove both the chars \r and \n from the WS rule.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.