[antlr-interest] Run-time construction of syntactic predicates

Jan Ploski jpljpl at gmx.de
Thu Oct 27 12:26:52 PDT 2005


Hello,

Is it possible to construct a syntactic predicate in ANTLR for
which the lookahead string is computed at run time from a token
previously read by the lexer?

Context: I need to extract strings from a Perl source file.
Such strings begin with a quote character after which some text
enclosed by a pair of arbitrary non-alphanumeric, non-whitespace
delimiters follows. For example

q/some string/
q!some string!
q#some string#

and even

q{some string}

are all legal strings.

I want to avoid putting rules for all possible pairs of delimiters
into my grammar.

My idea was to save the delimiter in an action after reading the
it first and use the saved value while looking for the second
delimiter. I can implement this easily by modifying the generated
lexer, but I am at loss when it comes to expressing this in the
grammar file (obviously, modifying generated code would be a last
resort).

I guess it would be something for the parser phase where I could
use a semantic predicate. However, moving the logic to parser would
be difficult because I also rely on the filter=IGNORE option in the
lexer (is there anything like that available at the parser level?)

I have already tried a semantic predicate in a lexer rule,
but this results in an exception being thrown rather than
the desired disambiguating effect.

Best regards -
Jan Ploski



More information about the antlr-interest mailing list