0% found this document useful (0 votes)
39 views

Compiler Design

Compiler design practical’s

Uploaded by

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

Compiler Design

Compiler design practical’s

Uploaded by

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

COMPILER DESIGN LAB

Introduction of Lexical Analysis:


Lexical Analysis is the first phase of the compiler also known as a scanner. It converts the High level
input program into a sequence of Tokens.

1. Lexical Analysis can be implemented with the Deterministic finite Automata.


2.The output is a sequence of tokens that is sent to the parser for syntax analysis

LEX:
 Lex is a program that generates lexical analyzer. It is used with YACC parser generator.
 The lexical analyzer is a program that transforms an input stream into a sequence of tokens.
 It reads the input stream and produces the source code as output through implementing the lexical
analyzer in the C program.
The function of Lex is as follows:
 Firstly lexical analyzer creates a program lex.1 in the Lex language. Then Lex compiler runs the
lex.1 program and produces a C program lex.yy.c.
 Finally C compiler runs the lex.yy.c program and produces an object program a.out.
 a.out is lexical analyzer that transforms an input stream into a sequence of tokens.

Vedant pant/210111792/Section-S/64
COMPILER DESIGN LAB

 LEX code using Regular Grammar (without file-handling):


1. Design a LEX Code to count the number of lines, space, tab-meta character and rest
of characters in a given Input pattern.
SOURCE CODE:
%{
#include<stdio.h>
#include<stdlib.h>
int line=0,space=0,tab=0,chare=0;
%}
%%
[" "] {space++;}
[\n] {line++;}
[\t] {tab++;}
[^\t" "\n] {chare++;}
%%
int
main(void){
yylex();
printf("Number of lines are:: %d\n",line);
printf("Number of spaces are:: %d\n",space);
printf("Number of tab character are:: %d\n", tab);
printf("Number of rest character are:: %d\n", chare);
}
int
yywrap(){r
eturn 1;
}
OUTPUT:

Vedant pant/210111792/Section-S/64
COMPILER DESIGN LAB

2. 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-Z_0-9]* {printf("Valid Identifier");}
^[^a-zA-Z_]{printf("Invalid Identifier");}
%%

int main()
{
printf("Enter the identifier :");
yylex();
}
int yywrap()
{
return 1;
}
OUTPUT:

Vedant pant/210111792/Section-S/64
COMPILER DESIGN LAB
3. Design a LEX Code to identify and print integer and float value in given Input
pattern.
SOURCE CODE;
%{
#include<stdio.h>
%}

%%
[0-9]+ {printf("Integer Number");}
[0-9]*.[0-9]+ {printf("Float Number");}
.* {printf("Invalid Number");}
%%

int main(void)
{
printf("Enter the number");
yylex();
}
int
yywrap()
{return 1;
}
OUTPUT:

Vedant pant/210111792/Section-S/64
COMPILER DESIGN LAB

4. Design a LEX Code for Tokenizing (Identify and print OPERATORS,


SEPERATORS, KEYWORDS, IDENTIFERS) the following C-fragment:
SOURCE CODE:
%{
#include<stdio.h>
%}

%%
while|for|if|else|for|int {printf("Keyword\n");}[(){},;]
{printf("Seperator\n");}
([a-zA-Z_])+|[a-zA-Z_0-9]* {printf("Identifier\n");}
[+-=*%] {printf("Operator\n");}
%%

int
main(){
yylex();
}
int
yywrap(){r
eturn 1;
}
OUTPUT:

Vedant pant/210111792/Section-S/64
COMPILER DESIGN LAB

 LEX code using Regular Grammar (with file-handling):


5. Design a LEX Code to count and print the number of total characters, words, white
spaces in given ‘Input.txt’ file.
SOURCE CODE:
%{
#include<stdio.h>
int sp=0,wd=0,ln=0,ch=0;
%}

%%
[\n] {ln++; ch+=yyleng;}
[\t" "] {sp++; ch+=yyleng;}
[^\n\t" "]+ {wd++; ch+=yyleng;}
%%

int
main(){ yyin=fopen("input.txt",
"r");yylex();
printf("\nTotal number of line:: %d",ln);
printf("\nTotal number of space:: %d",sp);
printf("\nTotal number of character:: %d",ch);
printf("\nTotal number of word:: %d\n",wd);
return 0;
}
int yywrap()
{
return 1;
}

Vedant pant/210111792/Section-S/64
COMPILER DESIGN LAB
OUTPUT:

Vedant pant/210111792/Section-S/64
COMPILER DESIGN LAB

6. Design a LEX Code to replace white spaces of ‘Input.txt’ file by a single blank
character into ‘Output.txt’ file.
SOURCE CODE:
%{
#include<stdio.h>
%}
%%
[\t" "]+ fprintf(yyout," ");
.|\n fprintf(yyout,"%s",yytext);
%%
int
yywrap()
{return 1;
}
int main(void)
{
yyin=fopen("input.txt","r");
yyout=fopen("output.txt","w");
yylex();
return 0;
}
INPUT:

OUTPUT:

Vedant pant/210111792/Section-S/64
COMPILER DESIGN LAB

7. Design a LEX Code to 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("i
p.c","r");
yyout=fopen("ot.c","w");
yylex();
return 0;
}
INPUT:

OUTPUT:

Vedant pant/210111792/Section-S/64
COMPILER DESIGN LAB
Ǫ8: 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 time.

%{
#include<stdio.h>
%}
%%
\<[^>]*\> fprintf(yyout,"%s\n",yytext);
.|\n;
%%
int yywrap()
{
return 1;
}
int main()
{
yyin=fopen("input.html","r");
yyout=fopen("out.txt","w");
yylex();
return 0;
}

INPUT:

Vedant pant/210111792/Section-S/64
COMPILER DESIGN LAB
OUTPUT:

Vedant pant/210111792/Section-S/64

You might also like