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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:21:30+00:00 2026-05-23T02:21:30+00:00

I was writing a simple grammar for a small language when I came across

  • 0

I was writing a simple grammar for a small language when I came across an issue. It’s throwing a NullPointerException when I try to interpret it. Oh, and this is ANTLRWorks 3.

Also – I specified that the language should be Python, but it still does stuff in Java 🙁 Why?

Here’s the input:

program Test1 = 
const t1 : int := 1;
const t2 : int := 2;
var x, y, z : int;
begin
x := 41;
y := 10;
z := 2 4 *;
end Test1.

Here’s my code:

grammar lang;

options {
    language=Python;
    output=AST;
    ASTLabelType=CommonTree;
}

tokens {DECL;} // an imaginary node

start : decl ;

decl : type ID ';' -> ^(DECL type ID)
     ;
type : INTTYPE  // automatic tree construction builds a node for this rule
     | FLOATTYPE
     ;
program
    : 'program' ID '='
    (const | variable)*
    'begin'
    statement*
    'end' ID '.'
    ;
const
    : 'const' ID ':' type ':=' expr ';'
    ;
variable
    : 'var' ID (',' ID)* ':' type ';'
    ;
statement
    : assignment
    ;
assignment
    : ID ':=' expr ';'
    ;



// expressions....fun!

term
    : ID
    | expr
    | INTTYPE
    ;
negation 
    : 'not'* term
    ;
unary
    : ('+' | '-')* negation
    ;
mult
    : unary (unary ('*' | '/' | 'mod'))*
    ;
add
    : mult (mult ('+' | '-'))*
    ;
relation
    : add (add ('==' | '!=' | '<' | '<=' | '>=' | '>'))*
    ;

expr
    : relation (relation ('and' | 'or'))* 
    ;

INTTYPE : 'int' ;
FLOATTYPE : 'float' ;
ID : ('a'..'z' | 'A'..'Z') ('a'..'z' | 'A'..'Z' | '0'..'9')* ;
INT : '0'..'9'+ ;
WS : (' '|'\n' | '\t') {$channel=HIDDEN;} ;

What am I doing wrong?

  • 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-23T02:21:31+00:00Added an answer on May 23, 2026 at 2:21 am

    As Kay already mentioned: there is no need to account for operator precedence in post-fix expressions. Post-fix expressions are just a list of one or more operands and operators. Here’s how to get your grammar working:

    grammar lang;
    
    options {
      language=Python;
      output=AST;
    }
    
    tokens {
      PROGRAM;
      STATS;
      DECL;
      ASSIGN;
      EXPR;
    }
    
    program
      :  'program' id=ID '=' decl* 'begin' statement* 'end' ID '.'
         -> ^(PROGRAM $id ^(DECL decl*) ^(STATS statement*))
      ;
    
    decl
      :  const 
      |  variable
      ;
    
    type 
      :  INTTYPE 
      |  FLOATTYPE
      ;
    
    const
      :  'const' ID ':' type ':=' expr ';' -> ^('const' type ID expr)
      ;
    
    variable
      :  'var' ID (',' ID)* ':' type ';' -> ^('var' type ID+)
      ;
    
    statement
      :  assignment
      ;
    
    assignment
      :  ID ':=' expr ';' -> ^(ASSIGN ID expr)
      ;
    
    expr
      :  exprAtom+ -> ^(EXPR exprAtom+)
      ;
    
    exprAtom
      :  operand 
      |  operator
      ;
    
    operand
      :  INT
      |  ID
      ;
    
    operator
      :  'and' | 'or' | '==' | '!=' | '<' | '<=' | '>=' | '>' | '+' | '-' | '*' | '/' | 'mod' | 'not'
      ;
    
    INTTYPE   : 'int' ;
    FLOATTYPE : 'float' ;
    ID        : ('a'..'z' | 'A'..'Z') ('a'..'z' | 'A'..'Z' | '0'..'9')* ;
    INT       : '0'..'9'+ ;
    WS        : (' '|'\n' | '\t') {$channel=HIDDEN;} ;
    

    Now generate a lexer and parser (Python source files!) from it by executing the following on the command line:

    java -cp antlr-3.1.3.jar org.antlr.Tool lang.g

    And if you now execute the following script

    #!/usr/bin/env python
    import antlr3
    from antlr3 import *
    from antlr3.tree import *
    from langLexer import *
    from langParser import *
    
    def print_level_order(tree, indent):
      print '{0}{1}'.format('   '*indent, tree.text)
      for child in tree.getChildren():
        print_level_order(child, indent+1)
    
    input = """
    program Test1 = 
    const t1 : int := 1;
    const t2 : int := 2;
    var x, y, z : int;
    begin
    x := 41;
    y := 10;
    z := 2 4 *;
    end Test1.
    """
    char_stream = antlr3.ANTLRStringStream(input)
    lexer = langLexer(char_stream)
    tokens = antlr3.CommonTokenStream(lexer)
    parser = langParser(tokens)
    tree = parser.program().tree 
    print_level_order(tree, 0)
    

    you’ll see the following being printed to the console:

    PROGRAM
       Test1
       DECL
          const
             int
             t1
             EXPR
                1
          const
             int
             t2
             EXPR
                2
          var
             int
             x
             y
             z
       STATS
          ASSIGN
             x
             EXPR
                41
          ASSIGN
             y
             EXPR
                10
          ASSIGN
             z
             EXPR
                2
                4
                *
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a simple scripting language on top of Java/JVM, where you can also
I'm writing a parser for a very simple grammar in javacc. It's beginning to
I am writhing a simple language with antlr, I defined a Lexer grammar in
Writing a simple regex, but I've never been very good at this. What I'm
I am writing a small, really simple lisp parser in ruby with the treetop
I'm writing simple program to communicate between smart devices and I receive 11001 when
I'm looking into writing simple graphics code in Android and I've noticed some synchronized()
I am writing simple site that requires users and profiles to be handled. The
How long does it take for an experienced Windows programmer to learn writing simple
I'm writing a simple OpenGL application that uses GLUT . I don't want to

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.