Skip to content

Commit 2de7b7b

Browse files
authored
Add files via upload
1 parent 13ab9e6 commit 2de7b7b

File tree

11 files changed

+415
-0
lines changed

11 files changed

+415
-0
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.17)
2+
project(Project1Phase1)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
add_executable(Project1Phase1 main.cpp Tokenizer.cpp Tokenizer.hpp Token.cpp Token.hpp Stack.cpp Stack.hpp)

Stack.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// Created by gmook on 2/6/2021.
3+
//
4+
5+
#include "Stack.hpp"
6+
7+
void Stack::push(int n) {
8+
stack.push_back(n);
9+
}
10+
int Stack::top() {
11+
return stack.at(stack.size()-1);
12+
}
13+
void Stack::pop() {
14+
stack.pop_back();
15+
}
16+
bool Stack::empty() {
17+
return stack.empty();
18+
}

Stack.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Created by gmook on 2/6/2021.
3+
//
4+
5+
#ifndef PROJECT1PHASE1_STACK_HPP
6+
#define PROJECT1PHASE1_STACK_HPP
7+
8+
9+
#include <vector>
10+
11+
class Stack {
12+
public:
13+
14+
void push(int n);
15+
int top();
16+
void pop();
17+
bool empty();
18+
19+
private:
20+
std::vector<int> stack;
21+
22+
};
23+
24+
25+
26+
#endif //PROJECT1PHASE1_STACK_HPP

Token.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//
2+
// Template by Ali A. Kooshesh on 1/27/21.
3+
// Additional Code by Garret Mook on 02/06/21.
4+
5+
6+
#include <iostream>
7+
#include <iomanip>
8+
#include "Token.hpp"
9+
10+
Token::Token(int line, int pos): _isOpenTag{false},
11+
_isCloseTag{false},
12+
_isCloseAngleBracket{false},
13+
_isCloseStandAloneTag{false},
14+
_tagName{""},
15+
_eof{false},
16+
_lineNumber{line},
17+
_charPos{pos},
18+
_eol{false},
19+
_isSlash{false} {}
20+
21+
bool &Token::isOpenTag() { return _isOpenTag; }
22+
bool &Token::isCloseTag() { return _isCloseTag; }
23+
24+
bool &Token::isCloseAngleBracket() { return _isCloseAngleBracket; }
25+
26+
bool &Token::endOfFile() { return _eof; }
27+
bool &Token::isCloseStandAloneTag() { return _isCloseStandAloneTag; }
28+
bool &Token::isSlash() { return _isSlash; }
29+
30+
std::string Token::tagName() { return _tagName; }
31+
32+
33+
bool &Token::eol(){
34+
return _eol;
35+
36+
}
37+
38+
void Token::makeOpenTag(std::string name) {
39+
_tagName = name;
40+
isOpenTag() = true;
41+
}
42+
std::string Token::makeOpenTag() {
43+
return _tagName;
44+
}
45+
void Token::makeCloseTag(std::string name) {
46+
_tagName = name;
47+
48+
isCloseTag() = true;
49+
}
50+
/*
51+
void Token::print() {
52+
std::cout << "[" << std::setw(2) << _lineNumber << ", " << std::setw(3) << _charPos << "] ";
53+
54+
if(isOpenTag()) {
55+
std::cout << "<" << tagName() << std::endl;
56+
} else if(isCloseStandAloneTag()) {
57+
std::cout << "/>" << std::endl;
58+
} else if(isCloseAngleBracket()) {
59+
std::cout << ">" << std::endl;
60+
} else if(isCloseTag()) {
61+
std::cout << "</" << tagName() << std::endl;
62+
} else if(isSlash() ) {
63+
64+
}
65+
else {// more else if's before this else
66+
std::cout << "Unknown" << std::endl;
67+
}
68+
}
69+
*/
70+
71+
72+
73+
void Token::print() {
74+
75+
if(isOpenTag()) {
76+
std::cout << "[" << std::setw(2) << _lineNumber << ", " << std::setw(3) << _charPos << "] ";
77+
std::cout << "<" << tagName() << std::endl;
78+
} else if(isCloseStandAloneTag()) {
79+
std::cout << "[" << std::setw(2) << _lineNumber << ", " << std::setw(3) << _charPos << "] ";
80+
std::cout << "/>" << std::endl;
81+
} else if(isCloseAngleBracket()) {
82+
std::cout << "[" << std::setw(2) << _lineNumber << ", " << std::setw(3) << _charPos << "] ";
83+
std::cout << ">" << std::endl;
84+
} else if(isCloseTag()) {
85+
std::cout << "[" << std::setw(2) << _lineNumber << ", " << std::setw(3) << _charPos << "] ";
86+
std::cout << "</" << tagName() << std::endl;
87+
} else if(isSlash() ) {
88+
89+
}
90+
else {// more else if's before this else
91+
std::cout << "Unknown" << std::endl;
92+
}
93+
}

