0% found this document useful (0 votes)
13 views21 pages

CD Lab

The document is a practical file for Compiler Design at Gyan Ganga Institute of Technology and Sciences, detailing various programming experiments using Lex and Yacc. It includes objectives and code for tasks such as tokenization, word counting, email validation, and arithmetic expression parsing. Each experiment is structured with a program code section and an output terminal section.

Uploaded by

textque5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views21 pages

CD Lab

The document is a practical file for Compiler Design at Gyan Ganga Institute of Technology and Sciences, detailing various programming experiments using Lex and Yacc. It includes objectives and code for tasks such as tokenization, word counting, email validation, and arithmetic expression parsing. Each experiment is structured with a program code section and an output terminal section.

Uploaded by

textque5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

GYAN GANGA INSTITUTE OF TECHNOLOGY AND

SCIENCES, JABALPUR

PRACTICAL FILE

COMPILER DESIGN
(IS-702(B))

SESSION: 2023-24

SUBMITTED TO : Prof. Sourabh Jain

SUBMITTED BY :....................

ENROLLMENT NUMBER :............

1
COMPILER DESIGN CB-502

INDEX
Sr. No. Title Date Sign

01 Write a Lex program to implement a tokenizer

02 Write a Lex program to count number of words

03 Write a lex program to check email address validation

04 Write a Lex program to count number of Vowels and consonants

05 Write a lex program for reverse of a string

06 Write a Yacc program to create a calculator with arithmetic


expressions validation

07 Write a Yacc Program to Implement LL1 Parsing

08 Write a Yacc program to convert from binary to decimal

09 Write a Yacc program to check if the given string is a palindrome

10 Write a program to implement intermediate code generator


EXPERIMENT 01

● Objective: - To write a Lex program to implement a Tokenizer.

● Program code:-

File Name: token.l


%{
#include <stdio.h>
int yywrap(void) {
return 1;
}
%}
%%
[0-9]+ {printf("NUMBER %s\n", yytext);}
[a-zA-Z]+ {printf("WORD %s\n", yytext);}
[ \t]+ { /* Ignore whitespace */ }
. {printf("PUNCTUATION %s\n", yytext);}
%%
int main(int argc, char **argv)
{
printf("Enter string to tokenize: ");
yylex();
return 0;
}

Output Terminal: -
COMPILER DESIGN CB-502

EXPERIMENT 02

● Objective:- To write a Lex program to count number of words

● Program code:-

File Name: wordcount.l


%{
#include<stdio.h>
#include<string.h>
int i = 0;
%}

%%
([a-zA-Z0-9])* {i++;}
"\n" {printf(" Number of words: %d\n", i); i = 0;}
%%

int yywrap(void){}

int main()
{
printf("Enter the String: ");
yylex();
return 0;
}

● Output Terminal: -
EXPERIMENT 03

● Objective:- To write a lex program to check email address validation

● Program code:-

File Name: emailvalid.l

%{
#include<stdio.h>
%}
%option noyywrap
%%
^[a-z][a-z0-9_]*(@[A-Za-z]+)(\.[a-z]+)+ {printf("valid");}
.* {printf("invalid");}
%%
int main()
{
printf("Enter email:");
yylex();
return 0;
}

● Output Terminal:-
COMPILER DESIGN CB-502

EXPERIMENT 04

● Objective:- To write a Lex program to count number of Vowels and consonants


● Program code:-

File Name: noofvowel.l


%{
int num_vowels = 0;
int num_consonants = 0;
%}

%%
[aAeEiIoOuU] { num_vowels++; }
[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ] { num_consonants++; }
[ \t\n];
.;
%%

int yywrap() {
return 1;
}

int main() {
char input[1000];
printf("Enter a string: ");
fgets(input, sizeof(input), stdin);
yy_scan_string(input);
yylex();

printf("Number of vowels: %d\n", num_vowels);


printf("Number of consonants: %d\n", num_consonants);
return 0;
}
● Output Terminal:-
COMPILER DESIGN CB-502

EXPERIMENT 05

● Objective:- To write a lex program for reverse of a string


● Program code:-

File Name: reverse.l


%{
#include <stdio.h>
#include <string.h>
%}
% option noyywrap
%%
[a-zA-Z]+ {
int len = strlen(yytext);
printf("Reversed String:");
for (int i = len - 1; i >= 0; i--) {
printf("%c", yytext[i]);
}
}
\n {
printf("\n");
}
.;
%%
int main() {
printf("Enter string:");
yylex();
return 0;
}
● Output Terminal:-
COMPILER DESIGN CB-502

EXPERIMENT 06

● Objective:- To write a Yacc program to create a calculator with arithmetic


expressions validation
● Program code:-
File Name: calci.l
%{
#include<stdio.h>
#include "y.tab.h"
extern int yylval;

%}
%%
[0-9]+ {
yylval=atoi(yytext);
return NUMBER;
}
[\t] ;
[\n] return 0;
. return yytext[0];

