-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
56 lines (47 loc) · 1.06 KB
/
main.cpp
File metadata and controls
56 lines (47 loc) · 1.06 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
// Main routine for lang compiler.
// This version only runs the lexer
//
// Author: Phil Howard
// phil.howard@oit.edu
//
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include "lex.h"
int main(int argc, char **argv)
{
const char *outfile_name;
int result = 0;
int token;
if (argc > 1)
{
yyin = fopen(argv[1], "r");
if (yyin == NULL)
{
std::cerr << "Unable to open file " << argv[1] << "\n";
exit(-1);
}
}
if (argc > 2)
outfile_name = argv[2];
else
outfile_name = "/dev/tty";
std::streambuf *cout_buf = std::cout.rdbuf();
std::ofstream output(outfile_name);
if (!output.is_open())
{
std::cerr << "Unable to open output file " << outfile_name << "\n";
exit(-1);
}
std::cout.rdbuf(output.rdbuf());
token = yylex();
while (token != 0)
{
std::cout << token << ":" << yytext << "\n";
token = yylex();
}
output.close();
std::cout.rdbuf(cout_buf);
return result;
}