0% found this document useful (0 votes)
15 views5 pages

21070122188-CCL-Assignment 2

Jzjsjzjzndbdjdjd
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)
15 views5 pages

21070122188-CCL-Assignment 2

Jzjsjzjzndbdjdjd
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/ 5

No.

Papo No
EXPERIMENT:
Data

EXeERImENT 2

1 Explan the folowing


-Commond ine arquments in frogzam
File opcn/read/ kiite functions in C
Tokens
aLer file
2 hte a lex proqran to caunt the humber of
Comntnls, keawords, identifiers__ wordslines, and
Spaces The ihgut file Write a sanple
hgut /outgut

2omMAAND INE ARGUmEN TS TN A C PROGRAM


Command line arquments in are USed to
pass fayometers to arameat the time
executian These are passed to
the main function cagtwied sing
two poxa metes
"int axa C Num ber Of nput strinas
Stings
2: FILE OPEN/READ/ WATTE FUNCTPONS TN C
Ir C, fle handlih s done usina aset of
standard ay fnciicns
togen C) Opensa file and retuyns a fle
takes two arauments
he hame of the file
6)ode oh -rend, w-wnte a-apgend
Sundaras Teacher's Sign.
No
EXPERIMENT

FLE fp fopen flenone txt


flend)Reads dota trom a file Tt takes
touy arauments a pointer to butfer , si2e of
each element to Yednumber of elemernls to
ead and the filegoinl
fread Chuffer, Sizeof lchar)
fwrte O eites data to a filé Tt
h,fp) alse takes
four gvments similarto treadO
Closes on open file
Relase CHp),
3 T0ENS
n proaammirs na.oae a token Is the
Smalles element of a proram that s
mtaninetu to the (ampler.
Tokexs inclde
Keuords Recerved Wards lte int, Ycturn
Ldentifiess None ot vaales, functiers
Canstants Literal Values ike 423.14
Operntors- Saabals hke t,-,
Segerator Sumbels ke
I erical analusis, the phacess of Canvertig a
Seauenre Gf chosocters inio
tokenss called totenization

4 REGULAS EXrSESsToN TN A LEX FILE


Lex is a tool sel to aeneyate lexical onalyzes
Anttexns used tomatch
Ssiarm Taachar's Sign
Teachar'
Sign s
Gunaran)
entifier
\n' Lozintf
A-2
\n; CAungEA printfLa-2
-at Lo
irput the in
bata EXPERIMENT
No.
CODE:

%{
int line_count = 0, word_count = 0, space_count = 0, comment_count =0;
int identifier_count = 0;
int keyword_count = 0;
char *keywords[] = {
"int", "float", "double", "char", "void", "if", "else", "while", "for",
"return", "switch", "case", "break", "continue", "default", "typedef",
"struct", "union", "enum", "sizeof", "static", "extern", "const", "volatile",
"unsigned", "signed", "long", "short", "goto", "auto", "register", NULL
};

int is_keyword(const char *word) {


for (int i = 0; keywords[i] != NULL; i++) {
if (strcmp(word, keywords[i]) == 0) {
return 1;
}
}
return 0;
}
%}
%%
[/][*][^*]*[*]+([^*/][^*]*[*]+)*[/] {comment_count++;}
"//".* {comment_count++;}
[a-zA-Z_][a-zA-Z0-9_]* { word_count++;
if (is_keyword(yytext)) {
keyword_count++;
} else {
identifier_count++;
}
} /* Match words */
[\t ]+ { space_count ++; } /* Count spaces (including tabs) */
[\n] { line_count++; } /* Count lines */
. ;
%%

int main(int argc, char** argv){


FILE *fp = fopen(argv[1], "r");
yyin = fp;
yylex();
printf("Number of lines = %d\n", line_count);
printf("Number of words = %d\n", word_count);
printf("Number of spaces = %d\n", space_count);
printf("Number of comments = %d\n", comment_count);
printf("Number of identifiers = %d\n", identifier_count);
printf("Number of keywords = %d\n", keyword_count);
}
int yywrap() {
return 1; // Return 1 to indicate the end of the input stream
}

INPUT TEXT FILE


HI/
the name is ram
if
The password is
//prerna14*

OUTPUT

You might also like