私はC#でJavaScript(ECMASCript)を解析しようとしています。C#、ANTLR、ECMAScript文法の問題
私は新しいプロジェクトを作成する方法について、次の命令を見つけた:、だから私はANTLRWorks、ANTLR v3では、アンパックANTLRをダウンロードした http://www.antlr.org/wiki/pages/viewpage.action?pageId=557075
VS2010プロジェクト(.NET4)を作成し、参照を追加チェックして生成されました文法。彼らのために
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と解決策だ:
は、それから私は、コンパイルエラーの多くを受け取ったので、私は、新しいランタイムをダウンロードし、古いものを上書きして、プロジェクトを再コンパイルしましantlr c# errors when integrating into VS2008
をし、隠しにで推奨されているように
The name 'HIDDEN' does not exist in the current context d:\Workspace.1\ScriptParser\ScriptParser\TestLexer.cs
[OK]を
を持って、私は次の会話の中でHIDDEN変更しました: [antlr-interest] How viable is the Csharp3 target? (more specific questions)私は入力を解析しようとしています。私はいくつかの例を発見し、次のコードを書いた:
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ファイルが、私が思ったほど簡単ではないだろうと思われ...
更新です:
public expr : mexpr (PLUS^ mexpr)* SEMI!
;
:Why are antlr3 c# parser methods private?で示唆したように
私はexprのルールの前に修正された「公共」を追加することにより、Test.g文法を修正しました
と、コードを再生し、(再び)隠しに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 highlights Chris LambrouによるECMAScript文法。
だから私は、字句解析/パーサを生成し、非常に簡単なJavaScriptコードでそれを実行しました:
var f = function() { };
と解析がtree.ToStringTree(から次の出力で失敗):
<error: var q = function() { };>
この前のQ&Aをチェックアウト:http://stackoverflow.com/questions/4396080/antlr-3-3-c-sharp-tutorials –
優れたチュートリアルをありがとう。私が理解するように、文法のルールはメソッドに変換する必要があります。私はhttp://www.antlr.org/wiki/pages/viewpage.action?pageId=557075の文法を使い、ANTLRWorksは "expr"、 "atom"などのメソッドを生成しますが、それらはprivateです。 –
ありがとうアレックス。 W.r.t.プライベートメソッド(パーサーのルール)、別のQ&Aを参照してください:http://stackoverflow.com/questions/6411520/why-are-antlr3-c-sharp-parser-methods-private –