我正在使用Antlr4cs-4.3.0与Visual Studio 2012, . net 4.5。我已经成功地生成并运行了一个由简单语法(calculator.g4)生成的解析器,因此我觉得我在Visual Studio中设置得很好。我现在正在尝试为Java生成一个解析器。g4语法,我从github.com/antlr/grammars-v4/java获得。生成的javalext .cs文件不会编译(见下面的代码和错误)——可能是因为它包含了只存在于java环境中的东西的引用。
如有任何建议,不胜感激。
罗伯特private bool JavaLetterOrDigit_sempred(RuleContext _localctx, int predIndex) {
switch (predIndex) {
case 2: return Character.isJavaIdentifierPart(_input.LA(-1));
case 3: return Character.isJavaIdentifierPart(Character.toCodePoint((char)_input.LA(-2),
(char)_input.LA(-1)));
}
return true;
}
错误:名称'Character'在当前上下文中不存在
错误:"Antlr4.Runtime。ICharStream'不包含'LA'的定义,也没有扩展方法'LA'接受类型为'Antlr4.Runtime '的第一个参数。
该语法包含Java代码。它只在以下规则中使用:
fragment
JavaLetter
: [a-zA-Z$_] // these are the "java letters" below 0xFF
| // covers all characters above 0xFF which are not a surrogate
~[u0000-u00FFuD800-uDBFF]
{Character.isJavaIdentifierStart(_input.LA(-1))}?
| // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
[uD800-uDBFF] [uDC00-uDFFF]
{Character.isJavaIdentifierStart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))}?
;
fragment
JavaLetterOrDigit
: [a-zA-Z0-9$_] // these are the "java letters or digits" below 0xFF
| // covers all characters above 0xFF which are not a surrogate
~[u0000-u00FFuD800-uDBFF]
{Character.isJavaIdentifierPart(_input.LA(-1))}?
| // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
[uD800-uDBFF] [uDC00-uDFFF]
{Character.isJavaIdentifierPart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))}?
;
移除{...}
部分:
fragment
JavaLetter
: [a-zA-Z$_] // these are the "java letters" below 0xFF
| // covers all characters above 0xFF which are not a surrogate
~[u0000-u00FFuD800-uDBFF]
| // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
[uD800-uDBFF] [uDC00-uDFFF]
;
fragment
JavaLetterOrDigit
: [a-zA-Z0-9$_] // these are the "java letters or digits" below 0xFF
| // covers all characters above 0xFF which are not a surrogate
~[u0000-u00FFuD800-uDBFF]
| // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
[uD800-uDBFF] [uDC00-uDFFF]
;
和(可选地)在后面的阶段执行这些检查,或者用c#代码替换Java代码。
这是一个添加的静态类Character
将使使用的Java
代码也有效的C#
代码。
public static class Character
{
public static bool isJavaIdentifierPart(int c)
{
// some code here
}
public static int LA(this Antlr4.Runtime.ICharStream cs, int la)
{
return cs.La(la);
}
public static int toCodePoint(char c)
{
// some code here
}
}