March 01, 1999
Compiler Construction with ANTLR and Java
Compiler Construction with ANTLR and JavaBy Gary L. SchapsDr. Dobb's Journal March 1999
// import directive
importDirective
// init-action
{File f=null; String str=null;}
// rule
: "import" str=fileName
// semantic actions
{
f = new File(str); KPLLexer lexer = null;
lexer = new KPLLexer(new FileInputStream(f));
parser = new KPLParser(lexer);
// this file's either another pattern file ...
if (str.substring(str.length()-4).equals(".kpl")) {
parser.compilationUnit();
}else{
// ... or else it's a test program program ...
parser.testProgram();
}
}
;
Example 3: ANTLR rule for discriminating import directive.
Copyright © 1999, Dr. Dobb's Journal
|
|
||||||||||||||||||||||||||||
|
|