You are on page 1of 6

Experiment Title.

Write a program to count number of words, characters,


spaces and lines in a given input file.

Student Name: Divanshi UID: 18bcs4016


Branch: CC-1 Section/Group: A
Semester: 5 Date of Performance: 13/08/2020
Subject Name: SP Lab Subject Code: CSP-340

1. Overview of 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

2. Tasks to be done:

 Count Number of Characters

 Count number of Words

 Count number of Spaces

 Count number of Lines

3. Steps for practical: (Mention the steps for each and every task)

 Make a C program file or text file.


 In Lex editor, write the logic to identify characters, words, spaces etc.
 Run lex.yy.c file in Dev C++.

Department of Computer Science and Engineering, AIT


4. Screenshots:

C File

Code

Department of Computer Science and Engineering, AIT


Output

Department of Computer Science and Engineering, AIT


5. Commands used:

Department of Computer Science and Engineering, AIT


%{

#include<stdio.h>

int word=0,space=0,line=0,chr=0;

%}

%%

([a-zA-Z0-9])* {word++;}

" " {space++;}

"\n" {line++;}

. {chr++;}

%%

main()

yyin=fopen("myfile.c","r");

yylex();

printf("Words: %d \n",word);

printf("White space: %d \n",space);

printf("Lines: %d \n",line);

printf("Characters %d \n",chr);

int yywrap()

return 1;

6. Result/Output/Writing Summary:
Department of Computer Science and Engineering, AIT
We successfully counted the number of Characters, white spaces and lines in a C
program file using Lex tool.

Learning outcomes (What I have learnt):

Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks


1.
2.
3.

Department of Computer Science and Engineering, AIT

You might also like