Token.hpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// Template by Ali A. Kooshesh on 1/27/21.
3+
// Additional Code by Garret Mook on 02/06/21.
4+
5+
#ifndef PROJECT1PHASE1_TOKEN_HPP
6+
#define PROJECT1PHASE1_TOKEN_HPP
7+
8+
#include <string>
9+
10+
class Token {
11+
public:
12+
Token(int, int);
13+
bool &isOpenTag();
14+
bool &isCloseTag();
15+
16+
bool &isCloseAngleBracket();
17+
18+
bool &endOfFile();
19+
bool &isCloseStandAloneTag();
20+
21+
22+
std::string tagName();
23+
//Self Added
24+
25+
void makeOpenTag(std::string);
26+
void makeCloseTag(std::string);
27+
//Self Added
28+
std::string makeOpenTag();
29+
bool &isSlash();
30+
31+
void print();
32+
33+
34+
35+
bool &eol();
36+
37+
private:
38+
bool _isOpenTag, _isCloseTag, _isCloseAngleBracket,
39+
_eof, _isCloseStandAloneTag, _eol, _isSlash;
40+
std::string _tagName;
41+
int _lineNumber, _charPos;
42+
43+
44+
};
45+
46+
47+
#endif //PROJECT1PHASE1_TOKEN_HPP

