-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwhile.cc
51 lines (44 loc) · 1.32 KB
/
while.cc
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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <list>
#include "FlexLexer.h"
#include "implementation.hh"
#include "while.tab.hh"
yyFlexLexer *lexer;
int yylex(yy::parser::semantic_type* yylval, yy::parser::location_type* yylloc) {
yylloc->begin.line = lexer->lineno();
int token = lexer->yylex();
if(token == yy::parser::token::ID || token == yy::parser::token::NUM) {
yylval->build(std::string(lexer->YYText()));
}
return token;
}
void yy::parser::error(const location_type& loc, const std::string& msg) {
std::cerr << "Line " << loc.begin.line << ": " << msg << std::endl;
exit(1);
}
int main(int argc, char *argv[]) {
if(argc != 3) {
std::cerr << "Usage: " << argv[0] << " (-c|-i) inputfile" << std::endl;
exit(1);
}
if(std::string(argv[1]) == "-c") {
current_mode = compiler;
} else if(std::string(argv[1]) == "-i") {
current_mode = interpreter;
} else {
std::cerr << "Usage: " << argv[0] << "(-c|-i) inputfile" << std::endl;
exit(1);
}
std::ifstream input(argv[2]);
if(!input) {
std::cerr << "Cannot open input file: " << argv[2] << std::endl;
exit(1);
}
lexer = new yyFlexLexer(&input);
yy::parser parser;
parser.parse();
delete lexer;
return 0;
}