-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·105 lines (85 loc) · 3.37 KB
/
Copy pathmain.cpp
File metadata and controls
executable file
·105 lines (85 loc) · 3.37 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
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
//
// main.cpp
// Project1
//
// Created by Michael Bird on 6/25/14.
// Copyright (c) 2014 Michael Bird. All rights reserved.
//
#include <iostream>
#include <string>
#include <fstream>
#include "LexicalAnalyzer.h"
using namespace std;
// function to return enum values as strings.
string tokenizer(int token) {
switch (token) {
case SPACE: return "SPACE";
case ERROR: return "ERROR";
case STOP: return "STOP";
//case INTEGER: return "ERROR";
case COMMA: return "COMMA";
case PERIOD: return "PERIOD";
case Q_MARK: return "Q_MARK";
case LEFT_PAREN: return "LEFT_PAREN";
case RIGHT_PAREN: return "RIGHT_PAREN";
case COLON: return "COLON";
case COLON_DASH: return "COLON_DASH";
case SCHEMES: return "SCHEMES";
case FACTS: return "FACTS";
case RULES: return "RULES";
case QUERIES: return "QUERIES";
case ID: return "ID";
case STRING: return "STRING";
default: return "ERROR";
}
}
int main (int argc, char *argv[])
{
//ifstream inputFile("/Users/birdman/Documents/Project1/Project1/input.txt");
//ofstream outputFile("/Users/birdman/Documents/Project1/Project1/output.txt");
ifstream inputFile(argv[1]);
ofstream outputFile(argv[2]);
string line;
// long long length;
/*
inputFile.seekg(0, ios::end); // put the "cursor" at the end of the file
length = inputFile.tellg(); // find the position of the cursor
inputFile.seekg(0, ios::beg); // return the "cursor" at the beginning of the file
if ( length == 0 ) { // checks for the file length
outputFile << "Failure: The file is empty!" << endl;
}
else {
*/
int token=SPACE;
int linecount = 0;
int tokencount = 0;
// Use getline to handle the code line by line which will make it easy to get the line numbers
LexAn *LA;
while(getline(inputFile, line)) {
linecount++;
LA = new LexAn(line);
// keeps reading in characters until the endline STOP token appears
while (token != STOP) {
tokencount++;
token = LA->lex();
if (token == STOP)
break; //do not show any end of line (STOP) tokens
else if(token == PLUS_CODE) {
outputFile << "Input Error on line " << linecount << endl;
delete LA; //frees the memory blocks so there arent memory leaks.
return 0;
}
else if (token == ERROR || tokenizer(token) == "ERROR") {
outputFile << "Input Error on line " << linecount << endl;
delete LA; //frees the memory blocks so there arent memory leaks.
return 0;
}
else {
outputFile << "(" << tokenizer(token) << ",\"" << LA->lexeme << "\"," << linecount << ")" << endl;
}
}
token = 0; //resets the token so that the getline while loop moves to the next line.
delete LA; //frees the memory blocks so there arent memory leaks.
}
outputFile << "Total Tokens = " << tokencount - linecount << endl;
}