-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc1.l
More file actions
97 lines (48 loc) · 1.29 KB
/
calc1.l
File metadata and controls
97 lines (48 loc) · 1.29 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
%{
#include <string.h>
#define st_size 200
static int count;
static float stack [st_size];
static void push (float x) {
if (++count<st_size)
stack[count]= x;
else
{printf ("STACK FULL\n");
exit(1);}
}
static float pop (void)
{
if (count>=0)
return stack[count--];
else {
printf ("STACK EMPTY\n");
exit(1);}
}
int int_v;
%}
digit [0-9]
int {digit}+
real ({digit}+[.]{digit}*)|({digit}*[.]{digit}+)
exp ({int}|{real})[eE]{int}
exp2 ({int}|{real})[eE][+-]{int}
exp3 [+-]({int}|{real})[eE][+-]{int}
int2 [+-]({int}|{real})
%%
({int}|{int2}|{real}|{exp}|{exp2}|{exp3}) {push (atof(yytext));}
"+" {push (pop() + pop());}
"*" {push (pop() * pop());}
"-" {float f= pop(); push (pop() - f);}
"/" {float f= pop(); push (pop() / f);}
\n {float answer = pop();
int k=answer;
if(k==answer)
{printf("%d\n",k);}
else
{printf("%f\n",answer);}}
[ \t\n] ;
[^-0-9+*/.;eE \t\n]+ {ECHO; fprintf (stderr,"Invalid Character: ");
printf("\n");
exit(1);}
%%
int main (void) {count= -1; yylex(); return 0;}
int yywrap (void) {return 1;}