JavaCC转储方法来打印AST [英] JavaCC dump method to print AST
本文介绍了JavaCC转储方法来打印AST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用JavaCC以特定格式打印AST。
我需要这样:
I am using JavaCC to print an AST in a particular format. I need it to be like this :
LetNode( Identier(X), ExprNode( PlusNode( IntegerLiteral(8), IntegerLiteral(2) )))
但我得到:
Start(LetNode(Identifier(x)(ExprNode(IntegerLiteral(5)(PlusNode(IntegerLiteral(5)()))
我正在使用转储方法来打印此内容:
I am using the dump method to print this:
public void dump(String prefix) {
System.out.print(toString(prefix));
System.out.print("(");
if (children != null) {
for (int i = 0; i < children.length; ++i) {
SimpleNode n = (SimpleNode)children[i];
if (n != null) {
n.dump(prefix +"");
}
}
System.out.print(")");
}
}
}
未经任何修改的输出为:
The output without any modification is :
Start
Sxl
Statement
VariableDecl
Identifier
Type
Expression
SimpleExpression
Term
Factor
Literal
IntegerLiteral
AdditiveOp
Term
Factor
Literal
IntegerLiteral
我的方法:
void AdditiveOp():
{}
{
(Plus() /*#Add(2)*/ | Minus() | Or())
}
void Plus():
{
}
{
(< PLUS >)
#PlusNode
}
如果我删除/ * * /它不能正常运行,因为它说它期望其他的东西不可以+
If I remove the /* */ it does not work as it says it expects something else not +
任何帮助将不胜感激!
推荐答案
使用JJT进行AST表达式表达的AST的通常方法是执行类似的操作
The usual way to make an AST with JJT for an expression grammar is to do something like this
void AdditiveExpression() #void :
{ Token t ; }
{
MultiplicativeExpression()
( "+" MultiplicativeExpression() #Add(2)
| "-" MultiplicativeExpression() #Subtract(2)
)*
}
这篇关于JavaCC转储方法来打印AST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文