#! /usr/local/bin/perl -sw # extend-grammar.pl by Teodor Zlatanov, tzz@iglou.com # March 26, 2000 # A script that extends the proper names it knows, as they are added # with the "name exists" command. # based on demo_selfmod.pl: A SELF-MODIFYING**2 PARSER # from the Parse::RecDescent distribution use Parse::RecDescent; $grammar = q{ process: query | definition query: do_you_know proper_name { print "I know " . $item{proper_name} . ", sure!\n" } query: do_you_know name { print $item{name} . " does not exist in my little world, sorry.\n" } definition: name /exists/i { $thisparser->Extend("proper_name: '$item{name}'"); print "\"$item{name}\" is now a valid proper name\n"; } proper_name: /Ted/ name: /\w+/ do_you_know: /do/i /you/i /know/i }; $parse = new Parse::RecDescent ($grammar); while (<>) { $parse->process($_) or print "Enter a query (do you know ...) or a definition (... exists)\n"; }