-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.l
94 lines (82 loc) · 1.72 KB
/
lexer.l
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
/**
Kacper Zielinski,
236698,
Wydzial Podstawowych Problemow Techniki,
Politechnika Wroclawska
Kompilator 2018/2019
Lexer
**/
%option noyywrap
%option yylineno
%{
#include "parser.tab.h"
#include <string>
extern void yyerror(const char*);
%}
%x COMMENT
%%
"[" BEGIN(COMMENT);
<COMMENT>"]" BEGIN(INITIAL);
<COMMENT>. ;
<COMMENT>\n ;
[ \t]+ ;
\n ;
"+" return ADD;
"-" return SUB;
"*" return MUL;
"/" return DIV;
"%" return MOD;
"=" return EQ;
"!=" return NEQ;
"<" return LE;
">" return GE;
"<=" return LEQ;
">=" return GEQ;
":=" return ASSIGN;
"(" return LB;
")" return RB;
":" return COLON;
";" return SEMICOLON;
"DECLARE" return DECLARE;
"IN" return IN;
"END" return END;
"WRITE" return WRITE;
"READ" return READ;
"IF" return IF;
"THEN" return THEN;
"ELSE" return ELSE;
"ENDIF" return ENDIF;
"WHILE" return WHILE;
"DO" return DO;
"ENDDO" return ENDDO;
"ENDWHILE" return ENDWHILE;
"FOR" return FOR;
"FROM" return FROM;
"TO" return TO;
"DOWNTO" return DOWNTO;
"ENDFOR" return ENDFOR;
[_a-z]+[A-Z0-9]+ {
std::string lexerError("Invalid identifier: '");
std::string yyLexerError(yytext);
lexerError += yyLexerError;
lexerError += "'";
yyerror(lexerError.c_str());
}
[_a-z]+ {
yylval.str = strdup(yytext);
yylval.var.str = strdup(yytext);
return pidentifier;
}
[0-9]+ {
yylval.str = strdup(yytext);
yylval.var.str = strdup(yytext);
return num;
}
. {
std::string lexerError("Unexcepted token: '");
std::string yyLexerError(yytext);
lexerError += yyLexerError;
lexerError += "'";
yyerror(lexerError.c_str());
}
%%