Tokenizer.cpp

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Template by Ali A. Kooshesh on 1/27/21.
2+
// Additional Code by Garret Mook on 02/06/21.
3+
4+
//READER/LEXICAL ANALYZER
5+
//Implemenation Of Operations
6+
7+
#include <iostream>
8+
#include "Tokenizer.hpp"
9+
#include <string>
10+
#include <vector>
11+
//
12+
#include <cstdio>
13+
#include <cmath>
14+
#include <stdio.h>
15+
#include <ctype.h>
16+
17+
//Initializer List
18+
Tokenizer::Tokenizer(std::string name): lineNumber{1},
19+
charPosition{1},
20+
inputFileName{name} {
21+
inputStream.open(inputFileName, std::ios::in); // open the input file. We will make sure that it is open in getToken.
22+
}
23+
//This defines what a Character of interest is to the reader.
24+
bool Tokenizer::charOfInterest(char c) {
25+
// is c the initial (or the sole) character of a token?
26+
return c == '<' || c == '>' || c == '/';
27+
}
28+
29+
Token Tokenizer::getToken() {
30+
char c;
31+
//Checks to see if the file we are trying to read is open
32+
if( ! inputStream.is_open()) {
33+
std::cout << "Tokenizer::getToken() called with a stream that is not open." << std::endl;
34+
std::cout << "Make sure that " << inputFileName << " exists and is readable. Terminating.";
35+
exit(2);
36+
}
37+
38+
//GM: This while loop executes until we find a charOfInterest
39+
while( inputStream.get(c) && ! charOfInterest(c) ) {
40+
// keep track of the line number and the character position here.
41+
if (c == '\n') {
42+
lineNumber++;
43+
44+
}
45+
else {
46+
charPosition++;
47+
}
48+
49+
}
50+
51+
52+
//GM: After loop stops this executes
53+
Token token(lineNumber, charPosition);
54+
55+
if( inputStream.eof() ) {
56+
token.endOfFile() = true;
57+
}
58+
// if < is found
59+
else if( c == '<' && inputStream.peek() != '/' && inputStream.peek() != '>') {
60+
if( isalpha(inputStream.peek()) ) {
61+
charPosition++;
62+
std::string needthisforPeek;
63+
while (inputStream.get(c) && isalpha(c) && !isspace(c) && inputStream.peek() != '/') {
64+
needthisforPeek = needthisforPeek + c;
65+
charPosition++;
66+
}
67+
68+
inputStream.putback(c);
69+
token.makeOpenTag(needthisforPeek);
70+
return token;
71+
}
72+
}
73+
74+
/*
75+
// if /> is found
76+
else if (c == '/' && inputStream.peek() == '>') {
77+
inputStream.get(c);
78+
token.isCloseStandAloneTag() = true;
79+
}
80+
*/
81+
82+
83+
else if (c == '/') {
84+
if (c == '/' && isalpha(inputStream.peek()) ) {
85+
token.isSlash() = true;
86+
charPosition++;
87+
88+
} else if (c == '/' && inputStream.peek() == '>') {
89+
inputStream.get(c);
90+
token.isCloseStandAloneTag() = true;
91+
charPosition++;
92+
}
93+
94+
95+
}
96+
97+
98+
99+
100+
101+
102+
103+
// if > is found
104+
else if( c == '>' ) {
105+
token.isCloseAngleBracket() = true;
106+
charPosition++;
107+
}
108+
// if <tagname is found then it is NOT a closed standalone tag
109+
else if (isalpha(inputStream.peek())) {
110+
token.isCloseStandAloneTag() = false;
111+
charPosition++;
112+
}
113+
114+
//IF </ is found
115+
else if ( inputStream.good() && c == '<' && inputStream.peek() == '/') {
116+
inputStream.get(c);
117+
charPosition++;
118+
std::string neededToReadPeek;
119+
if (c== '/' && isalpha(inputStream.peek())) {
120+
while (inputStream.good() && inputStream.get(c) && isalpha(c)) {
121+
neededToReadPeek += c;
122+
charPosition++;
123+
}
124+
inputStream.putback(c);
125+
token.makeCloseTag(neededToReadPeek);
126+
charPosition++;
127+
return token;
128+
}
129+
}
130+
131+
else {
132+
std::cout << "Unknown Character: " << c;
133+
exit(1);
134+
}
135+
136+
return token;
137+
138+
}

Tokenizer.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// Template by Ali A. Kooshesh on 1/27/21.
3+
// Additional Code by Garret Mook on 02/06/21.
4+
5+
6+
//READER/LEXICAL ANALZYER
7+
// Definition Of Datatype
8+
9+
#ifndef PROJECT1PHASE1_TOKENIZER_HPP
10+
#define PROJECT1PHASE1_TOKENIZER_HPP
11+
12+
#include <string>
13+
#include <fstream> //used for reading writing files
14+
#include "Token.hpp"
15+
16+
class Tokenizer {
17+
18+
public:
19+
//Operations(functions) that we perform on the datatype
20+
Tokenizer(std::string);
21+
22+
Token getToken();
23+
24+
private:
25+
int lineNumber, charPosition;
26+
std::string inputFileName;
27+
std::ifstream inputStream; //declare input-stream variable
28+
29+
bool charOfInterest(char c);
30+
};
31+
32+
#endif //PROJECT1PHASE1_TOKENIZER_HPP

imagesForReadme/exampleInput.PNG

10.1 KB
Loading

imagesForReadme/exampleOutput.PNG

8.13 KB
Loading

0 commit comments

Comments
 (0)