-
Notifications
You must be signed in to change notification settings - Fork 0
/
tdl.flex
70 lines (65 loc) · 1.67 KB
/
tdl.flex
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
%{
/*
* exp.flex: lexical analyzer for arithmetic expressions
*/
#include "tdl.h"
#include <string.h>
int get_symbol(char* yytext);
%}
blank [ \t]
letter [a-zA-Z]
digit [0-9]
digits {digit}+
fraction (\.{digits})
exponent (E(\+|\-)?{digits})
%%
{blank}+ ;
\n yylineno++;
({letter}|_)({letter}|{digit}|_)* return get_symbol( yytext);
{digits} return CONST_INT;
{digits}{fraction} return CONST_REAL;
{digits}{exponent} return CONST_REAL;
{digits}{fraction}{exponent} return CONST_REAL;
\"[^\n\"]*\" return CONST_CHAR;
. return yytext[ 0];
%%
#include <string.h>
int get_symbol(char* yytext)
{
if(strcmp(yytext,"program") == 0)
return PROGRAM;
else if(strcmp(yytext,"end") == 0)
return END;
else if(strcmp(yytext,"int") == 0)
return INT;
else if(strcmp(yytext,"real") == 0)
return REAL;
else if(strcmp(yytext,"write") == 0)
return WRITE;
else if(strcmp(yytext,"writeln") == 0)
return WRITELN;
else if(strcmp(yytext,"if") == 0)
return IF;
else if(strcmp(yytext,"then") == 0)
return THEN;
else if(strcmp(yytext,"else") == 0)
return ELSE;
else if(strcmp(yytext,"endif") == 0)
return ENDIF;
else if(strcmp(yytext,"false") == 0)
return FALSE;
else if(strcmp(yytext,"true") == 0)
return TRUE;
else if(strcmp(yytext,"for") == 0)
return FOR;
else if(strcmp(yytext,"endfor") == 0)
return ENDFOR;
else if(strcmp(yytext,"stop") == 0)
return STOP;
else if(strcmp(yytext,"break") == 0)
return BREAK;
else if(strcmp(yytext,"continue") == 0)
return CONTINUE;
else
return ID;
}