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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:16:51+00:00 2026-06-10T02:16:51+00:00

I already know the workaround for this problem, but I would like to really

  • 0

I already know the workaround for this problem, but I would like to really use this one approach, for at least one reason — it should work.

This is rule taken from “The Definitive ANTLR Reference” by Terence Parr (the books is for ANTLR3):

expr : (INT -> INT) ('+' i=INT -> ^('+' $expr $i) )*;

If INT is not followed by + the result will be INT (single node), if it is — subtree will be built with first INT (referred as $expr) as left branch.

I would like to build similar rule, yet with custom action:

mult_expr : (pow_expr -> pow_expr ) 
            (op=MUL exr=pow_expr 
              -> { new BinExpr($op,$mult_expr.tree,$exr.tree) })*; 

ANTLR accepts such rule, but when I run my parser with input (for example) “5 * 3” it gives me an error “line 1:1 missing EOF at ‘*’5”.

QUESTION: how to use back reference with custom rewrite action?

  • 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-10T02:16:52+00:00Added an answer on June 10, 2026 at 2:16 am

    I am persistent guy, and this idea of using my custom nodes in one step was bothering me… 😉

    So, I did. The crucial points are:

    • putting EOF! at the end of the “main” rule,

    • when labeling the tokens, putting labels next to token, not to group, so (op='*'|op='/'), not op=('*'|'/')

    I don’t know for sure if this approach of using grammar rules to create immediately custom nodes will be a good a idea, but since this solves the problem asked in question I am marking this as solution.

    And for the record, the most interesting rule looks now like this:

    mult_expr : (exl=pow_expr -> $exl ) 
            ((op=MUL|op=IDIV|op=RDIV|op=MOD) exr=pow_expr 
            -> { new BinaryExpression($op,$exl.tree,$exr.tree) })*; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I already know the answer to this one, but i hope maybe
I think I already know the answer to this but thought I would ask
I already know that most implementations use a single thread, but is there anything
Note: I have a workaround to this problem, but I'd prefer to comma-separate the
Look, I know we should move on and install the newest php already. But
Apache 2.2.17 PHP 5.3.3 Currently, my app doesn't use gzip, but I would like
I already know how to start a one new activity when you click a
I already know about Ghostscript front end viewers; but I was wondering how gs
I am experiencing something weird and have a workaround already, but I don't think
I know this flavor of question has been asked multiple times, but I've spent

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.