Professional Documents
Culture Documents
ex 6a pcd
ex 6a pcd
Exercise 6 a): Generate three address code for a simple program 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 from the given input source and provide it to parser for evaluation.
Algorithm:
6a.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
6a.y
1. Setup:
2. Lexer Setup:
3. Grammar Rules:
• Define grammar rules for statements and expressions using yacc/bison syntax.
4. Error Handling:
5. Code Generation:
6. Parsing:
7. Output:
• After parsing, generate and print the three-address code for the input expression
using ThreeAddressCode function.
Exno:6 Page 2
Kaviraj N(2112064) 19CS62C-PRINCIPLES OF COMPILER DESIGN LAB
Code:
6a.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;
}
6a.y
%{
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void ThreeAddressCode();
char AddToTable(char ,char, char);
int ind=0; // count number of lines
char temp = '1'; // for t1, t2, t3, ...
struct incod
{
char opd1;
Exno:6 Page 3
Kaviraj N(2112064) 19CS62C-PRINCIPLES OF COMPILER DESIGN LAB
char opd2;
char opr;
};
%}
%union
{
char sym;
}
%token <sym> LETTER NUMBER
%type <sym> expr
%left '+'
%left '*''/'
%left '-'
%%
statement: LETTER '=' expr ';' {AddToTable($1, $3, '=');}
| expr ';' {}
;
expr:
expr '+' expr {$$ = AddToTable($1, $3, '+');}
| expr '-' expr {$$ = AddToTable($1, $3, '-');}
| expr '*' expr {$$ = AddToTable($1, $3, '*');}
| expr '/' expr {$$ = AddToTable($1, $3, '/');}
| '(' expr ')' {$$ = $2;}
| NUMBER {$$ = $1;}
| LETTER {$$ = $1;}
|'-' expr {$$ = AddToTable($2, '\t', '-');}
Exno:6 Page 4
Kaviraj N(2112064) 19CS62C-PRINCIPLES OF COMPILER DESIGN LAB
;
%%
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)
{
if(code[cnt].opr != '=')
printf("t%c : = \t",temp++);
if(isalpha(code[cnt].opd1))
printf(" %c\t",code[cnt].opd1);
Exno:6 Page 5
Kaviraj N(2112064) 19CS62C-PRINCIPLES OF COMPILER DESIGN LAB
Output:
Exno:6 Page 6
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 for a simple program using LEX and YACC was successfully
generates and output was verified
Exno:6 Page 7