forked from ToshioCP/Gtk4-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturtle.lex
78 lines (70 loc) · 2.97 KB
/
turtle.lex
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
%top{
#include <string.h>
#include <stdlib.h>
#include <glib.h>
#include "turtle_parser.h"
static int nline = 1;
static int ncolumn = 1;
static void get_location (char *text);
/* Dinamically allocated memories are added to the single list. They will be freed in the finalize function. */
extern GSList *list;
}
%option noyywrap
REAL_NUMBER (0|[1-9][0-9]*)(\.[0-9]+)?
IDENTIFIER [a-zA-Z][a-zA-Z0-9]*
%%
/* rules */
#.* ; /* comment. Be careful. Dot symbol (.) matches any character but new line. */
[ ] ncolumn++; /* white space. [ and ] is a "character class". */
\t ncolumn += 8; /* assume that tab is 8 spaces. */
\n nline++; ncolumn = 1;
/* reserved keywords */
pu get_location (yytext); return PU; /* pen up */
pd get_location (yytext); return PD; /* pen down */
pw get_location (yytext); return PW; /* pen width = line width */
fd get_location (yytext); return FD; /* forward */
tr get_location (yytext); return TR; /* turn right */
tl get_location (yytext); return TL; /* turn left, since ver 0.5 */
bc get_location (yytext); return BC; /* background color */
fc get_location (yytext); return FC; /* foreground color */
dp get_location (yytext); return DP; /* define procedure */
if get_location (yytext); return IF; /* if statement */
rt get_location (yytext); return RT; /* return statement */
rs get_location (yytext); return RS; /* reset the status */
rp get_location (yytext); return RP; /* repeat, since ver 0.5 */
/* constant */
{REAL_NUMBER} get_location (yytext); yylval.NUM = atof (yytext); return NUM;
/* identifier */
{IDENTIFIER} { get_location (yytext); yylval.ID = g_strdup(yytext);
list = g_slist_prepend (list, yylval.ID);
return ID;
}
"=" get_location (yytext); return '=';
">" get_location (yytext); return '>';
"<" get_location (yytext); return '<';
"+" get_location (yytext); return '+';
"-" get_location (yytext); return '-';
"*" get_location (yytext); return '*';
"/" get_location (yytext); return '/';
"(" get_location (yytext); return '(';
")" get_location (yytext); return ')';
"{" get_location (yytext); return '{';
"}" get_location (yytext); return '}';
"," get_location (yytext); return ',';
. ncolumn++; return YYUNDEF;
%%
static void
get_location (char *text) {
yylloc.first_line = yylloc.last_line = nline;
yylloc.first_column = ncolumn;
yylloc.last_column = (ncolumn += strlen(text)) - 1;
}
static YY_BUFFER_STATE state;
void
init_flex (const char *text) {
state = yy_scan_string (text);
}
void
finalize_flex (void) {
yy_delete_buffer (state);
}