问题描述
当我遇到一个问题时,我正在为一种小语言编写一个简单的语法.当我尝试解释它时,它会抛出一个 NullPointerException
.哦,这是 ANTLRWorks 3.
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.
另外 - 我指定语言应该是 Python,但它仍然使用 Java :( 为什么?
Also - I specified that the language should be Python, but it still does stuff in Java :( Why?
这是输入:
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.
这是我的代码:
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;} ;
我做错了什么?
推荐答案
正如 Kay 已经提到的:无需在后缀表达式中考虑运算符的优先级.后缀表达式只是一个或多个操作数和运算符的列表.以下是让您的语法发挥作用的方法:
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;} ;
现在通过在命令行上执行以下命令来生成词法分析器和解析器(Python 源文件!):
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
*
这篇关于ANTLR 语法后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!