-
Notifications
You must be signed in to change notification settings - Fork 0
/
oprCount.l
64 lines (57 loc) · 961 Bytes
/
oprCount.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
%{
/* Program to check arithmetic expression and count operator if valid */
#include <stdio.h>
#include <stdlib.h>
int pc = 0;
int sc = 0;
int mc = 0;
int dc = 0;
int valid = 0;
int i;
%}
oprnd [a-zA-Z0-9]
opr [+*/-]
%%
{oprnd}({opr}{oprnd})+[\n] {
valid = 1;
for(i = 0; yytext[i] != '\0'; i++)
{
switch(yytext[i])
{
case '+':
pc++;
break;
case '-':
sc++;
break;
case '*':
mc++;
break;
case '/':
dc++;
break;
}
}
return 0;
}
^{oprnd}[\n] {
printf("\nOnly operand\n");
exit(0);
}
%%
int main()
{
printf("Enter expression to check: ");
yylex();
if(valid = 1)
{
printf("\nEnter expression is valid arithmetic expresion\n");
printf("+ = %d\n- = %d\n* = %d\n / = %d\n", pc, sc, mc, dc);
}
else
{
printf("\nInvalid arithmetic expression\n");
exit(1);
}
return 0;
}