12.8 Code Generation

We generate three kinds of outputs – raw assembly language code, reverse Polish notation (RPN) Intermediate Representation (IR) and 4-tuple IR. The code generation is achieved through yacc action terms.

Due to space constraints we do not propose to give the complete listings of the yacc action terms, but give a few indicative examples below.

12.8.1 Arithmetic Expression

We give here the yacc action terms for expr ‘+’ expr:

| expr ‘+’ expr { fprintf(fout1,″+\n″); $$ = $3;
                  fprintf(fout2,″%d: ADD T%d T%d T%d\n″, ++mcount,
                           tpop(), tpop(), ++tcount); tpush(tcount);

The files fout1 and fout2 get the RPN IR and 4-tuple IR outputs, respectively. The raw assembly language code is generated by the function efadd() in the ...

Get Compilers: Principles and Practice now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.