[antlr-interest] a strange problem

Su Zhang westlifezs at gmail.com
Sun Nov 23 15:25:26 PST 2008


thanks a lot, yet there still have a minor problem in my project, when I am
trying to get a return value from the program, I called in the main
function,but  I cannot find get the return value, does anybody see problem
in my grammar?

here it is:
num    returns [String t]
        :n = Num  {$t =
$n.getText();}|b64=Base64Block{$t=$b64.getText();}|ident=Id{$t=$ident.getText();}
        ;
Num   :     ( '0'..'9' ) * ;
Id   :     ( 'a'..'z' | 'A'..'Z' | '_' ) ( 'a'..'z' | 'A'..'Z' | '_' |'-'|
'0'..'9' ) *;
Base64Block  :('a'..'z'| 'A'..'Z'|'0'..'9'|'/'|'+'|'=')*;
the input is perfectly matched by the antlr and the program and can be
printed out correctly, but when i called like this  :
"System.out.println(parser.num());" , there is not any return value(there
should be some value in the num cause i put it in the input file), does
anybody know where my problem is?
thank you!

Su
2008/11/23 Kevin J. Cummings <cummings at kjchome.homeip.net>

> Su Zhang wrote:
>
>> Hi all,
>> I encountered a very strange problem. I put a string 'read' in the grammar
>> code, and when matching, I would like the input to match the toke ID which
>> is defined as ('a'..'z'). Obviously, the IDshould match read, but why it
>> cannot match, just becasue I have a hardcoded string 'read' in the grammar?
>> does everyone know how can i get ride of this trouble?
>>
>
> Keywords vs reserved words.  Literals in the parser are reserved words.  If
> you wish for them to match identifiers, you must create a parser rule for
> identifier which includes both IDENTIFIER and the literals.
>
> Thank you!
>> Su
>>
>
> --
> Kevin J. Cummings
> kjchome at rcn.com
> cummings at kjchome.homeip.net
> cummings at kjc386.framingham.ma.us
> Registered Linux User #1232 (http://counter.li.org)
>



-- 
Su Zhang
PHD Candidate
Computer Information and Science
Kansas State University
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20081123/52054266/attachment.html 


More information about the antlr-interest mailing list