-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.l
More file actions
34 lines (27 loc) · 1.02 KB
/
scanner.l
File metadata and controls
34 lines (27 loc) · 1.02 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
%{
#include "parser.tab.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int line_num = 1;
int col_num = 1;
%}
%option noinput nounput
%%
[ \t\r]+ { col_num += yyleng; }
\n { line_num++; col_num = 1; return '\n'; }
[A-Za-z_][A-Za-z0-9_]* { yylval.str_val = strdup(yytext); return VARIABLE; }
[0-9]+ { yylval.int_val = atoi(yytext); return INTEGER; }
"=" { col_num++; return ASSIGN; }
"+" { col_num++; return PLUS; }
"-" { col_num++; return MINUS; }
"*" { col_num++; return MULT; }
"/" { col_num++; return DIV; }
"(" { col_num++; return LPAREN; }
")" { col_num++; return RPAREN; }
. {
fprintf(stderr, "Lexical error: invalid character '%s' at line %d, col %d\n", yytext, line_num, col_num);
exit(1);
}
%%
int yywrap(void) { return 1; }