[antlr-interest] antlr 3 woe

John B. Brodie jbb at acm.org
Mon Dec 4 15:05:08 PST 2006


>I think i've stumbled upon the reason for the problems:
>
>how can i match something like :
>
>%META:TOPICINFO{}%
>
>
>this text should be matched by:
>
>
>meta	:      OTOPICINFO CTOPICINFO;
>
>fragment OTOPICINFO
>	:	'%META:TOPICINFO{'
>	;
>fragment CTOPICINFO:'}%
>
>but it isn't. maybe because of the string templates? how can i escap
>the }% part eg?

the `fragment` keyword establishes a Token which is a fragment, e.g. a
portion of, another Token.

fragment Tokens may not be referenced by parser rules, but ANTLRv3
currently does not check for that mis-use (there is a bug report about
this).

you need to re-work your lexer rules so that Tokens that the parser needs
to see are not created by fragment rules.

as for using % in rules. I believe that % inside an antlr string,
e.g. '...%...'  works just fine without any special escape. But a %
anywhere inside an action code fragment, e.g. {...%...} should be escaped
as \%.  So { System.out.printf("%s%n",x); } should be written as
{ System.out.printf("\%s\%n",x); } // java 1.5.

Hope this helps...
   -jbb


More information about the antlr-interest mailing list