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

  • SEARCH
  • Home
  • 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 3307960
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:28:00+00:00 2026-05-17T21:28:00+00:00

I might be asking a stupid/basic question but i had been confused about ANTLR

  • 0

I might be asking a stupid/basic question but i had been confused about ANTLR AST building.

What i want to made is a kind of Boolean expression parser such that on parent nodes i have operator and its operands as children.
for instance, a sentence

( ( A B C & D ) | ( E & ( F | G ) ) )

should ideally be representing

              |
             / \
            /   \
           /     \
          /       \
         &         &
        / \       / \
       /   \     /   \
      /     D   E     |
     /|\             / \
    A B C           /   \
                   F     G 

From the following grammar.

grammar Test;

options
{
   language = 'Java';
   output=AST;
}


exp    :    word (expRest^)? | '('! exp ')'! (expRest^)?  ;

expRest :    (('&'|'|'|'!'|'&!'|'|!')^) exp | (('~'^) digit+ exp);
word    :   letter letter* -> ^(letter letter*);
letter  :        '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'|'a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'i'|'j'|'k'|'l'|'m'|'n'|'o'|'p'|'q'|'r'|'s'|'t'|'u'|'v'|'w'|'x'|'y'|'z'|'A'|'B'|'C'|'D'|'E'|'F'|'G'|'H'|'I'|'J'|'K'|'L'|'M'|'N'|'O'|'P'|'Q'|'R'|'S'|'T'|'U'|'V'|'W'|'X'|'Y'|'Z';
digit   :    '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9';

The problem is, that i am getting ‘A B C’ as either a list(array) of nodes as children of ‘&’.

Is it possible to restrict it as a single string??? i.e. ‘A B C’
or in other words, is it possible to have multiple characters at root node in AST??? If yes then how can i achieve it?

for reference, i want to make a syntax tree of ‘risk factors & current economic state’

P.S. I have also tried :

word    :   (letter letter*)^ ;

And just for a reference, I am using .NET environment.

  • 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-17T21:28:01+00:00Added an answer on May 17, 2026 at 9:28 pm

    You can insert imaginary tokens in your grammar that will be the root of “groups” of words. I don’t think it’s a good idea to glue the A, B and C together since you’re probably need them separate, right?

    I couldn’t really figure out what you’re exactly trying to do, so here a little demo you can (try to) get your head around:

    grammar BoolExp;
    
    options { 
      output=AST; 
    }
    
    tokens {
      MultiWord;
    }
    
    parse
      :  booleanExp EOF!
      ;
    
    booleanExp
      :  orExp
      ;
    
    orExp
      :  andExp ('|'^ andExp)*
      ;
    
    andExp
      :  notExp ('&'^ notExp)*
      ;
    
    notExp
      :  '!'^ atom
      |  atom
      ;
    
    atom
      :  '(' booleanExp ')' -> booleanExp
      |  WORD WORD+         -> ^(MultiWord WORD+)
      |  WORD
      ;
    
    WORD
      :  ('a'..'z' | 'A'..'Z')+
      ;
    
    SPACE
      :  (' ' | '\t' | '\r' | '\n'){skip();}
      ;
    

    If you generate a parser from it and test it with the input:

    ( ( A B C & D ) | ( E & ( F | G ) ) )
    

    you’ll get the following AST:

    alt text

    • image created using http://graph.gafol.net/

    I did not post my (Java) test class that generated the DOT file that was used to create the AST image above since you said you’re using the .NET target. If you do want to have a look at it, leave a comment and I’ll post it as well.

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

Sidebar

Related Questions

This might be a stupid question, but I just saw a question asking how
I might be asking a question that's already been answered, but I cannot find
This might seem like a stupid question but it's been a long day. I'm
This question might seem stupid but i nonetheless i believe it's a worth asking
OK I might be asking a stupid question, but I'm banging my head over
This might seem like a stupid question I admit. But I'm in a small
I might be asking a very basic question and I am sorry for that.
I'm looking for a single answer (but I might be asking the wrong question)
This might be rediculous, asking about such a huge bit of code...but basically I
This might seem like a silly question, but after asking some questions on stackoverflow

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.