[antlr-interest] literals in 2.7.6
Omry Yadan
omry at yadan.net
Sun Feb 11 00:15:25 PST 2007
It worked, thanks.
here is a complete example, for future newbies:
-----------------------------Grammar
----------------------------------------------
class FooLexer extends Lexer;
options
{
testLiterals=false;
}
// skip whitespaces
WS : ( ' ' | '\t' ) { $setType(Token.SKIP); };
protected CHAR : ('a'..'z' | 'A'..'Z');
protected DIGIT : ('0'..'9');
// a classic definition of an identifier.
IDENTIFIER options{testLiterals=true;} : CHAR (CHAR|DIGIT)*;
class FooParser extends Parser;
options
{
// build AST to allow post parse analysis
buildAST=true;
}
// keywords be here
keyword : "keyword1" | "keyword2" | "keyword3";
// just a line with keywords and identifiers
line : (keyword | IDENTIFIER)*;
------------------------------------------------------------------------
------------------------Tester -----------------------------------------
public class TestLexer
{
public static void main(String[] args) throws TokenStreamException,
RecognitionException
{
String str = "str1 keyword1 str2 keyword2";
FooLexer lex = new FooLexer(new StringReader(str));
FooParser parse = new FooParser(lex);
parse.line();
CommonAST ast = (CommonAST)parse.getAST();
DumpASTVisitor visitor = new DumpASTVisitor();
visitor.visit(ast);
}
}
------------------------------------------------------------------------
Xue Yong Zhi wrote:
> You do not have to list RESERVED as a rule, just use it as a string in
> the parser.
More information about the antlr-interest
mailing list