Class Notes - Compiler Design
Class Notes - Compiler Design
COMPILER DESIGN
ASSIGNMENT 2
PART 1
%{
#include <unistd.h>
#include "y.tab.h"
#include <stdio.h>
%}
/* Regular definitions */
digit [0-9]
letter [a-zA-Z]
id {letter}({letter}|{digit})*
digits {digit}+
1
opFraction (\.{digits})?
opExponent ([Ee][+-]?{digits})?
number {digits}{opFraction}{opExponent}
%option yylineno
%%
2
"}" {return *yytext;}
{number} {
return T_NUM;
{id} {
return T_ID;
%%
Parser.y
%{
#include "abstract_syntax_tree.c"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
%}
3
%union // union to allow nodes to
store return different datatypes
char* text;
expression_node* exp_node;
%start START
%%
START : SEQ {
display_exp_tree($1);
printf("\n");
YYACCEPT;
};
4
| S {$$=$1;}
$$=init_exp_node(strdup("if"),$3,$6,NULL);
$$=init_exp_node(strdup("if-else"),$3,$6,$10);
| ASSGN { $$=$1;}
C : F RELOP F {
$$=init_exp_node(strdup($2),$1,$3,NULL);
| '>' { $$=">";}
| '>''=' { $$=">=";}
| '<''=' { $$="<=";}
| '=''=' { $$="==";}
| '!''=' { $$="!=";}
5
ASSGN : T_ID '=' E ';' {
$$ =
init_exp_node(strdup("="),init_exp_node(strdup($1),NULL,NULL,NULL),$3,NULL);
/* Expression Grammar */
E : E '+' T {
$$ = init_exp_node(strdup("+"),$1,$3,NULL);
| E '-' T {
$$ = init_exp_node(strdup("-"),$1,$3,NULL);
| T { $$ = $1; }
T : T '*' F {
$$ = init_exp_node(strdup("*"),$1,$3,NULL);
| T '/' F {
$$ = init_exp_node(strdup("/"),$1,$3,NULL);
| F { $$ = $1; }
| T_ID {
6
$$ = init_exp_node(strdup($1),NULL,NULL,NULL);
| T_NUM {
$$ = init_exp_node(strdup($1),NULL,NULL,NULL);
%%
void yyerror(char* s)
/* main function - calls the yyparse() function which will in turn drive
yylex() as well */
yyparse();
return 0;
#include <stdio.h>
7
#include <stdlib.h>
#include <string.h>
#include "abstract_syntax_tree.h"
node->left = left;
node->mid=mid;
node->right=right;
node->val= val;
return node;
printf("%s, ",exp_node->val);
display_exp_tree(exp_node->left);
display_exp_tree(exp_node->mid);
display_exp_tree(exp_node->right);
Abstract_syntax_tree.h
typedef struct expression_node
8
struct expression_node* left;
char* val;
}expression_node;
Output 1:
Output 2 :
Output 3
9
PART 2
ICG
Lexer.l
%{
#include <unistd.h>
#include "y.tab.h"
#include <stdio.h>
extern void yyerror(const char *); // declare the error handling function
%}
/* Regular definitions */
digit [0-9]
letter [a-zA-Z]
id {letter}({letter}|{digit})*
digits {digit}+
opFraction (\.{digits})?
opExponent ([Ee][+-]?{digits})?
number {digits}{opFraction}{opExponent}
%option yylineno
%%
10
">=" {return GTEQ;}
{number} {
return T_NUM;
{id} {
return T_ID;
11
. {} // anything else => ignore
%%
Parser.y
%{
#include "quad_generation.c"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE* icg_quad_file;
int temp_no = 1;
int label_no=1;
%}
%start START
12
%nonassoc T_IF
%nonassoc T_ELSE
%%
START : S {
printf("Valid syntax\n");
YYACCEPT;
};
/* Expression Grammar */
E : E '+' T {
$$ = new_temp();
| E '-' T {
$$ = new_temp();
13
quad_code_gen($$, $1, "-", $3);
| T
T : T '*' F {
$$ = new_temp();
| T '/' F {
$$ = new_temp();
| F
F : '(' E ')' {
$$=strdup($2);
| T_ID {
$$=strdup($1);
| T_NUM {
$$=strdup($1);
14
}
| T_IF '('C')' OC S CC {
$2 = new_label();
quad_code_gen($2,"","goto","");
quad_code_gen($3,"","Label","");} T_ELSE OC S CC
{quad_code_gen($2,"","Label","");}S
| ASSGN ';' S
|'{'S'}'
C : E rel E { $$ = new_temp();
$1 = new_label();
quad_code_gen($1,$$,"if","");
$$ = new_label();
quad_code_gen($$,"","goto","");
quad_code_gen($1,"","Label","");
};
rel : GT {strcpy($$,">");}
| LT {strcpy($$,"<");}
| LTEQ {strcpy($$,"<=");}
| GTEQ {strcpy($$,">=");}
15
| EQQ {strcpy($$,"==");}
| NEQ {strcpy($$,"!=");}
%%
void yyerror(char* s)
/* main function - calls the yyparse() function which will in turn drive
yylex() as well */
icg_quad_file = fopen("icg_quad.txt","a");
yyparse();
fclose(icg_quad_file);
return 0;
Quad generation.c
#include <stdio.h>
16
#include <stdlib.h>
#include <string.h>
#include "quad_generation.h"
fprintf(icg_quad_file,"%s\t%s\t%s\t%s\n",op,b,c,a);
char* new_temp()
sprintf(tempNew,"t%d",temp_no);
temp_no++;
return tempNew;
char* new_label()
sprintf(label,"L%d",label_no);
label_no++;
return label;
Quad_generation.h
17
extern FILE* icg_quad_file; //pointer to the output file
char* new_temp();
Output 1:
Ouput 2:
18
19