-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprg6.l
64 lines (41 loc) · 1.17 KB
/
prg6.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Progam : Program to identify integer, float, Keyword, Operator, Single_line_comment, Multi_line_comment. .
Author : Lalit
*/
%{
// header file
#include<stdio.h>
// initializing the variables
int c=0, w=0, s=0, l=0;
%}
WORD [^ t\n,\.:]+
EOL [\n]
BLANK [" "]
%%
{WORD} {w++; c=c+yyleng;}
{BLANK} {s++;}
{EOL} {l++;}
. {c++;}
%%
// yywrap function
int yywrap()
{
return 1; // returning 1 so that after the analysis of the file stop analysis
}
// main function with cmd line argument for source code file
int main(int argc, char *argv[])
{
if(argc!=2) // checking if valid argument is passed or not , argc gives argument count
{
printf("Usage: <./a.out> <sourcefile>\n"); // message printing if wrong argument is passed
exit(0);
}
yyin=fopen(argv[1],"r"); // opening the file which is passed as argument
yylex();// yylex function doing lexical analysis
printf("No of characters=%d\nNo of words=%d\nNo of spaces=%d\n No of lines=%d\n",c+s,w,s,l);// printing output
}
/*
--------------------------------------------------
| Note: program is not treating space as character .Provide file name in cla|
--------------------------------------------------
*/