forked from jaredhoberock/compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kaleidoscope.ll
68 lines (55 loc) · 1.43 KB
/
kaleidoscope.ll
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
%{
#include "kaleidoscope.yy.hpp" // automatically generated by bison
#include "driver.hpp"
#include "yylex_decl.hpp"
#undef yywrap
#define yywrap() 1
static yy::location loc;
%}
/*
%option noyywrap nounput batch debug noinput
*/
%option noyywrap nounput batch noinput
IDENTIFIER [a-zA-Z][a-zA-Z_0-9]*
BLANK [ \t]
NUMBER [0-9]*\.?[0-9]*
%{
// code run each time a pattern is matched
#define YY_USER_ACTION loc.columns(yyleng);
%}
%%
%{
// code run each time yylex is called
loc.step();
%}
{BLANK}+ loc.step();
[\n]+ loc.lines(yyleng); loc.step();
"def" return yy::parser::make_DEF(loc);
"extern" return yy::parser::make_EXTERN(loc);
{IDENTIFIER} return yy::parser::make_IDENTIFIER(yytext, loc);
{NUMBER} return yy::parser::make_NUMBER(std::atof(yytext), loc);
"(" return yy::parser::make_LPAREN(loc);
")" return yy::parser::make_RPAREN(loc);
"+" return yy::parser::make_PLUS(loc);
"-" return yy::parser::make_MINUS(loc);
"*" return yy::parser::make_MULTIPLIES(loc);
"/" return yy::parser::make_DIVIDES(loc);
";" return yy::parser::make_SEMICOLON(loc);
<<EOF>> return yy::parser::make_END(loc);
%%
void driver::scan_begin()
{
if(m_filename.empty() || m_filename == "-")
{
yyin = stdin;
}
else if(!(yyin = fopen(m_filename.c_str (), "r")))
{
error("cannot open " + m_filename + ": " + strerror(errno));
exit(EXIT_FAILURE);
}
}
void driver::scan_end()
{
fclose(yyin);
}