Compiler Design Project Report
Compiler Design Project Report
PROJECT REPORT
MINOR PROJECT
The main objective of the project is to generate the intermediate code for the given
IF-ELSE Statement. It consists of four main sections , the conditional section inside
the IF statement , the THEN part of the statement which the code enters if the given
IF condition is true , the ELSE part of the statement which the code enters if the
given IF condition is false , and the statements after the ELSE construct which is
evaluated after either the IF or ELSE part.
The program is modularized into three separate levels , the condition part , where in it
moves to different levels of code within the condition part as per the different
Boolean operators . The inside of the IF and ELSE statements move into different
intermediate codes as per different assignment statements.
Lex is a tool for building lexical analyzer or lexer. A lexer takes an arbitrary input
stream and tokenizes it. In this program, when we create a lex specification, we create
a set of pattern which lex matches against the input. Lex program basically has three
sections one is definition section another is rule section and last one is user subroutine
section which in this case is not written here but in yacc program. In the definition
section we have defined the header files and also y.tab.h which helps it to link up with
the yacc program. The rules section contain the patterns that has to be matched and
followed by an action part, which returns the tokens to the yacc program when the
input is parsed and the pattern is matched.
PROGRAM:
%{
#include”y.tab.h”
%}
ALPHA [A-Za-z] // variable defined for alphabets
Yacc takes a grammar that we specify and writes a parser that recognizes valid syntax
in that grammar. Grammar is a series of rules that the parser uses to recognize
syntactically valid input. Again in same way as lex specification, a yacc grammar has
three part structure. The first section is the definition section, handles control
information for the yacc generated by the parser. The second section contains the rule
of parser and the third section contains the C code. In the following program, the
definition section has the header files that will generate the necessary files needed to
run the C program next comes the declaration of the tokens which are passed from
the lex program. Then we define the grammar for the valid input typed by the user.
Then it contains the necessary SDT’s (Syntax Directed Translations) which move to a
specific function when that particular input is encountered by the parser . The
conditional code moves as per the Boolean expression defined into various levels .
The number of labels generated is equal to the number of the Boolean comparisons in
the conditional code . In the IF and THEN part of the statement , an intermediate
code for the corresponding assignment statements is generated . After which it jumps
to the ending of the code.
PROGRAM:
;
F :'='{push();}D
|D{pusha();}
;
D :NUM{push();} // left side of expression is a number or a identifier
|ID{push();}
;
E :V '='{push();} E{codegen_assign();}//SDT for assignment operator
| E '+'{push();} E{codegen();}
| E '-'{push();} E{codegen();}
| E '*'{push();} E{codegen();}
| E '/'{push();} E{codegen();}
| '(' E ')'
| '-'{push();} E{codegen_umin();} %prec UMINUS
| V
| NUM{push();}
| S
;
V : ID {push();}
;
%%
abc()
{
abcd++;
printf("\nX%d : if ",abcd);
}
abcde()
{
abcd++;
printf("\nX%d :not ",abcd);
}
second() // used for Boolean AND condition
{
int xyz=0;
xyz=abcd+1;
printf("falg=true else flag=false");
printf("\n if flag(true) goto x%d",xyz);
printf("\n if flag(false) goto L1");
}
first() // used for single Boolean condition
{
printf("flag=true else flag=false");
printf("\n if flag(true) goto L0");
printf("\n if flag(false) goto L1");
}
third() // used for Boolean OR condition
{
int xyz=0;
xyz=abcd+1;
printf("flag=true else flag=false");
printf("\n if flag(true) goto L0 ");
printf("\n if flag(false) goto x%d",xyz);
}
codegen() //used to print out the pushed elements in the stack
{
strcpy(temp,"t");
strcat(temp,i_);
printf("%s = %s %s %s\n",temp,st[top-2],st[top-1],st[top]);
top-=2;
strcpy(st[top],temp);
i_[0]++;
}
codegen_umin()
{
strcpy(temp,"t");
strcat(temp,i_);
printf("%s = -%s\n",temp,st[top]);
top--;
strcpy(st[top],temp);
i_[0]++;
}
OUTPUT:
X1: if a > 0 flag=true else flag=false
If flag(true) goto X2
If flag(false) goto L1
If flag(true) goto L0
If flag(false) goto X3
If flag(true) goto L0
If flag(false) goto L1
L0:
T0=x + 10
a=t0
L1:
t1 =x + 10
b = t1
L2:
Snapshot: