1
從書籍「最終ANTLR 4參考」開始工作,我試圖在Eclipse中運行ArrayInit.g4示例。我已經設法生成必要的java文件和其他文件,但是當我運行該示例並將值輸入到控制檯並按Enter時,沒有任何反應(第29和30頁)。ANTLR4項目將不會在Eclipse中顯示任何內容
ArrayInit.g4
/** Grammars always start with a grammar header. This grammar is called
* ArrayInit and must match the filename: ArrayInit.g4
*/
grammar ArrayInit;
@header
{
package com.foo.bar;
}
/** A rule called init that matches comma-separated values between {...}. */
init : '{' value (',' value)* '}' ; // must match at least one value
/** A value can be either a nested array/struct or a simple integer (INT) */
value : init
| INT
;
// parser rules start with lowercase letters, lexer rules with uppercase
INT : [0-9]+ ; // Define token INT as one or more digits
WS : [ \t\r\n]+ -> skip ;
Main.java
package com.foo.bar;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
public class Main
{
public static void main(String[] args) throws Exception
{
// create a CharStream that reads from standard input
ANTLRInputStream input = new ANTLRInputStream(System.in);
// create a lexer that feeds off of input CharStream
ArrayInitLexer lexer = new ArrayInitLexer(input);
// create a buffer of tokens pulled from the lexer
CommonTokenStream tokens = new CommonTokenStream(lexer);
// create a parser that feeds off the tokens buffer
ArrayInitParser parser = new ArrayInitParser(tokens);
ParseTree tree = parser.init(); // begin parsing at init rule
// Create a generic parse tree walker that can trigger callbacks
ParseTreeWalker walker = new ParseTreeWalker();
// Walk the tree created during the parse, trigger callbacks
walker.walk(new ShortToUnicodeString(), tree);
System.out.println(); // print a \n after translation
}
}
訂立控制檯: {99,3,451}
預期輸出: 「\ u0063 \ U0003 \ u01c3」