-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalc.lex
103 lines (97 loc) · 1.3 KB
/
calc.lex
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
98
99
100
101
102
103
%{
#include <stdlib.h>
#include <stdio.h>
#include "y.tab.h"
extern YYSTYPE yylval;
void yyerror(char*);
%}
%%
"+" {
printf("T_ADD ");
return OP_ADD;
}
"-" {
printf("T_SUB ");
return OP_SUB;
}
"*" {
printf("T_MUL ");
return OP_MUL;
}
"/" {
printf("T_DIV ");
return OP_DIV;
}
"^" {
printf("T_POW ");
return OP_POW;
}
"=" {
printf("T_EQL ");
return OP_EQL;
}
"(" {
printf("T_PRNL ");
return SYM_PRNL;
}
")" {
printf("T_PRNR ");
return SYM_PRNR;
}
"sin"|"SIN" {
printf("T_SIN ");
return FUNC_SIN;
}
"cos"|"COS" {
printf("T_COS ");
return FUNC_COS;
}
"sqrt"|"SQRT" {
printf("T_SQRT ");
return FUNC_SQRT;
}
"exp"|"EXP" {
printf("T_EXP ");
return FUNC_EXP;
}
"cosh"|"COSH" {
printf("T_COSH ");
return FUNC_COSH;
}
"sinh"|"SINH" {
printf("T_SINH ");
return FUNC_SINH;
}
"abs"|"fabs"|"ABS"|"FABS" {
printf("T_ABS ");
return FUNC_ABS;
}
[0-9]+ {
printf("T_INT ");
sscanf(yytext, "%lf", &yylval);
return T_INT;
}
[0-9]+(\.?[0-9]*e?-?[0-9]*)? {
sscanf(yytext, "%lf", &yylval);
printf("T_FLOAT ");
return T_FLOAT;
}
[a-zA-Z]([a-zA-Z]|[0-9])* {
printf("T_IDEN ");
return T_IDEN;
}
\n {
return EOL;
}
[ \t]+ {}
"quit"|"exit" {
return CMD_EXT;
}
. {
yyerror("ERROR: Unrecognized input!");
}
%%
int yywrap()
{
return 1;
}