Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Kaviraj N(2112064) 19CS62C-PRINCIPLES OF COMPILER DESIGN LAB

Exercise 6 b): To generate Three Address Code to Implement Quadruples and triples using LEX
and YACC

Aim:

To implement a simple three address code generator to define an expression grammar rules
using YACC specification, subroutine for action and use lex code specification to generate 3
address code in the form of Quadruples and Triples for the given input source and provide it to
parser for evaluation.

Algorithm:

6b.l

1. Header Inclusion: The code begins with the inclusion of "y.tab.h", which typically
contains definitions used by the yacc/bison parser.

2. Lexical Analyzer Definition: The %{ ... %} block is used to include C code that will be
copied verbatim to the generated lexer code. Here, it's including "y.tab.h". This section is
typically used for including header files and defining global variables.

3. Lexical Rules: Between the %% delimiters are the lexical rules. These rules define
patterns and corresponding actions to be taken when those patterns are matched.

• [0-9]+?: Matches one or more digits. The +? makes the + operator non-greedy,
meaning it will match as few characters as possible.

• Action: Sets yylval.sym to the first character of the matched text and
returns NUMBER.

• [a-zA-Z]+?: Matches one or more letters. Similar to the previous pattern, it's non-
greedy.

• Action: Sets yylval.sym to the first character of the matched text and
returns LETTER.

• \n: Matches a newline character.

• Action: Returns 0, indicating the end of an input line.

• .: Matches any character.

• Action: Returns the first character of the matched text.

4. yywrap Function: This function is called when the end of the input is reached. It's
typically used to inform the lexer that there's no more input to process.

Exno:6 Page 1
Kaviraj N(2112064) 19CS62C-PRINCIPLES OF COMPILER DESIGN LAB

• In this implementation, it simply returns 0, indicating there is no need for further


action.

6b.y

1. Setup:

• Include necessary header files.

• Declare global variables and structures.

2. Lexer Setup:

• Define the union type for token values.

• Define tokens and their types.

• Define precedence and associativity for operators.

3. Grammar Rules:

• Define grammar rules for statements and expressions.

4. Error Handling:

• Define yyerror function to handle parsing errors.

5. Code Generation:

• Implement AddToTable function to generate intermediate code.

• Generate intermediate code for each operation encountered during parsing.

6. Output Generation:

• Implement functions to generate different types of intermediate code: three-


address code, quadruple code, and triple code.

7. Parsing:

• In main function, prompt user to enter an expression.

• Parse the expression using yyparse.

8. Output:

Exno:6 Page 2
Kaviraj N(2112064) 19CS62C-PRINCIPLES OF COMPILER DESIGN LAB

• After parsing, generate and print different types of intermediate code for the input
expression

Code:
6b.l
%{
#include "y.tab.h"
%}
%%
[0-9]+? {yylval.sym=(char)yytext[0]; return NUMBER;}
[a-zA-Z]+? {yylval.sym=(char)yytext[0];return LETTER;}
\n {return 0;}
. {return yytext[0];}
%%
int yywrap()
{
return 0;
}
6b.y
%{
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void ThreeAddressCode();
void triple();
void qudraple();
char AddToTable(char ,char, char);

Exno:6 Page 3
Kaviraj N(2112064) 19CS62C-PRINCIPLES OF COMPILER DESIGN LAB

int ind=0;//count number of lines


char temp = '1'; //for t1,t2,t3.....
struct incod
{
char opd1;
char opd2;
char opr;
};
%}
%union
{
char sym;
}
%token <sym> LETTER NUMBER
%type <sym> expr
%left '+'
%left '*''/'
%left '-'
%%
statement: LETTER '=' expr ';' {AddToTable((char)$1,(char)$3,'=');}
| expr ';'
;
expr:
expr '+' expr {$$ = AddToTable((char)$1,(char)$3,'+');}
| expr '-' expr {$$ = AddToTable((char)$1,(char)$3,'-');}
| expr '*' expr {$$ = AddToTable((char)$1,(char)$3,'*');}
| expr '/' expr {$$ = AddToTable((char)$1,(char)$3,'/');}

Exno:6 Page 4
Kaviraj N(2112064) 19CS62C-PRINCIPLES OF COMPILER DESIGN LAB

| '(' expr ')' {$$ = (char)$2;}


