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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:02:23+00:00 2026-06-13T05:02:23+00:00

i try to do some tree to tree transform with antlr3.4 It’s (for this

  • 0

i try to do some tree to tree transform with antlr3.4

It’s (for this question) about boolean expressions were “AND” and “OR” are allowed to bind to n expressions.
The parser stage creates something like this

 (OR 
   (AND (expr1) (expr2) (expr3) 
     (OR (AND (expr4))
         (AND (expr5))
         (AND (expr6))
     )
   )
 )

Unfortunately there are AST nodes for “AND” and “OR” that bind just to one expression. (Which is useless, but hey – rules andExpr and orExpr are invoked)

I tried to kick them out (mean, replace them by their subnodes) but fail to do so in a tree grammar.
(BTW: Using a depth first tree traversal/modification in pure java works, but that’s not my intention)

I tried to use predicates but i can’t seem to get it right.

This is the grammar to parse the stream unmodified

 start  :  
   orExpr^   EOF!  
   ;

 orExpr       :  
   ^(OR  r+=andExpr+ )   -> ^(OR $r)
   ;

 andExpr  : 
   ^(AND unaryExpr+ )
   ; 

 notExpr:
   ^( NOT unaryExpr)    
   ;

 unaryExpr : 
   .+  // it gets more complicated below this
   ;

I tried a predicate to catch the one-subnode-case but fail to pass the n>1 case unmodified

 orExpr @init { int N = 0; }
   :  
   ( ^(OR  (r+=andExpr {N++;})+ )  {N==1}? -> $r) 
   ;

Any Ideas how to do it right?

edit:
Attached is the parser grammar which is pretty much the same…

 start 
   :  '('! orExpr^  ')'! EOF!        ;
 orExpr
   : a+=andExpr (  OR_T a+=andExpr )*  -> ^(OR  $a+ )  // 'AND' and 'OR' are multivalent
   ;

 andExpr
   : u+=unaryExpr ( AND_T u+=unaryExpr )* -> ^(AND $u+ )
   ; 

 notExpr
   : NOT_T unaryExpr -> ^( NOT unaryExpr)   
   ;

 unaryExpr
   : '('!  orExpr ')'! // -> ^( BRACE orExpr), brace not needed in the ast (but needed for propper parsing)
   |   notExpr
   |   internal^  // internal is very complex in itself
   ;
  • 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-13T05:02:23+00:00Added an answer on June 13, 2026 at 5:02 am

    You can do this directly in the parser. You do need to create some more parser rules as to not confuse ANTLR in the rewrite rules (see the inline comments):

    grammar T;
    
    options {
      output=AST;
      ASTLabelType=CommonTree;
    }
    
    start 
     : orExpr EOF! {System.out.println($orExpr.tree.toStringTree());}
     ;
    
    orExpr
     : (andExpr2 -> andExpr2) ((OR andExpr)+ -> ^(OR andExpr2 andExpr+))?
     ;
    
    // You can't use `andExpr` directly in the `orExpr` rule otherwise the rewrite
    // rule `-> ^(OR ... )` gets confused.
    andExpr2 : andExpr;
    
    andExpr
     : (notExpr2 -> notExpr2) ((AND notExpr)+ -> ^(AND notExpr2 notExpr+))?
     ; 
    
    notExpr2 : notExpr;
    
    notExpr
     : NOT^ notExpr
     | atom  
     ;
    
    atom
     : '(' orExpr ')' -> orExpr
     | ID
     ;
    
    OR    : '||';
    AND   : '&&';
    NOT   : '!';
    ID    : 'a'..'z'+;
    SPACE : ' ' {skip();};
    

    Parsing input like "a && b && c || d || f || g" will produce the following AST:

    enter image description here

    EDIT

    The tree grammar would then look like this:

    tree grammar TWalker;
    
    options {
      tokenVocab=T;
      ASTLabelType=CommonTree;
    }
    
    start 
     : expr
     ;
    
    expr
     : ^(OR expr+)
     | ^(AND expr+)
     | ^(NOT expr)
     | ID
     ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read Japanese, and want to try processing some Japanese text. I tried this
I try to draw some lines with different colors. This code tries to draw
I try to filter and get some set of objects using this segment. baseSet
I try to connect to Facebook throught Facebook API, I follow this example: https://github.com/facebook/facebook-android-sdk/tree/master/examples/simple
I might be barking up the wrong tree with my question, so I'll try
I have some questions about development and deployment. I'll try to be clear: Notes:
I've built a tree image, see question . Now I've some major groups. One
In a recent question I was encouraged to try using some basic data structures
In addition to this question: Preorder tree traversal copy folder I was wondering if
i have this problem in codeigniter: I try to make a navigation tree system

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.