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?
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:
Now generate a lexer and parser (Python source files!) from it by executing the following on the command line:
And if you now execute the following script
you’ll see the following being printed to the console: