-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.l
52 lines (48 loc) · 2.13 KB
/
calc.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
%{
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include "Types.h"
#include "y.tab.h"
int yywrap();
void yyerror (char *s);
int yylex();
%}
%%
"," { return yytext[0]; }
"<" { return yytext[0]; }
">" { return yytext[0]; }
"--" { return DECR; }
"++" { return INCR; }
"+=" { return PLUSASSIGN; }
"-=" { return MINUSASSIGN; }
"*=" { return MULASSIGN; }
"/=" { return DIVASSIGN; }
"%=" { return MODASSIGN; }
"if" { yylval.s = strdup(yytext) ; return IF;}
"else" { yylval.s = strdup(yytext) ; return ELSE;}
"else if" { yylval.s = strdup(yytext) ; return ELSEIF;}
"}" { return yytext[0]; }
"{" { return yytext[0]; }
"<=" { return LTE; }
">=" { return GTE; }
"==" { return EQ; }
"!=" { return NET; }
"!" { return NOT; }
"&&" { return AND; }
"||" { return OR; }
display { return DISPLAY; }
return { return RETURN ; }
float { yylval.s = strdup(yytext) ; return FLOAT; }
int { yylval.s = strdup(yytext) ; return INT; }
void { yylval.s = strdup(yytext) ; return VOID; }
'if' { return IF; }
[a-zA-Z]+([0-9]+)? { yylval.s = strdup(yytext) ; return VARNAME; }
[0-9]+(\.[0-9]+)?([eE][0-9]+)? { yylval.f = atof(yytext); return FNUM; }
[+\-*/)(=%;] { return yytext[0]; }
[ \t\n]+ { ; }
. { ECHO; yyerror ("unexpected character"); }
%%
int yywrap(){
return 1;
}