[antlr-interest] A question about dynamic grammar

Alec Lanter kintar1900 at gmail.com
Thu Jul 16 05:57:52 PDT 2009


Hello, everyone.
I've only recently started using ANTLR, and I love the way it takes a lot of
the drudge work out of simple recognizers.  I'm having a little trouble with
more complex tasks, though, and I was hoping someone could offer advice.

I have an old project that did command parsing for an interactive fiction
game.  The parser is written by hand, and deals with rules that I'm having a
very hard time translating to ANTLR.  One such example:  In a room, there
are three monkeys, all in a cage.  One of the monkeys is "monkey
from Madagascar".  The syntax for the "take" command is "take <noun> from
<noun>".  The hand-coded parser handles the ambiguity of the command "take
monkey from Madagascar from cage" by identifying "from" as a literal
separator in the verb rule and considering all possible ways the input could
be split around it.  So, it ends up checking the options:

   - "take"(verb) "monkey"(noun) "from"(literal) "Madagascar from
   cage"(noun)
   - "take"(verb) "monkey from Madagascar"(noun) "from"(literal)
   "cage"(noun)

The parser finds that only one of those constructions results in two valid
nouns, so it uses that interpretation when it invokes the verb.

I've been using the Pragmatic Programmers Definitive ANTLR Reference to try
and help myself along, and I've had some success with semantic predicates,
but I'm completely failing to understand how I can get ANTLR to perform the
lookahead necessary to recognize that there are two ways the input can be
parsed.  I've got grammar rules set up that successfully parse sentences
like "take monkey from cage" and "go north" by using lookup tables of verbs
and nouns in the predicates, but that falls apart when a noun has whitespace
in the name, causing it to be broken into multiple tokens.

Any advice?  I'm pretty sure the solution is sitting there in black and
white in the book, but the discussion on LL(*) and semantic predicates is a
little too abstract for me to handle this early in my experience with ANTLR.

Thanks,

-- Alec Lanter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090716/46133546/attachment.html 


More information about the antlr-interest mailing list