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

Cs6109 - Compiler Design: Lab Assignment

This document contains 6 lab assignments for a compiler design course. It includes Lex programs to: 1) Count the number of words in a text 2) Count the number of lines, spaces, and tabs 3) Remove multiple spaces, lines, and tabs from a file 4) Add line numbers to a given file 5) Count the total number of tokens 6) Identify valid and invalid identifiers Each assignment provides the code for the Lex program and sample input/output.

Uploaded by

vezhaventhan
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)
78 views8 pages

Cs6109 - Compiler Design: Lab Assignment

This document contains 6 lab assignments for a compiler design course. It includes Lex programs to: 1) Count the number of words in a text 2) Count the number of lines, spaces, and tabs 3) Remove multiple spaces, lines, and tabs from a file 4) Add line numbers to a given file 5) Count the total number of tokens 6) Identify valid and invalid identifiers Each assignment provides the code for the Lex program and sample input/output.

Uploaded by

vezhaventhan
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/ 8

CS6109 – COMPILER DESIGN

LAB ASSIGNMENT

Vezha Venthan T
2019503570

1) Lex Program to count number of words.

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

%%
([a-zA-Z0-9])* {i++;} /

"\n" {printf("%d\n", i); i = 0;}


%%

int yywrap(void){}

int main()
{

yylex();

return 0;
}
OUTPUT:

2) Lex program to count the number of lines, spaces and tabs.

CODE:
%{
#include<stdio.h>
int lc=0, sc=0, tc=0, ch=0; /*Global variables*/
%}

/*Rule Section*/
%%
\n lc++; //line counter
([ ])+ sc++; //space counter
\t tc++; //tab counter
. ch++; //characters counter
%%

int main()
{
// The function that starts the analysis
yylex();

printf("\nNo. of lines=%d", lc);


printf("\nNo. of spaces=%d", sc);
printf("\nNo. of tabs=%d", tc);
printf("\nNo. of other characters=%d", ch);

}
OUTPUT:

3) Lex program to take input from file and remove multiple spaces, lines
and tabs.

CODE:
%%
[ \n\t]+ {fprintf(yyout, "");}
. { fprintf(yyout, "%s", yytext); }
%%

int yywrap(){}

// driver code
int main()
{

/* yyin and yyout as pointer


of File type */
extern FILE *yyin, *yyout;

/* yyin points to the file input.txt


and opens it in read mode*/
yyin = fopen("Input.txt", "r");

/* yyout points to the file output.txt


and opens it in write mode*/
yyout = fopen("Output.txt", "w");
yylex();
return 0;
}

INPUT:

OUTPUT:

4) LEX program to add line numbers to a given file.

CODE:
%{
int line_number = 1; // initializing line number to 1
%}

/* simple name definitions to simplify


the scanner specification name
definition of line*/
line .*\n
%%
{line} { printf("%10d %s", line_number++, yytext); }

/* whenever a line is encountered increment count*/

/* 10 specifies the padding from left side to


present the line numbers*/

/* yytext The text of the matched pattern is stored


in this variable (char*)*/
%%

int yywrap(){}

int main(int argc, char*argv[])


{
extern FILE *yyin; // yyin as pointer of File type

yyin = fopen("in.c","r"); /* yyin points to the file


testtest.c and opens it
in read mode.*/

yylex(); // The function that starts the analysis.

return 0;
}

Input file:
#include <stdio.h>
int main() {
printf("Hello, World!");
printf("This is vezha");
printf("reg.no 2019503570");
return 0;
}
OUTPUT:

5) Lex code to count total number of tokens.

CODE:
%{
int n = 0 ;
%}
%%

"while"|"if"|"else" {n++;printf("\t keywords : %s", yytext);}

"int"|"float" {n++;printf("\t keywords : %s", yytext);}

[a-zA-Z_][a-zA-Z0-9_]* {n++;printf("\t identifier : %s", yytext);}

"<="|"=="|"="|"++"|"-"|"*"|"+" {n++;printf("\t operator : %s", yytext);}

[(){}|, ;] {n++;printf("\t separator : %s", yytext);}

[0-9]*"."[0-9]+ {n++;printf("\t float : %s", yytext);}


[0-9]+ {n++;printf("\t integer : %s", yytext);}

. ;
%%

int main()

yylex();

printf("\n total no. of token = %d\n", n);

OUTPUT:

6) Lex program to identify the identifier.

CODE:
%{
#include <stdio.h>
%}
%%
^[a-zA-Z_][a-zA-Z0-9_]* {printf("Valid Identifier");}
^[^A-Za-z_][0-9A-za-z] {printf("Invalid Identifier");}
.;
%%
int yywrap(void)
{
return 1;
}
void main()
{
printf("\nEnter any input :");
yylex();
}

OUTPUT:

You might also like