-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.l
60 lines (52 loc) · 1.68 KB
/
game.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
%{
#include "y.tab.h"
#include <string.h>
void yyerror (char *s);
int yylex();
%}
%%
[0-9]+ {yylval.num = atoi(yytext); return valtoken;}
"ADD" {yylval.op = yytext[0]; return operation;}
"SUBTRACT" {yylval.op = yytext[0]; return operation;}
"MULTIPLY" {yylval.op = yytext[0]; return operation;}
"DIVIDE" {yylval.op = yytext[0]; return operation;}
"LEFT" {yylval.dir = yytext[0]; return direction;}
"RIGHT" {yylval.dir = yytext[0]; return direction;}
"UP" {yylval.dir = yytext[0]; return direction;}
"DOWN" {yylval.dir = yytext[0]; return direction;}
"ASSIGN" {return assign;}
"TO" {return to;}
"VAR" {return var;}
"IS" {return is;}
"VALUE IN" {return valin;}
[.] {return yytext[0];}
[\n] {return yytext[0];}
[ ] {;}
[0-9]+[ \t]*[,][ \t]*[0-9]+ {
int i = 0, j = 0;
while (yytext[i])
{
if (yytext[i] != ' ' && yytext[i] != '\t')
yytext[j++] = yytext[i];
i++;
}
yytext[j] = '\0';
char str[100];
strcpy(str, yytext);
char* token_x = strtok(str, ",");
char* token_y = strtok(NULL, ",");
int x = atoi(token_x);
int y = atoi(token_y);
if(x >= 1 && x <= 4 && y >=1 && y <= 4){
yylval.id = yytext;
return coordinate;
}
else{
printf("\n2048> %s is not a valid coordinate. The tile coordinates must be in the range 1,2,3,4.\n", yytext);
return err_token;
}
}
[a-zA-Z_][a-zA-Z0-9_]* {yylval.id = yytext; return identifier;}
. {return err_token;}
%%
int yywrap (void) {return 1;}