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

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

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.

• \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.

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


action.

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

6a.y

1. Setup:

• Header inclusion: Include necessary header files (stdio.h, string.h, stdlib.h).

• Declare global variables and structures.

2. Lexer Setup:

• %{ ... %} block: Include headers, define global variables.

• %union: Define the union type for token values.

• %token and %type: Define tokens and their types.

• Define precedence and associativity for operators.

3. Grammar Rules:

• Define grammar rules for statements and expressions using yacc/bison syntax.

• Each rule corresponds to a production in the language grammar.

4. Error Handling:

• Define yyerror function to handle parsing errors.

5. Code Generation:

• Implement AddToTable function to generate three-address code.

• Generate three-address code for each operation encountered during parsing.

6. Parsing:

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

• Parse the expression using yyparse.

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

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++;
}
}
int main()
{
printf("\n Enter the Expression : ");
yyparse();
ThreeAddressCode();
return 0;
}

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

You might also like