This repository was archived by the owner on Jul 27, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathLanguage.lex
More file actions
157 lines (140 loc) · 1.99 KB
/
Copy pathLanguage.lex
File metadata and controls
157 lines (140 loc) · 1.99 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
%{
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "SymbolTable.h"
#ifdef TestLex
#define END 10
#define LPAREN 258
#define FUNC 258
#define RPAREN 259
#define LBRACE 260
#define RBRACE 261
#define NEWLINE 262
#define ASSIGNMENT 263
#define INTEGER 264
#define FLOATING 265
#define ADDITION 266
#define SUBTRACTION 267
#define MULTIPLICATION 268
#define DIVISION 269
#define MODULUS 270
#define EQUALITY 271
#define LESS 272
#define GREATER 273
#define VAR 274
#endif
%}
%%
\/\*.*\*\/ {
//Do nothing
}
\/\/.*\n {
//Do nothing
}
goto[ ]+\([A-z]+\) {
FILE* f = yyin;
if(yyin == stdin) {
//printf(stderr, "Cannot goto using stdin\n");
return END;
}
}
func[ ]+[A-z]+\(([A-z]+.*)*\) {
#ifndef TestLex
yylval.tok.val = 0;
yylval.tok.name = (char*) strdup(yytext);
#endif
return FUNC;
}
[A-z]+ {
#ifndef TestLex
yylval.tok.val = 0;
yylval.tok.name = (char*) strdup(yytext);
#endif
//printf("VARIABLE");
return VAR;
}
\- {
//printf("SUBTRACT");
return SUBTRACTION;
}
[0-9]+\.[0-9]+ {
#ifndef TestLex
yylval.tok.val = atof(yytext);
yylval.tok.name = 0;
#endif
//printf("FLOAT");
return FLOATING;
}
[0-9]+ {
#ifndef TestLex
yylval.tok.val = atoi(yytext);
yylval.tok.name = 0;
#endif
//printf("NUMBER");
return INTEGER;
}
\:= {
//printf("ASSIGNMENT");
return ASSIGNMENT;
}
\+ {
//printf("ADD");
return ADDITION;
}
\* {
//printf("MULT");
return MULTIPLICATION;
}
\/ {
//printf("DIVIDE");
return DIVISION;
}
\( {
//printf("LPAREN");
return LPAREN;
}
\) {
//printf("RPAREN");
return RPAREN;
}
\{ {
//printf("LBRACE");
return LBRACE;
}
\} {
//printf("RBRACE");
return RBRACE;
}
\% {
//printf("MOD");
return MODULUS;
}
\= {
return EQUALITY;
}
\< {
return LESS;
}
\> {
return GREATER;
}
\; {
return END;
}
[ \n\t] ;
. {
fprintf(stderr, "ERROR: Unknown Token %s\n", yytext);
}
%%
int yywrap() {
return 1; //scan forever
}
#ifdef TestLex
int main(int argc, char* argv[]) {
yyin = stdin;
if(argc > 1)
yyin = fopen(argv[1], "r");
return yylex();
}
#endif