-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.l
More file actions
80 lines (69 loc) · 2.09 KB
/
lexer.l
File metadata and controls
80 lines (69 loc) · 2.09 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
%{
/*
* Lexer for IMP language
*/
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <string.h>
#include "ast.h"
#include "parser.h"
extern int yylineno;
static char *my_strdup(const char *s) {
char *d = malloc(strlen(s) + 1);
if (d == NULL) return NULL;
strcpy(d, s);
return d;
}
%}
%option noyywrap
%option yylineno
%%
"PROGRAM" { return PROGRAM; }
"PROCEDURE" { return PROCEDURE; }
"IS" { return IS; }
"IN" { return IN; }
"END" { return END; }
"IF" { return IF; }
"THEN" { return THEN; }
"ELSE" { return ELSE; }
"ENDIF" { return ENDIF; }
"WHILE" { return WHILE; }
"DO" { return DO; }
"ENDWHILE" { return ENDWHILE; }
"REPEAT" { return REPEAT; }
"UNTIL" { return UNTIL; }
"FOR" { return FOR; }
"FROM" { return FROM; }
"TO" { return TO; }
"DOWNTO" { return DOWNTO; }
"ENDFOR" { return ENDFOR; }
"READ" { return READ; }
"WRITE" { return WRITE; }
"T" { return T; }
"I" { return I; }
"O" { return O; }
":=" { return ASSIGN; }
"+" { return PLUS; }
"-" { return MINUS; }
"*" { return TIMES; }
"/" { return DIVIDE; }
"%" { return MOD; }
"=" { return EQ; }
"!=" { return NEQ; }
"<" { return LT; }
">" { return GT; }
"<=" { return LEQ; }
">=" { return GEQ; }
"(" { return LPAREN; }
")" { return RPAREN; }
"[" { return LBRACKET; }
"]" { return RBRACKET; }
":" { return COLON; }
";" { return SEMICOLON; }
"," { return COMMA; }
[_a-z]+ { yylval.str = my_strdup(yytext); return PIDENTIFIER; }
[0-9]+ { yylval.num = atoll(yytext); return NUM; }
#.* { /* comment - ignore */ }
[ \t\r\n]+ { /* whitespace - ignore */ }
. { fprintf(stderr, "Błąd leksykalny w linii %d: nieznany znak '%s'\n", yylineno, yytext); exit(1); }
%%