[DGD] parse_string question

Felix A. Croes felix at dworkin.nl
Sun Dec 30 16:13:38 CET 2007


bart at wotf.org wrote:

>[...]
> Assume I have a big mapping called verbs, containing some 500 or so verbs as
> index, with some verb specific arrays as values.
>
> Then I have a grammar that looks something like
> whitespace=/[ ]+/
> word=/[^ ]+/
> part: words verb words
> verb: word ? test_verb
> words: word
> words: words word
>
> and a function 
>
> mixed * test_verb(mixed * arg)
> {
>    if(arg && sizeof(arg) == 1)
>       return verbs[arg[0]];
>    return nil;
> }
>
> This would eliminate alternative parse trees by returning nil for everything
> that is not a verb.

Given that there could only be one 'verb' in a 'part', with the first
and last 'word' excluded, you'd have 8 parse trees when a user types
10 words.  That's not terrible (it only gets bad when the number of
parse trees grows exponentially) but you can do better.


> Alternatively, I could create a grammar that has a regexp for each verb in the
> verbs mapping by looping through the indeces of the map
>
> I'd still end up calling a function to change the array for verbs, but it
> would remove the ambiguity of the grammar I believe. Its also less flexible
> because I can't change the verbs it recognizes dynamically.

The grammar is just a string, and you can build it on the fly.

Regards,
Dworkin



More information about the DGD mailing list