-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdd.l
More file actions
74 lines (64 loc) · 1.92 KB
/
dd.l
File metadata and controls
74 lines (64 loc) · 1.92 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
%{
#define YYSTYPE char*
#include "y.tab.h"
void comment();
void count();
void yyerror();
%}
%%
"fun" { count(); yylval = strdup(yytext); return FUN; }
"//"[^\n]* { /* Discard comments. */ }
"/*" { comment(); }
[ \t\n\f]+ { count(); /* Ignore whitespace */ }
"{" { count(); return OPEN_BRACE; }
"}" { count(); return CLOSE_BRACE; }
"(" { count(); return LB; }
")" { count(); return RB; }
"~" { count(); return NT; }
"!" { count(); return LN; }
"+" { count(); return PLUS; }
"-" { count(); return MINUS; }
"*" { count(); return MULT; }
"<" { count(); return LST; }
"&&" { count(); return AD; }
"||" { count(); return ORR; }
"==" { count(); return EQ; }
">" { count(); return GRT; }
">=" { count(); return GT_EQ; }
"<=" { count(); return LT_EQ; }
"=" { count(); return ASN; }
";" { count(); return SEMICOLON; }
"," { count(); return COMMA; }
[0-9]+ {
/* TODO: check numbers are in the legal range, and don't start with 0. */
count(); yylval = strdup(yytext); return NUMBER;}
"if" { count(); return IF; }
"else" { count(); return ELSE; }
"return" { count(); return RETURN; }
"var" { count(); return TYPE; }
[a-zA-Z][_a-zA-Z0-9]* { count(); yylval = strdup(yytext); return IDENTIFIER; }
%%
#define INPUT_EOF 0
int column = 0;
void comment(void) {
/* Consume characters up to the closing comment marker. */
char c, prev = 0;
while ((c = input()) != INPUT_EOF) {
if (c == '/' && prev == '*')
return;
prev = c;
}
yyerror("unterminated comment");
}
void count()
{
int i;
for (i = 0; yytext[i] != '\0'; i++)
if (yytext[i] == '\n')
column = 0;
else if (yytext[i] == '\t')
column += 8 - (column % 8);
else
column++;
ECHO;
}