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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:57:09+00:00 2026-05-27T07:57:09+00:00

Given a String like.. (a+(a+b)), (d*e) :- (e-f) Note: (d*e) and (e-f) are different

  • 0

Given a String like..

(a+(a+b)), (d*e) :- (e-f)

Note: (d*e) and (e-f) are different expressions. How can I fetch the expressions from this string. I have the grammar defined as..

parse returns [String value]
  :  addExp {$value=$addExp.value;} EOF
  ;

addExp returns [String value]
  :  multExp {$value=$multExp.value;} (('+' | '-' | '*') multExp{$value+= '+' + $multExp.value;})*
  ;

multExp returns [String value]
  :  atom {$value=$atom.value;} (('*' | '/') atom {$value+=$atom.value;)*
  ;

atom returns [String value]
  :  x=ID {$value=$x.text;}
  |  '(' addExp ')' {$value='('+$addExp.value+')';}
  ;

ID    : 'a'..'z' | 'A'..'Z';

I tried..

ANTLRStringStream a=new ANTLRStringStream("(a+(a+b)), (d*e) :- (e-f)");
SLexer l=new SLexer(a);
CommonTokenStream c=new CommonTokenStream(l);
SParser p=new Sparser(c);

String exp;
while(exp = p.parse())
{
 System.out.println(exp);
}

I’m thinking of something like hasNext() and then fetching.

  • 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-27T07:57:10+00:00Added an answer on May 27, 2026 at 7:57 am

    Your lexer rules TEXT possibly matches an empty string, causing the lexer to create an infinite amount of tokens. Also, you don’t need all those return statements after your rule: you can simply grab what a parser (or lexer) rule matched by adding .text after it.

    You could let your parser return a List<String>, or let it return a single String repeatedly invoke that parser rule until EOF is encountered.

    A little demo:

    grammar T;
    
    @parser::members {
      public static void main(String[] args) throws Exception {
        String src = "likes(a, b) :- likes(a, X), likes(X, b). hates(a, b) " + 
            ":- hates(a,X), hates(X,b). likes(a,b) :- says(god, likes(a,b)).";
        TLexer lexer = new TLexer(new ANTLRStringStream(src));
        TParser parser = new TParser(new CommonTokenStream(lexer));
        List<String> statements = parser.parse();
        for(String s : statements) {
          System.out.println(s);
        }
      }
    }
    
    parse returns [List<String> statements]
    @init{$statements = new ArrayList<String>();}
      :  (statement {$statements.add($statement.text);} ~TEXT+)+ EOF
      ;
    
    statement
      :  TEXT OPAR params CPAR
      ;
    
    params
      :  (param (COMMA param)*)?
      ;
    
    param
      :  TEXT
      |  statement
      ;
    
    COMMA : ',';
    OPAR  : '(';
    CPAR  : ')';
    TEXT  : ('a'..'z' | 'A'..'Z')+;
    SPACE : (' ' | '\t') {$channel=HIDDEN;};
    OTHER : . ;
    

    Note that ~TEXT+ in the parse rule matches one or more tokens other than TEXT.

    If you now create a lexer and parser and run the TParser class:

    *nix/MacOS

    java -cp antlr-3.3.jar org.antlr.Tool T.g
    javac -cp antlr-3.3.jar *.java
    java -cp .:antlr-3.3.jar TParser
    

    or

    Windows

    java -cp antlr-3.3.jar org.antlr.Tool T.g
    javac -cp antlr-3.3.jar *.java
    java -cp .;antlr-3.3.jar TParser
    

    you will see the following being printed to your console:

    likes(a, b)
    likes(a, X)
    likes(X, b)
    hates(a, b)
    hates(a,X)
    hates(X,b)
    likes(a,b)
    says(god, likes(a,b))
    

    EDIT

    And here’s how to return a single String opposed to a List<String>:

    @parser::members {
      public static void main(String[] args) throws Exception {
        String src = "likes(a, b) :- likes(a, X), likes(X, b). hates(a, b) " + 
            ":- hates(a,X), hates(X,b). likes(a,b) :- says(god, likes(a,b)).";
        TLexer lexer = new TLexer(new ANTLRStringStream(src));
        TParser parser = new TParser(new CommonTokenStream(lexer));
        String s;
        while((s = parser.parse()) != null) {
          System.out.println(s);
        }
      }
    }
    
    parse returns [String s]
      :  statement ~(TEXT| EOF)* {$s = $statement.text;}
      |  EOF                     {$s = null;}
      ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I create an array in smarty from a given string like 22||33||50
Given a string like this: a,string, with,various,values, and some,quoted What is a good algorithm
Given a string like so: Hello {FIRST_NAME}, this is a personalized message for you.
given an xml string like this: <some><nested><xml>value</xml></nested></some> what's the best option (using ruby) to
Given a UTC time string like this: 2005-11-01T00:00:00-04:00 What is the best way to
In most languages like C# for example given a string you can test (boolean)
Given a struct like this: public struct SomeStruct { public SomeStruct(String stringProperty, Int32 intProperty)
Given a string like this: <a href=http://blah.com/foo/blah>This is the foo link</a> ... and a
I kind of stuck on this. I have a string like 18=abcd1, 19=jghrt23, 20=outut
Given a string like String a=- = - - What is your name?; How

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.