[DGD] parse_string() example

Felix A. Croes felix at dworkin.nl
Tue Oct 7 20:42:09 CEST 1997


Grammar:

    whitespace:	' '
    word:	'[a-zA-Z0-9][a-zA-Z0-9]*'

    SENTENCE =	'give' OBJ 'to' LIV
    SKIP =	'the'
    SKIP =	'a'
    SKIP =	'an'
    SKIP =	'any'
    SKIP =
    NOUN =	word
    ADJS =	word ADJS
    ADJS =
    OBJ =	( SKIP ) ( ADJS ) NOUN ? find_obj
    LIV =	( SKIP ) ( ADJS ) NOUN ? find_liv

Notes:
 - For clarity, terminals have been written in lowercase and non-terminals
   in uppercase.  parse_string() does not enforce this, except for
   `whitespace' which always has to be lowercase.
 - The first grammar rule is the starting rule.
 - The return value from the functions find_obj() and find_liv(), if non-
   zero, will substitute the sentence in higher level rules and the final
   parse tree, something that I forgot to mention in the RFC.


If find_obj() and find_liv() both return matching objects, then the
following sentence

    "give long blue sword to the small dwarf"

will cause find_obj() to be called with

    ({ ({ }), ({ "long", "blue" }), "sword" })

and find_liv() with

    ({ ({ "the" }), ({ "small" }), "dwarf" })

and parse_string() will return the result

    ({ "give", OBJ(sword), "to", OBJ(dwarf) })


Dworkin



More information about the DGD mailing list