-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.cpp
106 lines (89 loc) · 2.96 KB
/
Test.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
#include "scan/Scanner.hpp"
#include "pars/Parser.hpp"
#include "scan/Token.hpp"
#include "scan/TType.hpp"
#include "buffer/Buffer.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using std::cout;
using std::endl;
using std::cerr;
void usage();
int main(int argc, char* argv[]) {
if (argc <= 1) {
usage();
return 1;
}
int parse;
if (strcmp(argv[1], "parse") == 0) {
parse = 1;
} else if (strcmp(argv[1], "scan") == 0) {
parse = 0;
} else {
usage();
return 1;
}
if (argc <= 2) {
usage();
cerr << "No input file given." << endl;
return 1;
} else if (argc <= 3) {
usage();
cerr << "No output file given." << endl;
return 1;
}
Scanner* scanner = new Scanner(argv[2]);
Buffer* buf_out = new Buffer(argv[3], false);
if (!parse) {
//=========================================================
// SCANNEROUTPUT
//=========================================================
const char* ttype_str[] = { "NO_TYPE", "INTEGER", "IDENTIFIER", "PRINT",
"READ", "IF", "ELSE", "WHILE", "INT", "ADDITITON", "SUBTRACTION",
"DIVISION", "MULTIPLICATION", "LT", "GT", "ASSIGN", "NE",
"EXCLAMATION", "AMPERSAND", "SEMICOLON", "COLON", "LEFTBRACKET",
"RIGHTBRACKET", "LEFTANGLEBRACKET", "RIGHTANGLEBRACKET",
"LEFTSQUAREBRACKET", "RIGHTSQUAREBRACKET" };
Token* t;
while ((t = scanner->nextToken())) {
if (t->getType() == NO_TYPE) {
delete t;
// skip error token for writing.
continue;
}
buf_out->addchars( "Token " );
buf_out->addchars( ttype_str[t->getType()]);
buf_out->addchars( " Line: " );
buf_out->addchars(t->getLine());
buf_out->addchars( ", Column " );
buf_out->addchars(t->getColumn());
// cout << "Token " << ttype_str[t->getType()]
// << " Line: " << t->getLine()
// << ", Column " << t->getColumn();
if (t->getType() == IDENTIFIER) {
buf_out->addchars(", Lexem: " );
buf_out->addchars( t->getEntry()->getLexem());
// cout << ", Lexem: " << t->getEntry()->getLexem();
} else if (t->getType() == INTEGER) {
buf_out->addchars(", Value: " );
buf_out->addchars(t->getValue());
// cout << ", Value: " << t->getValue();
}
buf_out->addchars("\n");
// cout << endl;
delete t;
}
//=========================================================
} else {
Parser* parser = new Parser(scanner, buf_out);
parser->parse();
delete parser;
}
delete buf_out;
delete scanner;
return 0;
}
void usage() {
cerr << "Usage: sysprog parse|scan inputfile outputfile" << endl;
}