Aditya Panwar Compiler File

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

Term Work

On
COMPILER DESIGN

(PCS-601)
2023-24

Submitted to Submitted by :
MR. MUKESH KUMAR Aditya Panwar
Rollno-03 Sec-I
Student id-20011077

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

GRAPHIC ERA HILL UNIVERSITY, DEHRADUN


ACKNOWLEDGMENT
I would like to particularly thank my Compiler Design Lab

Faculty Mr. Mukesh Kumar for his patience, support, and

encouragement throughout the completion of this Term work.

And last but not the least I am greatly indebted to all other persons

who directly or indirectly helped me during this course.

Devansh Thapliyal
University. Roll No.- 2018315
B. Tech CSE-I-VI Sem
Session: 2023-24
GEHU, Dehradun
INDEX

S.no PROGRAM NAME Date Page Sign.


no.

1. Design a LEX Code to count the number of lines, space, tab-


meta character and rest of characters in a given Input pattern.
2. Design a LEX Code to identify and print valid Identifier of
C/C++ in given Input pattern.
3. Design a LEX Code to identify and print integer and float
value in given Input pattern.
4. Design a LEX Code for Tokenizing (Identify and print
OPERATORS, SEPERATORS, KEYWORDS,
IDENTIFERS)
Design a LEX Code to count and print the number of total
5. characters, words, white spaces in given 'Input.txt' file.
Design a LEX Code to replace white spaces of 'Input.txt' file
6. by a single blank character into 'Output.txt' file.
Design a LEX Code to remove the comments from any C-
7. Program given at run-time and store into 'out.c' file.
Design a LEX Code to extract all html tags in the given
HTML file at run time and store into Text file given at run
8.time.
Design a DFA in LEX Code which accepts string containing
9. even number of 'a' and even number of 'b' over input alphabet
(a, b).
Design a DFA in LEX Code which accepts string containing
10.third last element 'a' over input alphabet (a, b).
Design a DFA in LEX Code to Identify and print Integer &
11.Float Constants and Identifier. YACC/LEX code:
Design YACC/LEX code to recognize valid arithmetic
12.expression with operators +, -, * and /.
Design YACC/LEX code to evaluate arithmetic expression
13.involving operators +, -, * and / without operator precedence
grammar & with operator precedence grammar.
14. Design YACC/LEX code that translates infix expression to
postfix expression.
15. Design Desk Calculator using YACC/LEX Code.
Practical 1