| NUMBER {$$ = (char)$1;}
| LETTER {$$ = (char)$1;}
|'-' expr {$$ = AddToTable((char)$2,(char)'\t','-');}
;
%%
yyerror(char *s)
{
printf("%s",s);
exit(0);
}
struct incod code[20];
char AddToTable(char opd1,char opd2,char opr)
{
code[ind].opd1=opd1;
code[ind].opd2=opd2;
code[ind].opr=opr;
ind++;
return temp++;
}
void ThreeAddressCode()
{
int cnt = 0;
char temp = '1';
printf("\n\n\t THREE ADDRESS CODE\n\n");
while(cnt<ind)
{

Exno:6 Page 5
Kaviraj N(2112064) 19CS62C-PRINCIPLES OF COMPILER DESIGN LAB

if(code[cnt].opr != '=')
printf("t%c : = \t",temp++);
if(isalpha(code[cnt].opd1))
printf(" %c\t",code[cnt].opd1);
else if(code[cnt].opd1 >='1' && code[cnt].opd1 <='9')
printf("t%c\t",code[cnt].opd1);
printf(" %c\t",code[cnt].opr);
if(isalpha(code[cnt].opd2))
printf(" %c\n",code[cnt].opd2);
else if(code[cnt].opd2 >='1' && code[cnt].opd2 <='9')
printf("t%c\n",code[cnt].opd2);
cnt++;
}
}
void quadruple()
{
int cnt = 0;
char temp = '1';
printf("\n\n\t QUADRAPLE CODE\n\n");
while(cnt<ind)
{
printf(" %c\t",code[cnt].opr);
if(code[cnt].opr == '=')
{
if(isalpha(code[cnt].opd2))
printf(" %c\t \t",code[cnt].opd2);
else if(code[cnt].opd2 >='1' && code[cnt].opd2 <='9')

Exno:6 Page 6
Kaviraj N(2112064) 19CS62C-PRINCIPLES OF COMPILER DESIGN LAB

printf("t%c\t \t",code[cnt].opd2);
printf(" %c\n",code[cnt].opd1);
cnt++;
continue;
}
if(isalpha(code[cnt].opd1))
printf(" %c\t",code[cnt].opd1);
else if(code[cnt].opd1 >='1' && code[cnt].opd1 <='9')
printf("t%c\t",code[cnt].opd1);
if(isalpha(code[cnt].opd2))
printf(" %c\t",code[cnt].opd2);
else if(code[cnt].opd2 >='1' && code[cnt].opd2 <='9')
printf("t%c\t",code[cnt].opd2);
else printf(" %c",code[cnt].opd2);
printf("t%c\n",temp++);
cnt++;
}
}
void triple()
{
int cnt=0;
char temp='1';
printf("\n\n\t TRIPLE CODE\n\n");
while(cnt<ind)
{
printf("(%c) \t",temp);
printf(" %c\t",code[cnt].opr);

Exno:6 Page 7
Kaviraj N(2112064) 19CS62C-PRINCIPLES OF COMPILER DESIGN LAB

if(code[cnt].opr == '=')
{ if(isalpha(code[cnt].opd2))
printf(" %c \t \t",code[cnt].opd2);
else if(code[cnt].opd2 >='1' && code[cnt].opd2 <='9')
printf("(%c)\n",code[cnt].opd2);
cnt++;
temp++;
continue;
}
if(isalpha(code[cnt].opd1))
printf(" %c \t",code[cnt].opd1);
else if(code[cnt].opd1 >='1' && code[cnt].opd1 <='9')
printf("(%c)\t",code[cnt].opd1);
if(isalpha(code[cnt].opd2))
printf(" %c \n",code[cnt].opd2);
else if(code[cnt].opd2 >='1' && code[cnt].opd2 <='9')
printf("(%c)\n",code[cnt].opd2);
else printf(" %c\n",code[cnt].opd2);
cnt++;
temp++;
}
}
main()
{
printf("\n Enter the Expression : ");
yyparse();
ThreeAddressCode();

Exno:6 Page 8
Kaviraj N(2112064) 19CS62C-PRINCIPLES OF COMPILER DESIGN LAB

quadruple();
triple();
}

Output:

Exno:6 Page 9
Kaviraj N(2112064) 19CS62C-PRINCIPLES OF COMPILER DESIGN LAB

Problem Program
Understanding and Implementation
Algorithm Record Total
(30)
(10) (10) (50)

Result:

Hence the three Address Code to Implement Quadruples and triples using LEX and YACC

Was successfully generated and output was verified

Exno:6 Page 10

You might also like