[antlr-interest] dynamic addition to a lexer rule.

Karel Bernolet masanasa at gmail.com
Wed Sep 26 13:19:06 PDT 2007


Hi Paul,

as far as I know anything about antlr (only started using it a week ago),
you might use semantic predicates to achieve what you want to do.

you should have to change your rule in something like this:

LETTER: 'A'..'Z'
NOUN {String noun = new String();}: (x:LETTER {noun += x.getText();})+
{isValidNoun(noun);}?;

in the 'public boolean isValidNoun(String noun)' method you can query a db,
check in a file or anything else you  want.

it's the only way I can think of besides changing your grammar file on the
fly, regenerating  your parser and updating your classloader with the new
class file on the fly  ...

(note: if you don't use java as target language, change the code blocks in
the example to your target language).

I hope this helps.

On 9/26/07, Paul Eames <peames at nexagent.com> wrote:
>
>  Hi,
>
> I'm evaluating whether I can use ANTLR to parse sentences in structured
> English.
>
> I have a number of rules around the valid combination of noun concepts,
> verb parts, and rule keywords in my example grammar which I think I know how
> to implement. My problem is that I cannot define the full grammar until
> runtime as users of our system are able to define concepts of interest to
> them. Is there a way to add to the alternatives of a lexer rule at runtime?
>
> As an example, imagine I had defined the following lexer rule in my
> grammar:
>
> NOUN : ( 'cat' | 'dog') ;
>
> A user of our application may wish to define 'rodrigues fruit bat' as a
> noun and when they do so I'd like to add that to the list of alternatives
> for the lexer to match.
>
> Many thanks,
> Paul.
>



-- 
dont blame my OS for your laziness
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070926/1507fbf6/attachment.html 


More information about the antlr-interest mailing list