[antlr-interest] newbie question: How to parse perl-strings like syntax?

Yogesh Pandey wypee at hotmail.com
Wed Oct 31 04:10:00 PDT 2007


Hi,

How would one write lexer rules for parsing perl like strings which can
have embedded expressions? For example:


// input syntax
TEMPLATE  tagA, tagB;

"This template simply prints
 today's date as $date"

ENDTEMPLATE


The following is what I tried but the problem is that OTHER_TEXT token is a 
super-set
of IDENT and always gets matched even for the tags (unlike lex, the rule 
IDENT
doesn't get precedence because its defined first in the file). This is not 
what I
want. How can I ask antlr to not match OTHER_TEXT token till I am inside
the template_body?

=== antlr.g

template:  'TEMPLATE' taglist template_body 'ENDTEMPLATE'
        ;

taglist: IDENT (',' IDENT)* ';'
       ;

template_body:
       '"' (EXPR | OTHER_TEXT)* '"'
       ;

LETTER:
        'a..z' | 'A..Z'
       ;

IDENT:
        LETTER+
        ;

EXPR:
        '$' IDENT
        ;

WS:
         (' '|'\r'|'\n'|'\t') { $channel = HIDDEN; }

OTHER_TEXT:
        (~('$'))*
        ; 



More information about the antlr-interest mailing list