[DGD]parse_string() problems

Geir Harald Hansen geirhans at ifi.uio.no
Sun May 23 17:36:12 CEST 1999


Hi,

finally I'm starting to work on my command parser.  And being a
parse_string() newbie, I'm running into some problems.  I include
a minimal piece of code to illustrate my problem:

static create()
{
  grammar = "\
whitespace = / +/ \
word = /[^ ]+/ \
verbrule: 'say' str \
substr: word \
substr: word substr \
str: substr ? parse_str \
";
}

static mixed *parse_str(string *str)
{
  return ({ ({ "_str", implode(str, " ") }) });
}

What I'm having trouble with at the moment is this:
I intended for the str rule to include any string.
The problem is that tokens are implicitly created from string constants,
like 'say' above, and these take precedence over the 'word' token rule.
>From the parser documentation:

   String constants take precedence over regular expressions, and any
   token that matches both a string constant and a regular expression
   will always match the string constant, only.
                                          ^^^^
The result is that my str production can never contain a 'say'.
So I cannot parse commands like:
  "say If only I could say what I want."
Any suggestions how to solve this?

Another question:
I want to have a verb production rule, that should match all known verbs.
I can see two ways of doing this, either:
  verb: 'look' verb: 'say' verb: 'get' verb: 'drop' etc. etc.
Or:
  verb: word ? check_verb
Where check_verb() would check whether the word is part of some verb
array, returning the word unchanged if it is, otherwise nil.

The last option would make the grammar smaller, but result in more execution
of LPC code.  Since the function does not modify the word, perhaps
the first solution is best?
For adjectives pretty much the same thing, although I may want to
allow abbreviations.  ("smile hap")  Which means I need an LPC function.

As an aside, what is the fastest way to check whether something is an
element of an array?  Is "if (sizeof(({ element }) & array))" faster
than those loops that compare with every element?

Finally; if anyone knows of publicly released DGD parser code/grammars or
would let me see theirs, just to get some idea of how things can be done,
it would be greatly appreciated. :)

Your hopeful (not to get a headache) parser friend,
Geir Harald Hansen (geirhans at ifi.uio.no)

List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list