-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.lex
More file actions
63 lines (53 loc) · 1.32 KB
/
lexer.lex
File metadata and controls
63 lines (53 loc) · 1.32 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
%option noyywrap noinput nounput
%{
#include <iostream> // cerr, endl
#include <string_view> // string_view
#include <vector> // vector
#include "ast.hpp"
#include "types.hpp"
#include "parser.hpp" // generated by yacc
/*
* @brief Extern lexical parsing function.
*/
extern int yylex();
/*
* @brief Extern error handling function.
*/
extern void yyerror(std::string_view msg);
%}
%%
"array" { return ARRAY; }
"begin" { return BEGIN_; }
"do" { return DO; }
"else" { return ELSE; }
"end" { return END_; }
"then" { return THEN; }
"if" { return IF; }
"not" { return NOT; }
"of" { return OF; }
"var" { return VAR; }
"writeln" { return WRITELN; }
"program" { return PROGRAM; }
"procedure" { return PROCEDURE; }
"function" { return FUNCTION; }
"Integer" { return INTEGER; }
"Real" { return REAL; }
"while" { return WHILE; }
":=" { return ASSIGN; }
"==" { return EQ; }
"!=" { return NE; }
"<=" { return LE; }
">=" { return GE; }
".." { return ELIPSIS; }
[a-z][A-Za-z0-9_]* {
yylval.identifier = new std::string(yytext);
return ID;
}
[0-9]+([.][0-9]+)? {
yylval.literal = std::atof(yytext);
return NUM;
}
[;:+*<>=/\[\](),.-] { return *yytext; }
[\t\n ] { /* ignore */ }
. { yyerror("Lexical error, invalid token: " + std::string(yytext)); }
%%