C#、ANTLR、ECMAScript 语法问题
我正在尝试用 C# 解析 JavaScript (ECMASCript)。
我发现以下有关如何创建新项目的说明: http://www.antlr.org/wiki/pages/viewpage.action ?pageId=557075
所以我下载了 ANTLRWorks、ANTLR v3、解压 ANTLR、创建了一个 VS2010 项目 (.NET4)、添加了引用、检查并生成语法。
然后我收到了很多编译错误:
找不到类型或命名空间名称“AstParserRuleReturnScope”(是否缺少 using 指令或程序集引用?)
找不到类型或命名空间名称“GrammarRule”(是否缺少 using 指令或程序集引用?)
Stackoverlowed 为他们提供了解决方案:antlr c# 集成到 VS2008 时出现错误
所以我下载了新的运行时,覆盖旧的并重新编译项目,得到
当前上下文 d:\Workspace.1\ScriptParser\ScriptParser\TestLexer.cs 中不存在名称“HIDDEN”
好的,我已按照以下对话中的建议将 HIDDEN 更改为 Hidden: [antlr-interest] 如何可行是Csharp3目标吗? (更具体的问题)
现在我正在尝试解析输入。我找到了一些例子并编写了以下代码:
using Antlr.Runtime;
namespace ScriptParser
{
class Program
{
static void Main(string[] args)
{
var stream = new ANTLRStringStream("1+2");
var lexer = new TestLexer(stream);
var tokenStream = new CommonTokenStream(lexer);
var parser = new TestParser(tokenStream);
// what exactly should be here???
}
}
}
我的目标是使用 ANTLR 解析 JavaScript 文件,但似乎这并不像我想象的那么容易......
更新:
正如 < a href="https://stackoverflow.com/questions/6411520/why-are-antlr3-c-sharp-parser-methods-private">为什么antlr3 c#解析器方法是私有的?我已经修改了Test.g 语法通过在 expr 规则之前添加修改的“public”:
public expr : mexpr (PLUS^ mexpr)* SEMI!
;
然后重新生成代码,将 HIDDEN 替换为 Hidden(再次)并将代码修改如下:
var stream = new ANTLRStringStream("1+2");
var lexer = new TestLexer(stream);
var tokenStream = new CommonTokenStream(lexer);
var parser = new TestParser(tokenStream);
var result = parser.expr();
var tree = (CommonTree)result.Tree;
崩溃了
root_0 = (object)adaptor.Nil();
而不是在以下生成的代码中
try { DebugEnterRule(GrammarFileName, "expr");
DebugLocation(7, 0);
try
{
// d:\\Workspace.1\\ScriptParser\\ScriptParser\\Test.g:7:13: ( mexpr ( PLUS ^ mexpr )* SEMI !)
DebugEnterAlt(1);
// d:\\Workspace.1\\ScriptParser\\ScriptParser\\Test.g:7:15: mexpr ( PLUS ^ mexpr )* SEMI !
{
root_0 = (object)adaptor.Nil();
DebugLocation(7, 15);
PushFollow(Follow._mexpr_in_expr31);
带有 NullReferenceException 消息,因为适配器为 null。
我通过添加
parser.TreeAdaptor = new CommonTreeAdaptor();
更新 2:
解决了这个问题。所以,最后我开始了我的主要任务:解析 JavaScript。
ANTLR 重点介绍 Chris Lambrou 的 ECMAScript 语法。
因此,我生成了词法分析器/解析器,并使用非常简单的 JavaScript 代码运行它:
var f = function () { };
解析失败,并显示来自 tree.ToStringTree() 的以下输出:
<error: var q = function () { };>
I'm trying to parse JavaScript (ECMASCript) with C#.
I found the following instruction on how to create new project:
http://www.antlr.org/wiki/pages/viewpage.action?pageId=557075
So I've downloaded ANTLRWorks, ANTLR v3, unpacked ANTLR, created a VS2010 project (.NET4), added references, checked and generated the grammar.
Then I recieved a lot of compilation error:
The type or namespace name 'AstParserRuleReturnScope' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'GrammarRule' could not be found (are you missing a using directive or an assembly reference?)
Stackoverlowed for them and got a solution: antlr c# errors when integrating into VS2008
So I've downloaded new runtime, overwrite the old one and recompiled the project and got
The name 'HIDDEN' does not exist in the current context d:\Workspace.1\ScriptParser\ScriptParser\TestLexer.cs
Ok, I've changed HIDDEN to Hidden as recommended at in the following conversation: [antlr-interest] How viable is the Csharp3 target? (more specific questions)
Now I'm trying to parse the input. I found a few examples and wrote the following code:
using Antlr.Runtime;
namespace ScriptParser
{
class Program
{
static void Main(string[] args)
{
var stream = new ANTLRStringStream("1+2");
var lexer = new TestLexer(stream);
var tokenStream = new CommonTokenStream(lexer);
var parser = new TestParser(tokenStream);
// what exactly should be here???
}
}
}
My goal is to parser JavaScript file with ANTLR but it seems that it will be the not as easy as I thought...
Update:
As suggested in Why are antlr3 c# parser methods private? I've modified the Test.g grammar by adding the "public" modified before the expr rule:
public expr : mexpr (PLUS^ mexpr)* SEMI!
;
and then regenerated the code, replaced HIDDEN to Hidden (again) and modified the code as follows:
var stream = new ANTLRStringStream("1+2");
var lexer = new TestLexer(stream);
var tokenStream = new CommonTokenStream(lexer);
var parser = new TestParser(tokenStream);
var result = parser.expr();
var tree = (CommonTree)result.Tree;
And not it is crashing on the line
root_0 = (object)adaptor.Nil();
in the following generated code
try { DebugEnterRule(GrammarFileName, "expr");
DebugLocation(7, 0);
try
{
// d:\\Workspace.1\\ScriptParser\\ScriptParser\\Test.g:7:13: ( mexpr ( PLUS ^ mexpr )* SEMI !)
DebugEnterAlt(1);
// d:\\Workspace.1\\ScriptParser\\ScriptParser\\Test.g:7:15: mexpr ( PLUS ^ mexpr )* SEMI !
{
root_0 = (object)adaptor.Nil();
DebugLocation(7, 15);
PushFollow(Follow._mexpr_in_expr31);
with the NullReferenceException message because the adapter is null.
I've resolved it by adding
parser.TreeAdaptor = new CommonTreeAdaptor();
Update 2:
So, finally I've started with my primary task: parse JavaScript.
ANTLR highlights the ECMAScript grammar by Chris Lambrou.
So I've generated lexer/parser and run it with the very simple JavaScript code:
var f = function () { };
and the parsing fails with the following output from tree.ToStringTree():
<error: var q = function () { };>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的语法规则规定表达式末尾应该有一个分号,但在您的 main 函数中:
缺少分号。不应该是“1+2;”吗?
Your grammar rule says that there should be a semicolon at the end of the expression, but in you main function:
is missing a semicolon. Shouldn't it be "1+2;"?