-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.l
More file actions
148 lines (119 loc) · 3.1 KB
/
Copy pathlexer.l
File metadata and controls
148 lines (119 loc) · 3.1 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
/* scanner for the Call Me Calc language */
%{
#include <stdio.h>
#include <stdarg.h>
#include "parser.h"
#include "grammar.tab.h"
/* need this for the call to atof() below */
#include <math.h>
int column = 0;
int line = 1;
int last_indent = 0;
void count(void);
void debug(const char *format, ...);
%}
DIGIT [0-9]
ID [a-z][a-z0-9]*
LETTER [a-zA-Z_]
%%
{DIGIT}+ {
count();
//printf( "An integer: %s (%d)\n", yytext,
// atoi( yytext ) );
yylval.longValue = atoi(yytext);
return INT_CONSTANT;
}
{DIGIT}+"."{DIGIT}* {
count();
//printf( "A float: %s (%g)\n", yytext,
// atof( yytext ) );
yylval.ldoubleValue = atof(yytext);
return FLOAT_CONSTANT;
}
LETTER?\"(\\.|[^\\"])*\" {
count();
yylval.stringValue = strdup(yytext);
return(STRING_CONSTANT);
}
"\'"."\'" {
count();
return CHAR_CONSTANT;
}
def {
count();
//printf( "function keyword: %s\n", yytext );
return DEF;
}
if {
count();
//printf( "if keyword: %s\n", yytext );
return IF;
}
else {
count();
//printf( "else keyword: %s\n", yytext );
return ELSE;
}
for {
count();
//printf( "para keyword: %s\n", yytext );
return FOR;
}
while {
count();
//printf( "if keyword: %s\n", yytext );
return WHILE;
}
{ID} {
count();
//printf( "An identifier: %s\n", yytext );
yylval.idValue = strdup(yytext);
return ID;
}
"("|")"|":"|"+"|"-"|"*"|"/"|"%"|"&"|"\|"|"=" {
count();
//printf( "An operator: %s\n", yytext );
return(yytext[0]);
}
"{"[^}\n]*"}" { /* eat up one-line comments */
count();
}
[ \t]+ { /* eat up whitespace */
count();
last_indent = column;
}
\n {
count();
return '\n';
}
. {
printf( "Unrecognized character '%s'on line %d, column %d: \n", yytext, line,column );
}
%%
int yywrap(void)
{
return 1;
}
void count(void)
{
int i;
for (i = 0; yytext[i] != '\0'; i++)
if (yytext[i] == '\n'){
column = 0;
line++;
}else if (yytext[i] == '\t')
column += 8 - (column % 8);
else
column++;
//ECHO;
// printf("\nFound something: %s\n", yytext);
}
void debug(const char *format, ...)
{
va_list args;
char buffer[BUFSIZE];
va_start(args, format);
vsnprintf(buffer, sizeof buffer, format, args);
va_end(args);
fprintf(stderr, "%s\n",buffer);
}