简体   繁体   English

Eclipse中的Antlr IDE无法正常工作

[英]Antlr IDE in Eclipse doesn't work

I'm using Antlr v3 and java for a project and all goes well in AntlrWorks, but when I switch to Antlr IDE, the plugin for eclipse, the interpreter cannot match the sentence that I wrote, telling me mismatch. 我在项目中使用Antlr v3和Java,并且在AntlrWorks中一切正常,但是当我切换到eclipse的插件Antlr IDE时,解释器无法匹配我编写的句子,告诉我不匹配。 And it seems to me that it cannot recognize the list of words to skip that I've specified in on lexer. 在我看来,它无法识别我在词法分析器中指定的要跳过的单词列表。 However it is the same that works perfectly in AntlrWorks. 但是,它在AntlrWorks中可以完美地工作。

Any suggestions? 有什么建议么?

I've set up the antlr buider in eclipse: Antlr Parser Generator v3.4 我已经在eclipse中设置了antlr buider:Antlr Parser Generator v3.4

This may be a configuration issue. 这可能是配置问题。 I had several difficulties getting everything to work so, not finding any one place for how to do this, I wrote my own document. 我在使所有内容正常工作方面遇到了一些困难,因此我没有找到任何一个执行该操作的地方,而是编写了自己的文档。 Hope this helps. 希望这可以帮助。

How To Install ANTLR IDE in Eclipse Indigo 如何在Eclipse Indigo中安装ANTLR IDE

