O'Reilly logo

Compilers: Principles and Practice by Himanshu B. Dave, Parag H. Dave

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required