我实际上在JavaScript中实现ANTLR-Visiter有问题。我已经创建了语法。但是,我没有发现文档,示例或教程。某种程度上只返回多维阵列。那里发生了类似的问题: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 : [ tr]+ -> 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;
在这一点上,我不知道如何继续并实施访客的方法。
在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的优先处理规则来简化语法,这使您可以以优先顺序列出Infix Operator,除非您指定<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
;