ANTLR 语法 后缀
我在为一种小语言编写简单的语法时遇到了一个问题。当我尝试解释它时,出现了一个NullPointerException
的错误。哦,对了,我是在使用ANTLRWorks 3。
另外,我明明指定了语言应该是Python,但它还是在用Java做事情 :( 这是为什么呢?
这是我的输入:
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;} ;
我哪里做错了呢?
1 个回答
2
正如Kay已经提到的:在后缀表达式中,不需要考虑运算符的优先级。后缀表达式只是一个包含一个或多个操作数和运算符的列表。下面是让你的语法正常工作的方式:
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源文件!):
java -cp antlr-3.1.3.jar org.antlr.Tool lang.g
如果你现在执行以下脚本
#!/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)
你会看到控制台打印出以下内容:
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
*