我实际上在用JavaScript实现antlr-visitor时遇到问题。我已经创建了一个语法。但是,我没有找到任何文档,示例或教程。以某种方式仅返回多维数组。那里发生了类似的问题:https://github.com/antlr/antlr4/issues/1995 不幸的是,此讨论中没有解决方案。在Java中,我已经有一个完成的访问者,因此只想将其转换为JS。因此,如果有没有监听器的解决方案,我将更愿意。 预先感谢您的帮助
编辑: 这是访问者,语法和启动工具的代码。
const antlr4 = require('antlr4');
const grammarLexer = require('./SimpleGrammarLexer');
const grammarParser = require('./SimpleGrammarParser');
const extendGrammarVisitor = require('./ExtendGrammarVisitor.js');
export class SimpleGrammar {
public static parseCode(formula: string) {
const inputStream = new antlr4.InputStream(formula);
const lexer = new grammarLexer.SimpleGrammarLexer(inputStream);
const commonTokenStream = new antlr4.CommonTokenStream(lexer);
const parser = new grammarParser.SimpleGrammarParser(commonTokenStream);
const visitor = new extendGrammarVisitor.ExtendGrammarVisitor();
const parseTree = parser.r();
visitor.visitR(parseTree);
}
}
grammar SimpleGrammar;
r: input;
INT : [0-9]+;
DOUBLE : [0-9]+'.'[0-9]+;
PI : 'pi';
E : 'e';
POW : '^';
NL : '\n';
WS : [ \t\r]+ -> skip;
ID : [a-zA-Z_][a-zA-Z_0-9]*;
PLUS : '+';
EQUAL : '=';
MINUS : '-';
MULT : '*';
DIV : '/';
LPAR : '(';
RPAR : ')';
input
: setVar NL input # ToSetVar
| plusOrMinus NL? EOF # Calculate
;
setVar
: ID EQUAL plusOrMinus # SetVariable
;
plusOrMinus
: plusOrMinus PLUS multOrDiv # Plus
| plusOrMinus MINUS multOrDiv # Minus
| multOrDiv # ToMultOrDiv
;
multOrDiv
: multOrDiv MULT pow # Multiplication
| multOrDiv DIV pow # Division
| pow # ToPow
;
pow
: unaryMinus (POW pow)? # Power
;
unaryMinus
: MINUS unaryMinus # ChangeSign
| atom # ToAtom
;
atom
: PI # ConstantPI
| E # ConstantE
| DOUBLE # Double
| INT # Int
| ID # Variable
| LPAR plusOrMinus RPAR # Braces
;
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var simpleGrammarVisitor = require('./SimpleGrammarVisitor.js');
var ExtendGrammarVisitor = (function (_super) {
__extends(ExtendGrammarVisitor, _super);
function ExtendGrammarVisitor() {
_super.apply(this, arguments);
}
ExtendGrammarVisitor.prototype.visitR = function(ctx) {
return this.visitChildren(ctx);
};
ExtendGrammarVisitor.prototype.visitToSetVar = function (ctx) {
return this.visitChildren(ctx);
};
ExtendGrammarVisitor.prototype.visitCalculate = function (ctx) {
return this.visitChildren(ctx);
};
ExtendGrammarVisitor.prototype.visitSetVariable = function (ctx) {
return this.visitChildren(ctx);
};
ExtendGrammarVisitor.prototype.visitToMultOrDiv = function (ctx) {
return this.visitChildren(ctx);
};
ExtendGrammarVisitor.prototype.visitPlus = function (ctx) {
var example = this.visit(ctx.plusorminus(0)); // ???
return this.visitChildren(ctx);
};
ExtendGrammarVisitor.prototype.visitMinus = function (ctx) {
return this.visitChildren(ctx);
};
ExtendGrammarVisitor.prototype.visitMultiplication = function (ctx) {
return this.visitChildren(ctx);
};
ExtendGrammarVisitor.prototype.visitDivision = function (ctx) {
return this.visitChildren(ctx);
};
ExtendGrammarVisitor.prototype.visitToPow = function (ctx) {
return this.visitChildren(ctx);
};
ExtendGrammarVisitor.prototype.visitPower = function (ctx) {
return this.visitChildren(ctx);
};
ExtendGrammarVisitor.prototype.visitChangeSign = function (ctx) {
return this.visitChildren(ctx);
};
ExtendGrammarVisitor.prototype.visitToAtom = function (ctx) {
return this.visitChildren(ctx);
};
ExtendGrammarVisitor.prototype.visitConstantPI = function (ctx) {
return this.visitChildren(ctx);
};
ExtendGrammarVisitor.prototype.visitConstantE = function (ctx) {
return this.visitChildren(ctx);
};
ExtendGrammarVisitor.prototype.visitDouble = function (ctx) {
return this.visitChildren(ctx);
};
ExtendGrammarVisitor.prototype.visitInt = function (ctx) {
return this.visitChildren(ctx);
};
ExtendGrammarVisitor.prototype.visitVariable = function (ctx) {
return this.visitChildren(ctx);
};
ExtendGrammarVisitor.prototype.visitBraces = function (ctx) {
return this.visitChildren(ctx);
};
return ExtendGrammarVisitor;
}(simpleGrammarVisitor.SimpleGrammarVisitor));
exports.ExtendGrammarVisitor = ExtendGrammarVisitor;
目前,我不知道如何继续和实现访问者的方法。
答案 0 :(得分:1)
在JavaScript visitChildren
中返回一个数组,其中包含访问子级的结果。因此,例如,如果一个节点有两个孩子,那么visitChildren(node)
将返回[visit(node.child1), visit(node.child2)]
(而在Java中,您只会得到visit(node.child2)
的结果,而visit(node.child1)
的结果将是被丢弃)。如果只有一个孩子,则会得到一个仅包含一个元素的数组(而在Java中,您只会得到不包含该元素的元素)。
在您的代码中,您到处都在调用visitChildren
,因此这就是为什么您使用嵌套数组的原因。如果您不希望使用数组,则不应该调用visitChildren
或对其进行调用,解压缩数组然后返回其他内容。例如,以下两种方法可以实现visitPlus
以返回加法结果(如果这是目标):
// Using visitChildren and unpacking
ExtendGrammarVisitor.prototype.visitPlus = function (ctx) {
let [lhs, rhs] = this.visitChildren(ctx);
return lhs+rhs;
};
// Or without visitChildren
ExtendGrammarVisitor.prototype.visitPlus = function (ctx) {
return this.visit(ctx.plusOrMinus()) + this.visit(ctx.multOrDiv());
// If you added labels to the grammar, you could write the above more readably
// using labels like `lhs` and `rhs` instead of `plusOrMinus` and `multOrDiv`,
// so the reader doesn't have to remember which one is the right and which one
// the left operand
};
当然,要执行此操作,还需要更改所有其他访问者方法以返回数字。而且,如果您希望访客执行除评估表达式之外的其他操作,则需要相应地调整代码。
PS:您可以通过使用ANTLR4的优先级处理规则来简化语法,除非您指定<assoc=right>
,否则该规则允许您按优先级从高到低的顺序列出中缀运算符,并且运算符保持左关联。这样,您可以将所有表达规则合并为一个规则,如下所示:
expr
: PI # ConstantPI
| E # ConstantE
| DOUBLE # Double
| INT # Int
| ID # Variable
| LPAR expr RPAR # Braces
| MINUS expr # ChangeSign
| <assoc=right> lhs=expr op=POW rhs=expr # InfixExpression
| lhs=expr op=(MULT|DIV) rhs=expr # InfixExpression
| lhs=expr op=(PLUS|MINUS) rhs=expr # InfixExpression
;