What's the point of (grammar ...)?

What’s the point of (grammar $ for $) when (understand $ as $) can do everything the former can and then some?

(grammar $ for $) rules are templates. If the verb you are implementing does not have to have any fancy stuff that require processing beyond what a (grammar...) line can provide, there is no need to go for an (understand...) rule.
Other than that you are correct that anything that a (grammar...) line can do, (understand...) can do as well. In fact there are two special (understand...) rules in the library that query the (grammar entry...) table to provide the understanding of the new verbs there. They are:

(understand [$Verb | $Words] as $Action)
	*(grammar entry $Verb $Grammar $Action)
	*(match grammar $Grammar against $Words into $ObjList)
	(populate template $Action with $ObjList)

(understand [$Verb | $Words] as $Action)
	*(grammar entry $Verb $Grammar $Action reversed)
	*(match grammar $Grammar against $Words into $ObjList)
	(reverse $ObjList $RevObjList)
	(populate template $Action with $RevObjList)

OK, but is there any advantage to using (grammar $ for $) instead (understand $ as $) if you don’t need to do any fancy stuff? Is it faster?

Yes it is faster according to the manual and it is also more memory efficient, but even more importantly it is less prone to make bugs with the grammar tables that than freestyling with the understand rules.
From Chapter 10: Understanding player input :

[…] the library defines a generic (understand $ as $) rule that queries a table of grammar definitions. This table is called (grammar entry $ $ $), and it is constructed at compile-time from instantiations of the (grammar $ for $) access predicate.

The first parameter of the grammar entry is the first word of the grammar rule. This helps the compiler create efficient lookup code.

There are two reasons for translating the grammar tokens into numbers: It makes the code more compact (and therefore faster on old systems), and it prevents the grammar tokens from cluttering the game dictionary.

Edit: Typo.


Ah, thanks! It was all in the manual after all!

1 Like

Yeah, (grammar $ for $) is the newer system, which is basically syntactic sugar over (understand $ as $) for the most common cases. It used to be that you’d have to write your own (understand $ as $) rules every time you wanted to add a new verb; now there’s a simpler and more convenient way to do it, if you’re doing things in the most standard ordinary way.