-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.l
64 lines (57 loc) · 1.3 KB
/
Calculator.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
%{
#include <stdio.h>
#include "y.tab.h"
#include "calculator.h"
#include "float.h"
#include "math.h"
#include <stdbool.h>
#include <string.h>
int yyparse();
%}
%option noyywrap
%%
"exit" { exit(0); }
\n { return EOLN; }
"cos" { return COS; }
"sin" { return SIN; }
"tan" { return TAN; }
"sec" { return SEC; }
"csc" { return CSC; }
"cot" { return COT; }
"e" { return E; }
"arcsin" { return ARCSIN; }
"arccos" { return ARCCOS; }
"arctan" { return ARCTAN; }
"arcsec" { return ARCSEC; }
"arccsc" { return ARCCSC; }
"arccot" { return ARCCOT; }
"ln" { return LN; }
"log" { return LOG; }
"sqrt" { return SQRT; }
\( { return LPARENTHESIS; }
\) { return RPARENTHESIS; }
\^ { return EXPONENT; }
\* { return MULTIPLY; }
\/ { return DIVIDE; }
\+ { return ADD; }
\- { return SUBTRACT; }
\= {
yylval.fun->equals = true;
return EQUALS;
}
[a-zA-Z][0-9]* {
yylval.fun->find = strdup(yytext);
if (!yylval.fun->equals)
yylval.fun->var = strdup(yytext);
return VARIABLE;
}
[0-9\.]+ {
sscanf(yytext, "%lf", &yylval.fun->dub);
return NUMBER;
}
[ \t] ;
. {
printf("Invalid input. Exiting.\n");
exit(-1);
}
%%