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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:25:45+00:00 2026-05-13T09:25:45+00:00

I’m still on my quest for a really simple language and I know now

  • 0

I’m still on my quest for a really simple language and I know now that there are none. So I’m writing one myself using ANTLR3.

I found a really great example in this answer:

Exp.g:

grammar Exp;

eval returns [double value]
    :    exp=additionExp {$value = $exp.value;}
    ;

additionExp returns [double value]
    :    m1=multiplyExp       {$value =  $m1.value;} 
         ( '+' m2=multiplyExp {$value += $m2.value;} 
         | '-' m2=multiplyExp {$value -= $m2.value;}
         )* 
    ;

multiplyExp returns [double value]
    :    a1=atomExp       {$value =  $a1.value;}
         ( '*' a2=atomExp {$value *= $a2.value;} 
         | '/' a2=atomExp {$value /= $a2.value;}
         )* 
    ;

atomExp returns [double value]
    :    n=Number                {$value = Double.parseDouble($n.text);}
    |    '(' exp=additionExp ')' {$value = $exp.value;}
    ;

Number
    :    ('0'..'9')+ ('.' ('0'..'9')+)?
    ;

WS  
    :   (' ' | '\t' | '\r'| '\n') {$channel=HIDDEN;}
    ;

Java Code:

public Double evaluate(String string, Map<String, Double> input) throws RecognitionException {
    ANTLRStringStream in = new ANTLRStringStream(string);
    ExpLexer lexer = new ExpLexer(in);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    return new ExpParser(tokens).eval();
}

Using this ANTLR grammer I can evaluate expressions like

(12+14)/2

and get 13 as a result.

Now the only thing missing for my use-case is a way to inject simple double variables into this, so that I can evaluate the following by supplying {“A”: 12.0, “B”:14.0} as the input map:

(A+B)/2

Any ideas?

  • 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-13T09:25:46+00:00Added an answer on May 13, 2026 at 9:25 am

    You could create a Map<String, Double> memory in your parser and introduce a Identifier in your grammar:

    Identifier
      :  ('a'..'z' | 'A'..'Z' | '_') ('a'..'z' | 'A'..'Z' | '_' | '0'..'9')*
      ;
    

    Then your atomExp parser rule would look like this:

    atomExp returns [double value]
        :    n=Number                {$value = Double.parseDouble($n.text);}
        |    i=Identifier            {$value = memory.get($i.text);} // <- added!
        |    '(' exp=additionExp ')' {$value = $exp.value;}
        ;
    

    Here’s a small (complete) demo:

    grammar Exp;
    
    @parser::members {
    
      private java.util.HashMap<String, Double> memory = new java.util.HashMap<String, Double>();
    
      public static Double eval(String expression) throws Exception {
        return eval(expression, new java.util.HashMap<String, Double>()); 
      }
    
      public static Double eval(String expression, java.util.Map<String, Double> vars) throws Exception {
        ANTLRStringStream in = new ANTLRStringStream(expression);
        ExpLexer lexer = new ExpLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        ExpParser parser = new ExpParser(tokens);
        parser.memory.putAll(vars);
        return parser.parse(); 
      }
    }
    
    parse returns [double value]
        :    exp=additionExp {$value = $exp.value;}
        ;
    
    additionExp returns [double value]
        :    m1=multiplyExp      {$value =  $m1.value;} 
            ( '+' m2=multiplyExp {$value += $m2.value;} 
            | '-' m2=multiplyExp {$value -= $m2.value;}
            )*  
        ;
    
    multiplyExp returns [double value]
        :   a1=atomExp       {$value =  $a1.value;}
            ( '*' a2=atomExp {$value *= $a2.value;} 
            | '/' a2=atomExp {$value /= $a2.value;}
            )*  
        ;
    
    atomExp returns [double value]
        :    n=Number                {$value = Double.parseDouble($n.text);}
        |    i=Identifier            {$value = memory.get($i.text);}
        |    '(' exp=additionExp ')' {$value = $exp.value;}
        ;
    
    Identifier
        :    ('a'..'z' | 'A'..'Z' | '_') ('a'..'z' | 'A'..'Z' | '_' | '0'..'9')*
        ;
    
    Number
        :    ('0'..'9')+ ('.' ('0'..'9')+)?
        ;
    
    WS  
        :   (' ' | '\t' | '\r'| '\n') {$channel=HIDDEN;}
        ;
    

    And now theres no need to instantiate the parser/lexer yourself, you can simply do:

    import org.antlr.runtime.*;
    import java.util.*;
    
    public class ANTLRDemo {
        public static void main(String[] args) throws Exception {
            Map<String, Double> vars = new HashMap<String, Double>();
            vars.put("two", 2.0);
            vars.put("pi", Math.PI);
            System.out.println(ExpParser.eval("two * pi", vars));
        }
    }
    

    which will produce:

    6.283185307179586
    

    Good luck!

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

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
We're building an app, our first using Rails 3, and we're having to build

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.