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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:59:46+00:00 2026-05-26T12:59:46+00:00

How to create a parser rule that accept whatever the previous rules doesn’t accept?

  • 0

How to create a parser rule that accept whatever the previous rules doesn’t accept?

What I am doing is that I try to rewrite c++ src files with ANTLR. My grammar only need to understand a subset of C++ and ignore the rest. By ignoring the rest, I mean I must still output the input line as it is. I cannot simply drop the input. For example, I may need to locate #if, #ifdef, #ifndef, #else, #elif, #endif but send any other valid C++ syntax back the to the output as it is.

Part of my solution looks like:

inputLines :  ( preprocessorLineSet  |  oneNormalInputLine ) ;
preprocessorLineSet  : ....;// pattern to match #if #else etc
oneNormalInputLine : (any_token_except_crlf)* CRLF {System.out.println($text)}; 
// a catch-all rule for anything including #if #else #endif, it must send any unrecognised input back to the ouput

I am assuming the parser would try the alternatives in the order listed in the grammar. So my preprocessorLineSet rule is listed before oneNormalInputLine in the inputLines rule. But, it seems like ANTLR still prefer oneNormalInputLine even if the input is of the #if pattern which I assume should be matched by the previous rule.

Is my assumption correct? Is it a correct way to implement this kind of ignore-the-rest logic?

  • 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-26T12:59:47+00:00Added an answer on May 26, 2026 at 12:59 pm

    JavaMan wrote:

    I am assuming the parser would try the alternatives in the order listed in the grammar. So my preprocessorLineSet rule is listed before oneNormalInputLine in the inputLines rule.

    Correct, the rules are tried from left to right (preprocessorLineSet before oneNormalInputLine).

    JavaMan wrote:

    But, it seems like ANTLR still prefer oneNormalInputLine even if the input is of the #if pattern which I assume should be matched by the previous rule.

    Wouldn’t you need to exclude stuff like #if and #elif from any_token_except_crlf? Could you post a working example including a driver class that shows the unexpected behavior?

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

Sidebar

Related Questions

I'm using CUP to create a parser that I need for my thesis. I
I'm trying to create a parser using Treetop that is somewhat recursive. An expression
I am trying to write an ANTLR parser rule that matches a list of
Antlr users usually create a parser that generates the AST(Abstract syntax tree), and a
I've been trying to create a parser using simpleparse. I've defined the grammar like
I am trying to create a generic formatter/parser combination. Example scenario: I have a
I want to create a script that parses or makes sense of apache's error
I have a script that parse the database and create php classes to work
Im trying to create a parser for a small language with commands including labels
UPDATE: It was suggested in the comments that I create a wiki for 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.