%%
int yywrap()
{
return 1;
}
● Output Terminal: -
%{
#include<stdio.h>
int flag=0;
%}
%token NUMBER
%left '+' '-'
%left '*' '/' '%'
%left '(' ')'
%%
ArithmeticExpression: E{
printf("\nResult=%d\n", $$);
return 0;
};
E:E'+'E {$$=$1+$3;}
|E'-'E {$$=$1-$3;}
|E'*'E {$$=$1*$3;}
|E'/'E {$$=$1/$3;}
|E'%'E {$$=$1%$3;}
|'('E')' {$$=$2;}
|NUMBER {$$=$1;};
%%
void main()
{
printf("\nEnter Any Arithmetic Expression which can have operations Addition,
Subtraction, Multiplication, Division, Modulus and Round brackets:\n");
yyparse();
if(flag==0)
printf("\nEntered arithmetic expression is Valid\n\n");
}
void yyerror()
{
printf("\nEntered arithmetic expression is Invalid\n\n");
flag=1;
}
COMPILER DESIGN CB-502

EXPERIMENT 07

● Objective:- To write a Yacc Program to Implement LL1 Parsing


● Program code:-

File Name: ll1.l


%{
#include "y.tab.h"
%}

%%
[0-9] { yylval = *yytext - '0'; return NUM; }
[+*()$] { return *yytext; }
[ \t] ;
\n { return *yytext; }
. { printf("Invalid character %c\n", *yytext); }
%%

int yywrap() {
return 1;
}

● Output Terminal:-
File Name: ll1.y

%{
#include <stdio.h>
int yylex();
void yyerror(char *);
int size[5][6];
char m[5][6][3];
char s[30], stack[20];
int top = 0;
%}
%token NUM
%%
main: expr '$' { printf("SUCCESS\n"); }
;
expr: term expr1 {;}
;
expr1: '+' term expr1 {;}
|{;}
;
term: factor term1 {;}
;
term1: '*' factor term1 {;}
|{;}
;
factor: '(' expr ')' {;}
|NUM {;}
;
%%
void yyerror(char *s) {
}
int main() {
printf("Enter the input string: ");
yyparse();
return 0;
}
COMPILER DESIGN CB-502

EXPERIMENT 08

● Objective:- To write a Yacc program to convert from binary to decimal

● Program code:-

File Name: unitconvert.l


%{
#include<stdlib.h>
#include "y.tab.h"
%}

%%
"." { return yytext[0]; }
"0" { yylval.n=0; return ZERO; }
"1" { yylval.n=1; return ONE; }
\n { return 0; }
. {;}
%%

int yywrap(){
return 1;
}
File Name: unitconvert.y
%{
#include<stdio.h>
void yyerror(char *);
int yylex();
int m1 = 0;
int m2 = 1;
%}
%token ONE
%token ZERO
%union {
float f;
int n;
}
%type <f>E
%type <n>A
%type <f>B
%type <n>ONE
%type <n>ZERO
%%
S: E {printf("ans: %f\n", $1);};
E: A {$$=$1;}
|A'.'B {
$$=$1+$3;
printf("decimal part: %d\n", $1);
printf("fractional part: %f\n", $3);
};
A: {$$ = 0;}
| ONE A { $$ =(1 << m1 ) | $2; m1++; };
| ZERO A { $$ = $2; m1++;};
;
B: {$$ = 0;}
| B ONE { $$ = $1 +1.0/(1 << m2); m2++;}
| B ZERO { $$=$1 ; m2++;}
;
%%
void yyerror(char *s){
printf("ERROR %s .\n", s); }
int main(){
printf("Enter binary number: ");
return yyparse();
}
COMPILER DESIGN CB-502

Output Terminal:-
EXPERIMENT 09

● Objective:-To write a Yacc program to check if the given string is a palindrome

● Program code:-

File Name: palindrome.l


%{
#include <stdio.h
#include <stdlib.h>
#include "y.tab.h"
%}
%%
[a-zA-Z]+ {yylval.f = yytext; return STR;}
[-+()*/] {return yytext[0];}
[ \t\n] {;}
%%
int yywrap()
{
return -1;
}
COMPILER DESIGN CB-502

File Name: palindrome.y


%{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
extern int yylex();
void yyerror(char *msg);
int flag;
int i;
int k =0;
%}
%union {
char* f;
}
%token <f> STR
%type <f> E
%%
S:E{
flag = 0;
k = strlen($1) - 1;
if(k%2==0){
for (i = 0; i <= k/2; i++) {
if ($1[i] == $1[k-i]) {
} else {
flag = 1;
}
}
if (flag == 1) printf("Not palindrome\n");
else printf("palindrome\n");
printf("%s\n", $1);
}else{
for (i = 0; i < k/2; i++) {
void yyerror(char *msg)
{
fprintf(stderr, "%s\n", msg);
exit(1);
}
int main()
{
printf("Enter string:");
yyparse();
return 0;
}

● Output Terminal:-
COMPILER DESIGN CB-502

EXPERIMENT 10

● Objective:- To write a program to implement intermediate code generator (Three


address code )
● Program code:-

File Name: icg.l


%{
#include <stdio.h>
#include <string.h>
%}
%option noyywrap
%%
[a-zA-Z]+ {
int len = strlen(yytext);
printf("Reversed String:");
for (int i = len - 1; i >= 0; i--) {
printf("%c", yytext[i]);
}
}
\n {
printf("\n");
}
.;
%%
int main() {
printf("Enter string:");
yylex();
return 0;
}
Output:-

You might also like