我找不到任何说明如何在 Visual Studio 2022 中获得 ANTLR 支持的文档。似乎 ANTLR 扩展开发人员没有跟上对 Visual Studio API 对象模型所做的更改:ANTLR VSIX仅与 Visual 兼容Studio 2019 和ANTLR 语言支持不适用于 Visual Studio 2017 以后的任何版本。
如何使用 Visual Studio 2022 在 .NET 6.0 中使用 ANTLR 语法并构建解析器?
我正在尝试创建一个行首标记:
lexer grammar ScriptLexer;
BOL : {getCharPositionInLine() == 0;}; // Beginning Of Line token
Run Code Online (Sandbox Code Playgroud)
但上面发出错误
The name 'getCharPositionInLine' does not exist in the current context
Run Code Online (Sandbox Code Playgroud)
当它创建此代码时:
private void BOL_action(RuleContext _localctx, int actionIndex) {
switch (actionIndex) {
case 0: getCharPositionInLine() == 0; break;
}
}
Run Code Online (Sandbox Code Playgroud)
当getCharPositionInLine()方法不存在...
我定义了一个 ANTLR4 语法,其中词法分析器和解析器位于两个不同的文件中。它是一种类似于 XML 的语法。在我的解析器中,我也想定义一个可以重复使用自身用于计算目的的表达式(如“(1+1)*2”)。
parser grammar TestExpressionParser;
options { tokenVocab=TestExpressionLexer; }
compileUnit : calculator ;
calculator : '<' 'calculator' '>' tiers? rules?'</' 'calculator' '>' ;
// tiers
tiers : '<' 'tiers' '>' tier* '</' 'tiers' '>' ;
tier : '<' 'tier' attrReference '>' '</' 'tier' '>' ;
// rules
rules : '<' 'rules' '>' (rule) '</' 'rules' '>' ;
rule : '<' 'rule' '>' (calculation|reference|description)* '</' 'rule' '>' ;
calculation : '<' 'calculation' '>' expression '</' 'calculation' '>' ; …Run Code Online (Sandbox Code Playgroud)