As an example, we translate expressions into code for a stack computer: here, the code for operands is followed by an operator that works on these operands.
The root clause of this compiler is
'root' expression(-> X) code(X)
The procedure code is defined as follows
'action' code (Expr) 'rule' code(plus(X1, X2)): code(X1) code(X2) print("plus") 'rule' code(minus(X1, X2)): code(X1) code(X2) print("minus") 'rule' code(mult(X1, X2)): code(X1) code(X2) print("mult") 'rule' code(div(X1, X2)): code(X1) code(X2) print("div") 'rule' code(neg(X)): code(X) print("neg") 'rule' code(num(N)): print(N)