Aditya Panwar Compiler File
Aditya Panwar Compiler File
Aditya Panwar Compiler File
On
COMPILER DESIGN
(PCS-601)
2023-24
Submitted to Submitted by :
MR. MUKESH KUMAR Aditya Panwar
Rollno-03 Sec-I
Student id-20011077
And last but not the least I am greatly indebted to all other persons
Devansh Thapliyal
University. Roll No.- 2018315
B. Tech CSE-I-VI Sem
Session: 2023-24
GEHU, Dehradun
INDEX
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
%%
<A>b BEGIN B;
<B>a BEGIN C;
<B>b BEGIN A;
<C>a BEGIN B;
%%
int main(){
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");}
%%
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");}
%%
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"
%}
%%
[ \t] ;
. return yytext[0];
%%
int yywrap()
return 1;
}
YACC:
%{
#include <stdio.h>
#include "lex.yy.c"
%}
%token num id
%%
E:E'+'E
|E'-'E
|E'*'E
|E'/'E
|E'%'E
|'('E')'
|num
|id
%%
int main()
{
printf("Enter the expression: ");
yyparse();
return 0;
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"
%}
%%
[ \t] ;
. return yytext[0];
%%
int yywrap()
return 1;
}
YACC:
%{
#include <stdio.h>
#include "lex.yy.c"
%}
%token num
%left '('')'
%%
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()
return 0;
Output:
14. Design a YACC/LEX code that translates infix expression to
postfix expression.
Program:
LEX:
%{
#include <stdio.h>
#include "14.tab.h"
%}
%%
[ \t] ;
. return yytext[0];
%%
int yywrap()
return 1;
}
YACC:
%{
#include<stdio.h>
#include "lex.yy.c"
%}
%token num id
%%
S: E {printf("\n");}
E: E '+' E {printf("+");}
| E '*' E {printf("*");} |
E '-' E {printf("-");}
| E '/' E {printf("/");}
| '(' E ')'
| id {printf("%c", yylval);}
%%
int main()
return 0;
Output:
15. Design Desk Calculator using YACC/LEX code.
Program:
LEX:
%{
#include <stdio.h>
#include "13.tab.h"
%}
%%
[ \t] ;
. return yytext[0];
%%
int yywrap()
return 1;
}
YACC:
%{
#include <stdio.h>
#include "lex.yy.c"
%}
%token num
%left '('')'
%%
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()
return 0;
Output: