Professional Documents
Culture Documents
ex 6b pcd
ex 6b pcd
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.
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
6b.y
1. Setup:
2. Lexer Setup:
3. Grammar Rules:
4. Error Handling:
5. Code Generation:
6. Output Generation:
7. Parsing:
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
Exno:6 Page 4
Kaviraj N(2112064) 19CS62C-PRINCIPLES OF COMPILER DESIGN LAB
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
Exno:6 Page 10