[antlr-interest] Scanning Perl-style quoted strings q{foo{bar}quux}?! [correction]

David-Sarah Hopwood david-sarah at jacaranda.org
Wed Jul 29 16:05:10 PDT 2009


David-Sarah Hopwood wrote:
> Ralf S. Engelschall wrote:
>> To a small ANTLR-based expression language I would like to add
>> Perl-style quoted strings:
[...]
>> Remains the question: what is the best way to implement this in ANTLR 3?
> 
> Remember that lexer rules can be recursive, so you don't have to explicitly
> keep track of nesting depth. The following approach (untested) is more
> declarative, and incidentally avoids the problem you encountered:
> 
> QSTRING
>   : 'q' ( AngleQS | BraceQS | BrackQS | ParenQS | SlashQS | BangQS ) ;
> 
> fragment AngleQS
>   : '<' ( AngleQS | ~('<' | '>') )* '>' ;
> 
> fragment BraceQS
>   : '{' ( BraceQS | ~('{' | '}') )* '}' ;
> 
> fragment BrackQS
>   : '[' ( BrackQS | ~('[' | ']') )* ']' ;
> 
> fragment ParenQS
>   : '[' ( ParenQS | ~('[' | ']') )* ']' ;

This should be
    : '(' ( ParenQS | ~('(' | ')') )* ')' ;
of course.

> fragment SlashQS
>   : '/' ( SlashQS | ~'/' )* '/' ;
> 
> fragment BangQS
>   : '!' ( BangQS | ~'!' )* '!' ;

-- 
David-Sarah Hopwood  ⚥  http://davidsarah.livejournal.com



More information about the antlr-interest mailing list