-
Notifications
You must be signed in to change notification settings - Fork 1
/
tokenizer.l
60 lines (52 loc) · 2.24 KB
/
tokenizer.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
%{
/////////////////////////////////////////////////////////////////////
//
// Creator: Sara Baradaran, Mahdi Heidari
// Creation Date: Jan 2020
// Project Title: C to MIPS assembly compiler
//
/////////////////////////////////////////////////////////////////////
#include "parser.tab.h"
#include <string.h>
%}
%%
[1-9][0-9]*|0 {printf("I see a num : %s \n", yytext); yylval.ival = atoi(yytext); return NUM; }
"void " {printf("I see void keyword \n"); return VOID;}
"int " {printf("I see int keyword \n"); return INT;}
"if" {printf("I see int if \n"); return IF;}
"else" {printf("I see int else \n"); return ELSE;}
"return" {printf("I see return \n"); return RETURN;}
"while" {printf("I see while \n"); return WHILE;}
"global " {printf("I see global \n"); return GLOBAL;}
"print" {printf("I see print \n"); return PRINT;}
"\\n" {printf("I see backn \n"); return BACKN;}
([a-z]|[A-Z])+ {printf("I see an id : %s \n", yytext);
yylval.sval = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.sval, yytext); return ID;}
"=" {printf("I see = \n"); return ASSIGN;}
"+" {printf("I see + \n"); return PLUS;}
"-" {printf("I see - \n"); return MINUS;}
"*" {printf("I see * \n"); return MUL;}
"/" {printf("I see / \n"); return DIV;}
"<=" {printf("I see <= \n"); return BLE;}
">=" {printf("I see >= \n"); return BGE;}
"<" {printf("I see < \n"); return BLT;}
">" {printf("I see > \n"); return BGT;}
"==" {printf("I see == \n"); return EQUAL;}
"!=" {printf("I see != \n"); return NOTEQUAL;}
"!" {printf("I see ! \n"); return NOT;}
"&&" {printf("I see && \n"); return AND;}
"&" {printf("I see & \n"); return BITAND;}
"||" {printf("I see || \n"); return OR;}
"|" {printf("I see | \n"); return BITOR;}
"(" {printf("I see ( \n"); return OPENP;}
")" {printf("I see ) \n"); return CLOSEP;}
"{" {printf("I see { \n"); return OPENA;}
"}" {printf("I see } \n"); return CLOSEA;}
";" {printf("I see ; \n"); return SEMICOLON;}
"," {printf("I see , \n"); return COMMA;}
"[" {printf("I see [ \n"); return OPENB;}
"]" {printf("I see ] \n"); return CLOSEB;}
[ \t]+ {printf("I see space \n");}
[\n]+ {printf("I see newline \n");}
%%
int yywrap(){}