-
Notifications
You must be signed in to change notification settings - Fork 0
/
cal_scanner.l
36 lines (32 loc) · 1.24 KB
/
cal_scanner.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
/*recognize tokens for the calculator and print them out*/
%{
#define YYSTYPE double
# include "cal_parser.tab.h"
# include <stdlib.h>
%}
white [ \t]+
digit [0-9]
integer {digit}+
exp [eE][+-]?{integer}
double {digit}*\.?{integer}{exp}?|{integer}+{exp}?\.
hex 0[xX][0-9a-fA-F]+
oct "O"[0-7]+
%%
"+" { printf("PLUS\n"); return ADD; }
"-" { printf("MINUS\n"); return SUB; }
"*" { printf("MULT\n"); return MUL; }
"/" { printf("DIV\n"); return DIV; }
"(" { printf("OPENPAR\n"); return OP; }
")" { printf("CLOSEPAR\n"); return CP; }
"^" { printf("POWER\n"); return POW;}
"%" { printf("MOD\n"); return MOD; }
{integer} { printf("INTEGER\n"); yylval = strtol (yytext, NULL, 10); return NUMBER; }
{double} { printf("DOUBLE\n"); yylval = atof(yytext); return FLOAT; }
{hex} { printf("HEX-NUMBER\n"); yylval = strtol (yytext + 2, NULL, 16); return HEX;}
{oct} { printf("OCTAL-NUMBER\n"); yylval = strtol (yytext + 1, NULL, 8); return OCT;} /*octal number with a O prefix*/
"$" { printf("EOF\n");}
\n { printf("\nNEWLINE\n"); return EOL; }
"//".* /* ignores comments */
[ \t] { /* ignore white space */ }
. { yyerror("Other character %c\n", *yytext); }
%%