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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:24:06+00:00 2026-06-15T00:24:06+00:00

I have already looked at this question and even though the question titles seem

  • 0

I have already looked at this question and even though the question titles seem to be the same; it doesn’t answer my question, at least not in any way that I can understand.

Parsing Math

Here is what I am parsing:

PI -> 3.14.
Number area(Number radius) -> PI * radius^2.

This is how I want my AST tree to look, minus all the useless root nodes.

how it should look http://vertigrated.com/images/How%20I%20want%20the%20tree%20to%20look.png

Here are what I hope are the relevant fragments of my grammar:

term : '(' expression ')'
     | number -> ^(NUMBER number)
     | (function_invocation)=> function_invocation 
     | ATOM
     | ID
     ;

power : term ('^' term)* -> ^(POWER term (term)* ) ;
unary : ('+'! | '-'^)* power ;
multiply : unary ('*' unary)* -> ^(MULTIPLY unary (unary)* ) ;
divide : multiply ('/' multiply)* -> ^(DIVIDE multiply (multiply)* );
modulo : divide ('%' divide)* -> ^(MODULO divide (divide)*) ;
subtract : modulo ('-' modulo)* -> ^(SUBTRACT modulo (modulo)* ) ;  
add : subtract ('+' subtract)* -> ^(ADDITION subtract (subtract)*) ;

relation : add (('=' | '!=' | '<' | '<=' | '>=' | '>') add)* ;

expression : relation (and_or relation)*
           | string  
           | container_access
           ;
and_or : '&' | '|' ;

Precedence

I still want to keep the precedence as illustrated in the following diagrams, but want to eliminate the useless nodes if at all possible.

Source: Number a(x) -> 0 - 1 + 2 * 3 / 4 % 5 ^ 6.

Here are the nodes I want to eliminate:

how I want the precedence tree to look http://vertigrated.com/images/example%202%20desired%20result.png

Basically I want to eliminate any of those nodes that don’t directly have a branch under them to binary options.

  • 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-15T00:24:08+00:00Added an answer on June 15, 2026 at 12:24 am

    You must realize that the two rules:

    add : sub ( ('+' sub)+ -> ^(ADD sub (sub)*) | -> sub ) ;
    

    and

    add : sub ('+'^ sub)* ;
    

    do not produce the same AST. Given the input 1+2+3, the first rule will produce:

      ADD
       |
    .--+--.
    |  |  |
    1  2  3
    

    where the second rule produces:

         (+)
          |
       .--+--.  
       |     |
      (+)    3
       |
    .--+--.
    |     |
    1     2
    

    The latter makes more sense: infix expressions are expected to have 2 child nodes, not more.

    Why not simply remove the literals in your parser rules and just do:

    add : sub (ADD^ sub)*;
    
    ADD : '+';
    

    Creating the same AST using a rewrite rule would look like this:

    add : (sub -> sub) ('+' s=sub -> ^(ADD $add $s))*;
    

    Also see chapter 7: Tree Construction from The Definitive ANTLR Reference. Especially the paragraphs Rewrite Rules in Subrules (page 173) and Referencing Previous Rule ASTs in Rewrite Rules (page 174/175).

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

Sidebar

Related Questions

I have already looked at this question but there was no solution. Here is
Have already looked at this stack question , at which point I looked at
I have already looked at this similar question but i am still wondering if
Quick question (and sorry if it's already been asked, I have looked but couldn't
I have already surveyed SO for an answer, and could not find an appropriate
I've already posted this question here , but since it's maybe not that Qt-specific,
Before I posted this question, I already looked this , but I couldn't get
I have honestly looked for over an hour for a satisfactory answer to this
I'm new at this and have looked at several posts already related to this
I've already looked at this question on representing strings in Python but my question

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.