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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:42:44+00:00 2026-06-01T15:42:44+00:00

so I’m making some basic programming language (just for exercise). I’ve made simple grammar

  • 0

so I’m making some basic programming language (just for exercise). I’ve made simple grammar using ANTLR.

Let’s use this as example of simple program.

begin
    int a;
    a = 3+4*4;
end

And let’s test if that runs with following java class.

public class Test {
    public static void main(String[] args) throws RecognitionException {    
        CharStream charStream = new ANTLRStringStream("here goes the code");
        LangLexer lexer = new LangLexer(charStream);
        TokenStream tokeStream = new CommonTokenStream(lexer);
        LangParser parser = new LangParser(tokeStream);
        Lang.program();
        System.out.println("done");
    }
}

Now I’m stuck. I want to make, for example “input x” and “print x”. So that when you have “input x” in your code, the program wants you to enter some number, and stores given value in var X. And with print X it outputs that value in console.

begin
    int a, b, c;
    input a;
    input b;
    c = a + b;
    print c;
end

Any ideas and suggestions?
Thanks!

  • 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-01T15:42:45+00:00Added an answer on June 1, 2026 at 3:42 pm

    How to do that exactly is hard to explain since you need a lot of underlying stuff (grammar, AST walker, scope/symbol-table, etc.).

    I’ve written a blog that explains how to create a small programming language. If you read through it and/or download the zip of the last part, you only need to add a couple of things to support user-input. These are the changes you need to make:

    In file TL.g:

    atom
      :  Number
      |  Bool
      |  Null
      |  lookup
      |  Input '(' String? ')' -> ^(Input String?) // added this line
      ;
    
    Input : 'input'; // added this rule
    

    In file TLTreeWalker.g:

    expression returns [TLNode node]
      :  ^(TERNARY a=expression b=expression c=expression) {node = new TernaryNode($a.node, $b.node, $c.node);}
      |  ^(In a=expression b=expression)                   {node = new InNode($a.node, $b.node);}
      |  ^('||' a=expression b=expression)                 {node = new OrNode($a.node, $b.node);}
      |  ^('&&' a=expression b=expression)                 {node = new AndNode($a.node, $b.node);}
      |  ^('==' a=expression b=expression)                 {node = new EqualsNode($a.node, $b.node);}
      |  ^('!=' a=expression b=expression)                 {node = new NotEqualsNode($a.node, $b.node);}
      |  ^('>=' a=expression b=expression)                 {node = new GTEqualsNode($a.node, $b.node);}
      |  ^('<=' a=expression b=expression)                 {node = new LTEqualsNode($a.node, $b.node);}
      |  ^('>' a=expression b=expression)                  {node = new GTNode($a.node, $b.node);}
      |  ^('<' a=expression b=expression)                  {node = new LTNode($a.node, $b.node);}
      |  ^('+' a=expression b=expression)                  {node = new AddNode($a.node, $b.node);}
      |  ^('-' a=expression b=expression)                  {node = new SubNode($a.node, $b.node);}
      |  ^('*' a=expression b=expression)                  {node = new MulNode($a.node, $b.node);}
      |  ^('/' a=expression b=expression)                  {node = new DivNode($a.node, $b.node);}
      |  ^('%' a=expression b=expression)                  {node = new ModNode($a.node, $b.node);}
      |  ^('^' a=expression b=expression)                  {node = new PowNode($a.node, $b.node);}
      |  ^(UNARY_MIN a=expression)                         {node = new UnaryMinusNode($a.node);}
      |  ^(NEGATE a=expression)                            {node = new NegateNode($a.node);}
      |  Number                                            {node = new AtomNode(Double.parseDouble($Number.text));}
      |  Bool                                              {node = new AtomNode(Boolean.parseBoolean($Bool.text));}
      |  Null                                              {node = new AtomNode(null);}
      |  lookup                                            {node = $lookup.node;}
      |  ^(Input String?)                                  {node = new InputNode($String.text);} // added this line
      ;
    

    Add the following class:

    package tl.tree.functions;
    
    import tl.TLValue;
    import tl.tree.TLNode;
    
    import java.io.PrintStream;
    import java.util.Scanner;
    
    public class InputNode implements TLNode {
    
        private String prompt;
        private PrintStream out;
    
        public InputNode(String p) {
            this(p, System.out);
        }
    
        public InputNode(String p, PrintStream o) {
            prompt = (p == null) ? "" : p;
            out = o;
        }
    
        @Override
        public TLValue evaluate() {
            out.println(prompt);
            Scanner keyboard = new Scanner(System.in);
    
            if(keyboard.hasNextDouble()) 
                return new TLValue(Double.valueOf(keyboard.nextDouble()));
            else if(keyboard.hasNextInt()) 
                return new TLValue(Integer.valueOf(keyboard.nextInt()));
            else if(keyboard.hasNextBoolean()) 
                return new TLValue(Boolean.valueOf(keyboard.nextBoolean()));
            else 
                return new TLValue(keyboard.nextLine().trim()); // else it's a plain string
        }
    }
    

    And if you now evaluate the input:

    a = 10;
    b = input("Enter a number: ");
    println(a + b);
    

    you will be prompted with the message Enter a number: and this number is then added to a and will be printed to the console.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want use html5's new tag to play a wav file (currently only supported

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.