Tested With Indigo R2 and IDE 2.1.2 经过Indigo R2和IDE 2.1.2的测试

  1. Install Eclipse. 安装Eclipse。
  2. Download ANTLR complete binaries jar that includes ANTLR v2. 下载包含ANTLR v2的ANTLR完整二进制文件jar。 Extract to a temp directory. 提取到临时目录。 Copy the antlr-nn folder to an appropriate permanent location, for example the same folder that Eclipse is installed in. 将antlr-nn文件夹复制到适当的永久位置,例如Eclipse安装所在的文件夹。
  3. Add ANTLR IDE update site to Eclipse. 将ANTLR IDE更新站点添加到Eclipse。
    • In Eclipse, click on Help and select Install New Software. 在Eclipse中,单击“帮助”,然后选择“安装新软件”。
    • Click Add… button. 单击添加...按钮。
    • In the Add Repository window, for Location type http://antlrv3ide.sourceforge.net/updates and type something like ANTLR IDE for the Name and click OK to get back to the Available Software window. 在“添加存储库”窗口中,为“位置”键入http://antlrv3ide.sourceforge.net/updates,然后为“名称”键入类似ANTLR IDE的名称,然后单击“确定”返回到“可用软件”窗口。
    • Check the box for ANTLR IDE vn.nn and click on through until it is installed. 选中ANTLR IDE vn.nn的复选框,然后单击直至安装。 Eclipse will probably restart. Eclipse可能会重新启动。
  4. Configure the ANTLR IDE. 配置ANTLR IDE。
    • In the Eclipse main window, click Window then Preferences. 在Eclipse主窗口中,单击窗口,然后单击首选项。
    • In the left pane, expand ANTLR and select Builder. 在左窗格中,展开ANTLR并选择Builder。
    • In the right pane, click the Add… button. 在右窗格中,单击“添加...”按钮。
    • In the Add ANTLR Package window, click Directory… and navigate to the location of the antlr-nn folder and click OK. 在“添加ANTLR程序包”窗口中,单击“目录...”,然后导航到antlr-nn文件夹的位置,然后单击“确定”。
    • Click OK to close the Add ANTLR Package window. 单击“确定”关闭“添加ANTLR程序包”窗口。
    • Select Code Generator in the left pane and click on Project relative folder in the right pane. 在左窗格中选择Code Generator,然后在右窗格中单击Project relative文件夹。 Type a folder name. 输入文件夹名称。 Examples: antlr-java or antlr-generated. 示例:antlr-java或antlr生成。
    • Select any other configuration parameters but DO NOT check –nfa or –dfa in the under General in the Building window. 选择任何其他配置参数,但不要在“建筑物”窗口的“常规”下检查–nfa或–dfa。 If checked, these will cause ANTLR errors preventing java files from being generated in the output folder. 如果选中,将导致ANTLR错误,从而阻止在输出文件夹中生成Java文件。
    • Click OK to close the Preferences window. 单击“确定”关闭“首选项”窗口。
  5. Create a new Java project and enable ANTLR support. 创建一个新的Java项目并启用ANTLR支持。
    • From the Eclipse main window, go to File, New, Java Project. 在Eclipse主窗口中,转到File,New,Java Project。 Click Next, type a project name and click Finish. 单击下一步,键入一个项目名称,然后单击完成。
    • To enable ANTLR support for the project, in the Package Explorer window (left pane) right-click the project just created and select Configure, Convert to ANTLR project. 要为项目启用ANTLR支持,请在Package Explorer窗口(左窗格)中右键单击刚刚创建的项目,然后依次选择Configure,Convert to ANTLR project。
    • Add the ANTLR complete jar file to the project: right-click the project and select Properties, Java Build Path, click Add External JARs…, browse to the ANTLR jar file, select it, and click OK. 将ANTLR完整的jar文件添加到项目中:右键单击项目,然后选择“属性”,“ Java构建路径”,单击“添加外部JAR…”,浏览到ANTLR jar文件,选择它,然后单击“确定”。 Click OK to close the project Properties window. 单击“确定”关闭项目“属性”窗口。
  6. Create an ANTLR grammar. 创建一个ANTLR语法。
    • Create a new ANTLR grammar: right-click the src folder of the project, then File, New, Other, expand ANTLR and select Combined Grammar. 创建一个新的ANTLR语法:右键单击项目的src文件夹,然后依次单击“文件”,“新建”,“其他”,展开ANTLR并选择“组合语法”。 Click Next, type grammar name, select a Language option, and click Finish. 单击“下一步”,键入语法名称,选择“语言”选项,然后单击“完成”。
    • A “.g” file is created with the options selected and a blank rule. 使用选定的选项和空白规则创建一个“ .g”文件。 Add the options language=Java, @header, @lexer::header, and @members statements at the top (see example). 在顶部添加选项language = Java,@ header,@ lexer :: header和@members语句(请参见示例)。 Auto completion is the easiest way to add these (press CTRL-space to bring up auto-completion list). 自动完成是添加这些内容的最简单方法(按CTRL-空格以显示自动完成列表)。
  7. Save the grammar. 保存语法。
    • When saved, a folder containing generated Java code for the grammar should appear in the Project Explorer. 保存后,包含已生成语法的Java代码的文件夹应出现在Project Explorer中。 If it does not, make sure the –nfa or –dfa options are not checked in ANTLR Preferences under General in the Building window (Step 4g). 如果没有,请确保在“建筑物”窗口的“常规”下的“ ANTLR首选项”中未选中–nfa或–dfa选项(步骤4g)。 [Confirm if these needed: check CLASSPATH environment variable points to the Java7 that matches your Eclipse install (32 or 64 bits) and Windows Path environment variable had Java7 SDK.] [确认是否需要这些:检查CLASSPATH环境变量是否指向与您的Eclipse安装(32或64位)匹配的Java7,并且Windows Path环境变量具有Java7 SDK。
    • To avoid “cannot be resolved to a type” Java errors, right-click the folder containing generated Java code, then Build Path, Use as a Source Folder. 为避免“无法解析为类型” Java错误,请右键单击包含生成的Java代码的文件夹,然后右键单击“构建路径”,“用作源文件夹”。

SAMPLE COMBINED GRAMMAR 样本组合语法

grammar test; //must match filename.g

options {
    language = Java;
}

@header { //parser
    package pkgName; //optional
    import java.<whatever you need>.*;
}

@members { //parser
    // java code here
}

@lexer::header { //lexer
    package pkgName; //optional
    import java.<whatever you need>.*;
}

@lexer::members {
    // java code here
}
/*------------------------------------------------------------------
 * PARSER RULES (convention is all lowercase)
 *------------------------------------------------------------------*/
parserule: LEXRULE;

/*------------------------------------------------------------------
 * LEXER RULES (convention is all uppercase)
 *------------------------------------------------------------------*/
LEXRULE: 'a'..'z';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM