|
[NEXT] [PREV]
HANDBOOK
/ GENTLE PRIMER
/ At a Glance
/Code Generation |
|
Gentle
Applications Concepts Examples Handbook Support Download
|
While the parser of a compiler translates a concrete program
(of the source language) into a (high-level)
abstract syntax
(which may then be transformed into a lower-level intermediate
representation),
the code generator of a compiler translates the abstract syntax into
a concrete program (of the target language).
This mapping can be specified in exactly the same way as within the
parser with the exception that abstract syntax now becomes an input parameter
that controls rule selection.
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)
|