Objective : count the number of lines, space, tab-meta character, and rest of
the characters in a given input pattern.
Source Code:
%{
#include<stdio.h>
#include<string.h>
int count=0,space=0,tcount=0,rcount=0;
%}
%%
\n count++;
" " space++;
\t tcount++; [^\t"
"\n] rcount++;
end return 0;
%%
int yywrap(){return
1;} int main() {
printf("Enter the input:\n"); yylex();
printf("Number of lines: %d\n",count);
printf("Number of spaces: %d\n",space);
printf("Number of meta spaces:
%d\n",tcount); printf("Number of characters:
%d\n",rcount); return 0;
}

Output:
Practical-02
Objective: Design a LEX Code to identify and print valid identifier of C/C++
in given input pattern Source Code:
%{
#include<stdio.h>
%}
%%
^[a-zA-Z_][a-zA-Z0-9_]* {printf("%s\n",yytext);}
. {;}
%%
int yywrap()
{ return
1; }
int main(void)
{
printf("Enter the
input:\n"); yylex(); return
0;
}

Output:
Practical-03
Objective : identify and print integer and float value in given input pattern
Source Code:
%{
#include<stdio.h>
#include<string.h>
%}
%%
[0-9]+"."+[0-9]+ {printf("%s :float\n",yytext);}
[0-9]+ {printf("%s :integer\n",yytext);}
. {;}
%%
int
yywrap(){
return 1;} int
main() {
printf("Enter the
input:\n"); yylex(); return
0;
}

Output:
Practical-04
Objective: Design a LEX Code for Tokenizing (Identify and print
OPERATORS, SEPERATORS, KEYWORDS, IDENTIFIERS) the C-
Fragment
Source Code:
%{
#include<stdio.h>
%}
%%
auto|double|int|struct|break|else|long|switch|case|enum|register|typedef|char|extern|return|unio
n|continue|for|signed|void|do|if|static|while|default|goto|sizeof|volatile|const|float|short
{ECHO;printf("\tKEYWORD\n");}
[{};,()] {ECHO; printf("\tSEPERATOR\n");}
[+-/=*%] {ECHO; printf("\tOPERATOR\n");}
^[a-zA-Z_]+[0-9]*[a-zA-Z_]* {ECHO; printf("\tIdentifier\n");}
.|\n ;
%%
int
yywrap(void){}
int main() {
printf("Enter the
input:\n"); yylex(); return
0;
}

Output:
Practical-05
Objective: Count and print the total characters, words, white spaces in given
‘input.txt’ file
Source Code:
%{
#include<stdio.h> int
ch=0,words=0,white=0;
%}
%%
" " {white++; words++;}
[\t\n] {words++;} [^\n\t]
{ch++;}
%%
int
yywrap(void){}
int main() {
yyin=fopen("input5.txt","r"); yylex();
printf("Number of characters: %d\n",ch);
printf("Number of words: %d\n",words);
printf("Number of whitespaces:
%d\n",white); return 0;
}

Input:

Output:
Practical-06
Objective : Code replace white spaces of ‘input.txt’ file by a single blank
character into ‘output.txt’ file
Source Code:
%{
#include<stdio.h>
#include<string.h>
%}
%%
[\t" "]+ fprintf(yyout," ");
.|\n fprintf(yyout,"%s",yytext);
%%
int yywrap(void){}
int main()
{ yyin=fopen("input6.txt","r");
yyout=fopen("output6.txt","w")
;
yylex();
return 0;
}

Input:

Output:
Practical-07
Objective : Remove the comments from any C-Program given at run time and
store into ‘out.c’ file
Source Code:
%{
#include<stdio.h>
%}
%%
\ / \ /(.*) {};
\ / \*(.*\n)*.*\*\ / {};
%%
int yywrap()
{ return 1; } int main() {
yyin=fopen("input7.c","r");
yyout=fopen("output7.c","w")
;
yylex();
return 0;
}

Input:

Output:
Open /+l output7-/Desktop.c

1
2 #include
3 int Main()
4 {
5
6 int a,b;
7 int c;
8
9 c=a+b;
10 return 0;
11 }
Practical-08
Objective : Code to extract all HTML tags in the given HTML file at
run time and store into text file given at run time.
Source Code:
%{
#include<stdio.h>
%}
%%
\<[^>]*\> fprintf(yyout,"%s\n",yytext);
.|\n;
%%
int yywrap()
{ return 1; } int main() {
yyin=fopen("input8.html","r");
yyout=fopen("output8.txt","w")
;
yylex();
return 0;
}

Input:

Output:
Open /+I

CTYPE htMl> 2
3 <htMl> 4
s <body>
6
7
8 <hl>
9 </hl>
10
11
12 <p>
13 </p>
14
15
16</body>
17
18 </htMl>
9. Design a DFA in LEX code which accepts string containing even
number of ‘a’ and even number of ‘b’ over input alphabet {a,b}.

Program:
%{

#include<stdio.h>

%}

%s A B C DEAD

%%

<INITIAL>a BEGIN A; /*BEGIN separates pattern from

action*/ <INITIAL>b BEGIN C;

<INITIAL>[^ab\n] BEGIN DEAD; {printf("invalid string and now IN DEAD STATE\n");}

<INITIAL>\n BEGIN INITIAL; {printf("ACCEPTED\n");} //taliking about epsilon here

<A>a BEGIN INITIAL;

<A>b BEGIN B;

<A>[^ab] BEGIN DEAD; {printf("invalid string and now IN DEAD STATE\n");}

<B>a BEGIN C;

<B>b BEGIN A;

<B>[^ab] BEGIN DEAD; {printf("invalid string and now IN DEAD STATE\n");}

<C>a BEGIN B;

<C>b BEGIN INITIAL;

<C>[^ab] BEGIN DEAD; {printf("invalid string and now IN DEAD STATE\n");}


<DEAD>a BEGIN DEAD;

<DEAD>b BEGIN DEAD;

<DEAD>[^ab] BEGIN DEAD; {printf("invalid string and now IN DEAD STATE\n");}

%%

int main(){

printf("Enter string: ");

yylex();

return 0;

int yywrap(){

return 1;

Output:
10. Design a DFA in LEX code which accepts string containing third last
element ‘a’ over input alphabet {a,b}.

Program:
%{
#include<stdio.h>
%}
%s A B C D E F G DEAD
%%
<INITIAL>b BEGIN INITIAL;
<INITIAL>a BEGIN A;
<INITIAL>[^ab\n] BEGIN DEAD;
<INITIAL>\n BEGIN INITIAL; {printf("Not Accepted\n");}

<A>b BEGIN F;
<A>a BEGIN B;
<A>[^ab\n] BEGIN DEAD;
<A>\n BEGIN INITIAL; {printf("Not Accepted\n");}

<B>b BEGIN D;
<B>a BEGIN C;
<B>[^ab\n] BEGIN DEAD;
<B>\n BEGIN INITIAL; {printf("Not Accepted\n");}

<C>b BEGIN D;
<C>a BEGIN C;
<C>[^ab\n] BEGIN DEAD;
<C>\n BEGIN INITIAL; {printf("Accepted\n");}

<D>b BEGIN G;
<D>a BEGIN E;
<D>[^ab\n] BEGIN DEAD;
<D>\n BEGIN INITIAL; {printf("Accepted\n");}

<E>b BEGIN F;
<E>a BEGIN B;
<E>[^ab\n] BEGIN DEAD;
<E>\n BEGIN INITIAL; {printf("Accepted\n");}

<F>b BEGIN G;
<F>a BEGIN E;
<F>[^ab\n] BEGIN DEAD;
<F>\n BEGIN INITIAL; {printf("Not Accepted\n");}

<G>b BEGIN INITIAL;


<G>a BEGIN A;
<G>[^ab\n] BEGIN DEAD;
<G>\n BEGIN INITIAL; {printf("Accepted\n");}

<DEAD>[^\n] BEGIN DEAD;


<DEAD>\n BEGIN INITIAL; {printf("Invalid\n");}

%%
int yywrap()
{
return 1;
}
int main()
{
printf("Enter String\n");
yylex();
return 0;
}

Output:
11. Design a DFA in LEX Code to identify and print integer and
float constants and identifier.

Program:
%{
%}
%s A B C DEAD
%%
<INITIAL>[0-9]+ BEGIN A; <INITIAL>[0-
9]+[.][0-9]+ BEGIN B; <INITIAL>[A-Za-
z_][A-Za-z0-9_]* BEGIN C; <INITIAL>[^\n]
BEGIN DEAD;
<INITIAL>\n BEGIN INITIAL; {printf("Not Accepted\n");}

<A>[^\n] BEGIN DEAD;


<A>\n BEGIN INITIAL; {printf("Integer\n");}

<B>[^\n] BEGIN DEAD;


<B>\n BEGIN INITIAL; {printf("Float\n");}

<C>[^\n] BEGIN DEAD;


<C>\n BEGIN INITIAL; {printf("Identifier\n");}

<DEAD>[^\n] BEGIN DEAD;


<DEAD>\n BEGIN INITIAL; {printf("Invalid\n");}

%%
int yywrap()
{
return 1;
}

int main()
{
printf("Enter String\n");
yylex();
return 0;
}
Output:
12. Design a YACC / LEX Code to recognize valid arithmetic
expression with operators +,-,* and /.

Program:

Lex:

%{

#include<stdio.h>

#include "12.tab.h"

%}

%%

[a-zA-Z]+ {return id;}

[0-9]+ {return num;}

[-+\/*=] {return yytext[0];}

[ \t] ;

[\n] {return 0;}

. return yytext[0];

%%

int yywrap()

return 1;

}
YACC:

%{

#include <stdio.h>

#include "lex.yy.c"

void yyerror(char *s);

%}

%token num id

%left '+' '-'

%left '*' '/' '%'

%%

S:E {printf("Valid arithmetic expression\n"); return 0; }

|E'='E {printf("Valid arithmetic expression\n"); return 0; }

E:E'+'E

|E'-'E

|E'*'E

|E'/'E

|E'%'E

|'('E')'

|num

|id

%%

int main()

{
printf("Enter the expression: ");

yyparse();

return 0;

void yyerror(char *s)

printf("Error: %s\n", s);

Output:
13. Design YACC/LEX code to evaluate arithmetic expression involving
operators +,-,*,/ without and with operator precedence grammar.

Program:

LEX:

%{

#include <stdio.h>

#include "13.tab.h"

extern int yylval;

%}

%%

[0-9]+ {yylval = atoi(yytext); return num;}

[-+\/*] {return yytext[0];}

[ \t] ;

[\n] {return 0;}

. return yytext[0];

%%

int yywrap()

return 1;

}
YACC:

%{

#include <stdio.h>

#include "lex.yy.c"

void yyerror(char *s);

%}

%token num

%left '+' '-'

%left '*' '/' '%'

%left '('')'

%%

S:E {printf("Result: %d\n", $1); 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;}

| num { $$ = $1; }

%%

int main()

printf("Enter the arithmetic expression: ");


yyparse();

return 0;

void yyerror(char *s)

printf("Error: %s\n", s);

Output:
14. Design a YACC/LEX code that translates infix expression to
postfix expression.

Program:

LEX:

%{

#include <stdio.h>

#include "14.tab.h"

extern int yylval;

%}

%%

[a-zA-Z]+ { yylval=*yytext; return id; }

[0-9]+ {yylval = atoi(yytext); return num;}

[-+\/*()] {return (int)yytext[0];}

[ \t] ;

[\n] {return 0;}

. return yytext[0];

%%

int yywrap()

return 1;

}
YACC:

%{

#include<stdio.h>

#include "lex.yy.c"

void yyerror(char *s);

%}

%token num id

%left '+' '-'

%left '*' '/'

%%

S: E {printf("\n");}

E: E '+' E {printf("+");}

| E '*' E {printf("*");} |

E '-' E {printf("-");}

| E '/' E {printf("/");}

| '(' E ')'

| num {printf("%d", yylval);}

| id {printf("%c", yylval);}

%%

int main()

printf("Enter the arithmetic expression: ");


yyparse();

return 0;

void yyerror(char *s)

printf("Error: %s\n", s);

Output:
15. Design Desk Calculator using YACC/LEX code.

Program:

LEX:

%{

#include <stdio.h>

#include "13.tab.h"

extern int yylval;

%}

%%

[0-9]+ {yylval = atoi(yytext); return num;}

[-+\/*] {return yytext[0];}

[ \t] ;

[\n] {return 0;}

. return yytext[0];

%%

int yywrap()

return 1;

}
YACC:

%{

#include <stdio.h>

#include "lex.yy.c"

void yyerror(char *s);

%}

%token num

%left '+' '-'

%left '*' '/' '%'

%left '('')'

%%

S:E {printf("Result: %d\n", $1); 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;}

| num { $$ = $1; }

%%

int main()

printf("Enter the arithmetic expression: ");


yyparse();

return 0;

void yyerror(char *s)

printf("Error: %s\n", s);

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy