[go: up one dir, main page]

0% found this document useful (0 votes)
36 views8 pages

Practicalcode 08 10 Session2023 24

The document lists 10 practical assignments for a Compiler Design course. The practicals include writing Lex and Yacc programs to perform tasks like counting words, implementing a lexical analyzer, and generating three address code. Guidelines for each practical are provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views8 pages

Practicalcode 08 10 Session2023 24

The document lists 10 practical assignments for a Compiler Design course. The practicals include writing Lex and Yacc programs to perform tasks like counting words, implementing a lexical analyzer, and generating three address code. Guidelines for each practical are provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

ACAD-32 a) Iss. No.

: 01,
Shri Ramdeobaba College of Engineering and Management, Rev. No.: 00
Nagpur -440013 Date of Rev:
Ref. Clause(s): 7.5.1
01/01/2018
Department:
List of Practicals
Information Page: 01/01
Sem: VI Course : Compiler Design [ ITP357]
Technology
Program:
Session: 2023-24 Date: 20-01-2024
B.Tech

COs
Sr.No. Name of Practical
Mapped
8 Write lex program CO2,4
a. To count the number of words in every line of the input program file.
( Note: Input file is any source program file )
b. To design lexical analyzer for C like language and print the output as
lexeme and token type.
(Note : Write rules for few keywords, identifiers, special symbols,
arithmetic symbols, integer constant and float constant )

a) Aim : Program to count the number of vowels and consonants in a given string.

%{ #include<stdio.h>
int vowels=0;
int cons=0;
%}
%%
[aeiouAEIOU] { vowels++;}
[a-zA-Z] { cons++;}
%%
int yywrap() /* if %option noyywrap is written at the start of the
{ return 1; } program so no need to write the function yywrap */

main() {
printf(“Enter the string\n”); /* at the end of input use ctrl-d */
yylex();
printf(“No of vowels=%d\nNo of consonants=%d\n”,vowels,cons);
}
b) Make relevant changes in following program for work as lexical analyzer. This
program only recognized only identifiers do it for keywords, special symbols also.

%{
#include<stdio.h>
int count=0;
%}
op [+-*/]
letter [a-zA-Z]
digit [0-9]
id {letter}*|({letter}{digit})+
notid ({digit}{letter})+
%%
[\t\n]+
{id} {printf("%s is an identifier\n", yytext);
count++;}
{notid} {printf("%s is not an identifier\n", yytext);}
%%

int main()
{
FILE *fp;
char file[10];
printf("\nEnter the filename: ");
scanf("%s", file);
fp=fopen(file,"r");
yyin =fp;
yylex();
printf("Total identifiers are: %d\n", count);
return 0;
}

9 Write a Yacc Program accept strings from arithmetic grammar. CO5


Lex File

%option noyywrap
%{
#include "y.tab.h"
%}
%%
[0-9]+ { return NUMBER; }
[a-zA-Z][a-zA-Z0-9_]* { return ID; }
\n { return NL ;}
. { return yytext[0]; }
%%
YACC File

%{
#include<stdio.h>
%}

%token NUMBER ID NL
%left '+' '-'
%left '*' '/‘

%%
stmt : exp NL { printf("Valid Expression"); }
;

exp : exp '+' exp


| exp '-' exp
| exp '*' exp
| exp '/' exp
| '(' exp ')'
| ID
| NUMBER
;
%%

main ()
{
printf("Enter the expression\n");
yyparse();
}

10 Write a program to create three address code for arithmetic statement using Lex / CO6
YACC
( eg. A =B + C * F )

/* Lex for TAC*/

%{
#include"y.tab.h"
extern char yyval;
%}

%%

[0-9]+ {yylval.dval =(char)(yytext[0]); return NUM; }


[a-z] {yylval.dval =(char)(yytext[0]); return LETTER; }
\n { return 0; }
. { return yytext[0]; }
%%

/* YACC for TAC */


%{
#include"y.tab.h"
#include<stdio.h>
char addtotable(char,char, char, char);
int index=0;
char t='A';
struct expr{
char op1;
char op2;
char op3;
char r;
};
%}

%union{ char dval; }

%token <dval> NUM LETTER


%type <dval> S E

%%
S:LETTER '=' E ';' {addtotable((char)$1,'-',(char)$3,'='); }
;
E: E '+' E { $$ = addtotable((char)$1,(char)$1,(char)$3,'+'); }
| NUM { $$ = (char)$$; }
| LETTER { (char)$$;}

%%

struct expr arr[20];


void yyerror(char *s)
{
printf("Errror %s",s);
}

char addtotable(char t,char a, char b, char o)


{
//t++;
//printf("%c",t);
arr[index].op1 =a;
arr[index].op2 = b;
arr[index].op3 = o;
arr[index].r = t;

printf("%c\t",arr[index].op3);
printf("%c\t",arr[index].op1);
printf("%c\t",arr[index].op2);
printf("%c",arr[index].r);
printf("\n");
index++;
return t;
}
int yywrap()
{
return 1;
}

int main(){
printf("Enter the expression: ");
yyparse();
return 0;
}

C.B.Thaokar / P.J.Assudani
Course Coordinators

/* Recursive Parser */
Eg : S -> a Ab | bb
A -> bA| c

W= aabb

S(){ A( ) { main() {
if ( ch==‘a’) { if ( ch= = ‘a’ ) { if ( S ( ) != error)
advance(); advance(); printf(“ Accepted string “);
if ( A( ) ! = error) if ( ch= =‘b’) else
{ advance(); printf(“ fail to accept”);
if ( ch = = ‘b’ ) { } }
advance (); else
if ( ch = = $) return(error);
return( success);
else
return ( error);
}
else
return( error); } }
else
return (error);
}

def identify_basic_blocks(three_address_code):
basic_blocks = [ ]
current_block = [ ]

for line in three_address_code:


if line.startswith('LABEL'):
if current_block:
basic_blocks.append(current_block)
current_block = [line]
else:
current_block.append(line)

if current_block:
basic_blocks.append(current_block)

return basic_blocks

# Example usage:
three_address_code = [
'a = b + c',
'LABEL L2:',
' if a< b goto LABEL L1',
'c = t1',
'LABEL L1:',
't2 = c * d',
' goto LABEL L2',
'f = e - c'
]

basic_blocks = identify_basic_blocks(three_address_code)
for block in basic_blocks:
print("Basic Block:")
for line in block:
print(line)
print()

-------------------------------------------------------------
Output :
Basic Block:
a = b + c

Basic Block:
LABEL L2:
if a< b goto LABEL L1
c = t1

Basic Block:
LABEL L1:
t2 = c * d
goto LABEL L2
f = e - c

You might also like