Tads 3 Grammar Intrinsics

Hello,

I wish to modify a grammar rule at run-time.
In the technical docs, there is an article about how to create a new grammar at run-time and add content via addAlt …

local prod = new GrammarProd();
prod.addAlt('noun->n1', new NounPhraseProd(), cmdDict, symtab);

but unfortunately there is no explanation how to modify an existing rule (there is only a slight hint that the prodName property (like grammar prodName(tag): …) is stored in an string-like object, but how can I refer to this object? Hopefully somebody here could give me a nudge in the right direction …

Greetings,
–MI

Are you trying to modify the verb rules for an action? If yes, you do that with the VerbRule() function-macro and the modify keyword and override the default rules. For example, if you wanted to modify the ThrowAt action to also allow something like “throw ball through window” (by default only “at” is allowed, not “through”), you would do this:

modify VerbRule(ThrowAt) ('throw' | 'toss') dobjList ('at' | 'through' | 'thru') singleIobj : /* empty */ ;

You leave the action and verb phrase specification empty.

No, I want to try a new approach in defining verbs, and now I found out how it works!
In case anyone will ever need this, it goes like that:

first we need a function that gives back the object from a string

globalSymToVal(val)
{    
    return t3GetGlobalSymbols()[val];      
}

then we are able to refer to grammar objects and we can use it for addAlt function calls.
I’ve set up a VerbResolver object which serves as a new approach in defining verbs. It uses an internal lookuptable and adds grammar at its construct call to an existing rule called VerbWord which resolves into various verb - preposition - object slots. This add a great deal of flexibility and is also new way to present the verb definitions to the author in a readable and compact way. (Assuming that we have defined a VerbRule(Verb) grammar and a PrepWord(Prep) grammar that resolves somewhere in a predicate grammar VerbPrepObj (see below)

VerbResolver: object
    
    construct() {
        
        local lst = tab.keysToList();
                
        foreach(local cur in lst) {
            
            for (local i = 1; i <= cur.length() ; i++) {
           
                if (i == 1) {
                    local obj= globalSymToVal('VerbWord');
                    obj.addAlt('"<<cur[i]>>"', new DynamicProd()); //add the string cur[i] (e.g. 'pick' as new grammar to existing VerbWord rule)
                }
                else {
                    local obj= globalSymToVal('PrepWord');
                    obj.addAlt('"<<cur[i]>>"', new DynamicProd()); //add the string cur[i] (e.g. 'up' as new grammar to existing PrepWord rule)
                }
            }       
        }
    }
    
    tab = [
        ['pick','up'] -> Take,
        // to be completed with all various verb preposition combinations ....
        * -> 'undefined'
    ]

    getValidVerbPhrase(obj) {

        local verbTok = nil;
        local prepTok = nil;
        local myVerb = nil;
        local myPrep = nil;
        
        if (!obj.verb_) return UnmatchedVerb; //we have no verb
        
        if (obj.verb_) {
            verbTok = obj.verb_.tokenList.sublist(obj.verb_.firstTokenIndex,obj.verb_.firstTokenIndex);
            myVerb = getTokVal(verbTok[1]);
        }
        if (obj.prep_) {
            prepTok = obj.prep_.tokenList.sublist(obj.prep_.firstTokenIndex,obj.prep_.firstTokenIndex);
            myPrep = getTokVal(prepTok[1]);
        }
        
        local solvedAction = tab[[myVerb,myPrep]];
        
        if (solvedAction != 'undefined')
            return solvedAction;
        else
            return UnmatchedVerb;
    }

;

DefineIAction(UnmatchedVerb)
    execAction(cmd)
    {
        "I didn&lsqos;t understand this combination of verb and prepositon. "; //for testing
    }
;

class VerbWordProd: DynamicProd
;
class PrepWordProd: DynamicProd
;

grammar VerbWord(Verb):
    ' ' // must not be empty
    : VerbWordProd
;

grammar PrepWord(Preposition):
    ' ' //must not be empty 
    : PrepWordProd
;

VerbRule(VerbPrepObj)
    VerbWord -> verb_ multiDobj
    | VerbWord -> verb_ PrepWord -> prep_ multiDobj
    | VerbWord -> verb_ multiDobj PrepWord -> prep_
    : VerbProduction
    action = VerbResolver.getValidVerbPhrase(self)
;

Of course, the example above (pick up something) is already defined in the standard lib and I’m not sure whether it will be possible to cover all the various verb preposition object forms but I’ll keep on testing. It might be an interesting alternative grammar definition solution. If someone is interested in this approach (I think especially of translatory works) i’ll give you an update notice when I got the slightest form of a complete solution.
Greetings,
– MI