[MUD-Dev2] Command Parsing. Lame question!

Zach Collins Siege siegemail at gmail.com
Wed Jun 24 06:22:50 CEST 2009


On Wed, Jun 3, 2009 at 10:31 AM, Tiago<tiago.matias at gmail.com> wrote:

> But if I input "look left" the grammar won't work since the lexer is giving
> me Command Command and the second token cannot be shifted. Now, as I see it,
> my grammar is not context free and the tokens have different meaning
> according to previous ones.
>
> My question is, what is the best approach to solve this type of problem ? My
> first approach was trying to recognize literal strings in the grammar. Like:
>
> Command ? ?: ? "left"
> ? ? ? ? ? ? ? ? ? ? | ? "open" (etc)
>
> but that makes the grammar a mess. Another idea I had was processing tokens
> from the lexer directly and building commands accordingly, but the code
> would also be a mess because I'd lose the syntactic verifications of the
> parser.
>
> In your opinion, what is the best approach for this type of problem ?

Some commands accept arguments, others don't. Three possible states:
command accepts|requires|ignores argument. If a command ignores
arguments, then the next token should be a new command string. If a
command requires arguments, then the next token should never be a new
command; parse it as an argument regardless. If a command accepts (but
doesn't need) arguments, parse the next token as an argument, and if
you get a syntax error try again as two commands.

Basically, you have to switch modes depending on where, positionally,
you are in the command statement, based on the grammar you describe.

-- 
Zach Collins (Siege)
"If code can be speech, then software can be art."



More information about the mud-dev2-archive mailing list