简体   繁体   中英

How to run g4 file by ANTLR4 plugin in Eclipse

I installed ANTLRv4 plugin for my Eclipse and I created a file Hello.g4 :

/**
 * Define a grammar called Hello
 */
grammar Hello;

r  : 'hello' ID ;         // match keyword hello followed by an identifier

ID : [a-z]+ ;             // match lower-case identifiers

WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines

How can I run this g4 file in Eclipse and how to view a parse tree ?

The use of the ANTLR plugin works in two phases: first, you compile the .g4 file in order to produce the .java code for the lexer/parser/visitor/listeners..., then if you open the Parse Tree window.

To do that, your project must have the xtext nature, if the nature is not enabled: right click on your project, then configure->Add Xtext nature . Once the nature is enabled, in the project properties, you should see a ANTLR4 entry. You can configure special options for your project here.

Each time you will save modifications on your .g4 (if you have opened it with the ANTLR Editor given with the plugin), this will trigger a recompilation of your files.

To open the Parse Tree window, select Window->Show View->Other...->ANTLR4->Parse Tree . This will open the window. Now, you have to keep in mind that the ANTLR Editor and the Parse Tree window communicate. In your ANTLR Editor, if you put your cursor on a rule, you will see that the Parse Tree view updates a part of its screen and shows the rule you have selected. You can type your expression and see the parse tree.

Here is what I obtain using your grammar: ANTLR4 Eclipse插件中的解析树和语法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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