7.2 Semantic Actions in YACC
TBoS p 156-169
Semantic actions are attached to grammar rules by following each rule by a :=. This YACC definition receives a list of as and changes them to bs.
a := [b | ];
a := [b];)
(compile (function ) [a a a a a])
[b b b b b]
The first rule says that any input of the form a is to be translated into an output consisting of b consed to the translation of .
The syntax of requires that the input be a non-empty list composed of occurrences of a. So (compile (function ) [a a a]) gives [b b b].
The second rule is the base case.
As in Shen, round brackets signify function applications and square ones form lists. The following reformulation is an example:
:= (question );)
NP VP -> (append [(protect Is) it true that] NP VP [?]))
(compile (function ) [the cat likes the dog])
[Is it true that the cat likes the dog ?]
and are both reserved non-terminals. always succeeds consuming none of the input and under semantic completion returns the empty list.
always succeeds and consumes all of the input and under semantic completion returns that remaining input.