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);
                  efadd();}

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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.