Related articles |
---|
ANTLR literals/common prefixes question blipf@yahoo.com (blipf) (2002-05-03) |
From: | blipf <blipf@yahoo.com> |
Newsgroups: | comp.compilers,comp.compilers.tools.pccts |
Date: | 3 May 2002 16:04:59 -0400 |
Organization: | Compilers Central |
Keywords: | PCCTS, question |
Posted-Date: | 03 May 2002 16:04:59 EDT |
Hi,
I'm trying to learn ANTLR. While trying to work out a problem I am
having I thought I'd start with a trivial example:
class P extends Parser;
abc : "ABC" {System.out.println("yes");} ;
class L extends Lexer;
WS: ( ' ' | '\t' | '\r' '\n' | '\n' ) {$setType(Token.SKIP);} ;
Which I run on this data:
ABC
I get no errors during compilation, but when I run the parser with
-trace I get:
> abc; Exception in thread "main"
antlr.TokenStreamRecognitionException: unexpected char: A
What do you suppose the problem could be? What is the difference
between literals in the parser and literals in the lexer with a tokens
block in the parser?
The next more complicated thing I was trying was:
class P extends Parser;
abc : def ghi "END" {System.out.println("END");} ;
def : "ABC" LSB "xyz" COLON a:NAME RSB {System.out.println(a.getText());} ;
ghi : "ABCDEF" LSB "Abc_Def" COLON a:NAME RSB {System.out.println(a.getText());} ;
class L extends Lexer;
LSB : '[' ;
RSB : ']' ;
COLON : ':' ;
DIGIT : '0' .. '9' ;
NAME : (LCLETTER|UCLETTER) (LCLETTER|UCLETTER|DIGIT)* ;
WS: ( ' ' | '\t' | '\r' '\n' | '\n' ) {$setType(Token.SKIP);} ;
protected
LCLETTER : 'a' .. 'z' ;
protected
UCLETTER : 'A' .. 'Z' ;
with data:
ABC [ xyz fdsa ]
ABCDEF [ Abc_Def ]
It functions as I would expect until it matches Abc_ and then complains:
line 1: expecting "Abc_Def", found 'Abc'
< ghi; < abc; Exception in thread "main"
antlr.TokenStreamRecognitionExcepti on: unexpected char: _
Why was it okay with "ABC" vs. "ABCDEF" but then get's confused by Abc_Def?
Return to the
comp.compilers page.
Search the
comp.compilers archives again.