-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
415 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
cmake_minimum_required(VERSION 3.17) | ||
project(Project1Phase1) | ||
|
||
set(CMAKE_CXX_STANDARD 17) | ||
|
||
add_executable(Project1Phase1 main.cpp Tokenizer.cpp Tokenizer.hpp Token.cpp Token.hpp Stack.cpp Stack.hpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// Created by gmook on 2/6/2021. | ||
// | ||
|
||
#include "Stack.hpp" | ||
|
||
void Stack::push(int n) { | ||
stack.push_back(n); | ||
} | ||
int Stack::top() { | ||
return stack.at(stack.size()-1); | ||
} | ||
void Stack::pop() { | ||
stack.pop_back(); | ||
} | ||
bool Stack::empty() { | ||
return stack.empty(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// Created by gmook on 2/6/2021. | ||
// | ||
|
||
#ifndef PROJECT1PHASE1_STACK_HPP | ||
#define PROJECT1PHASE1_STACK_HPP | ||
|
||
|
||
#include <vector> | ||
|
||
class Stack { | ||
public: | ||
|
||
void push(int n); | ||
int top(); | ||
void pop(); | ||
bool empty(); | ||
|
||
private: | ||
std::vector<int> stack; | ||
|
||
}; | ||
|
||
|
||
|
||
#endif //PROJECT1PHASE1_STACK_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// | ||
// Template by Ali A. Kooshesh on 1/27/21. | ||
// Additional Code by Garret Mook on 02/06/21. | ||
|
||
|
||
#include <iostream> | ||
#include <iomanip> | ||
#include "Token.hpp" | ||
|
||
Token::Token(int line, int pos): _isOpenTag{false}, | ||
_isCloseTag{false}, | ||
_isCloseAngleBracket{false}, | ||
_isCloseStandAloneTag{false}, | ||
_tagName{""}, | ||
_eof{false}, | ||
_lineNumber{line}, | ||
_charPos{pos}, | ||
_eol{false}, | ||
_isSlash{false} {} | ||
|
||
bool &Token::isOpenTag() { return _isOpenTag; } | ||
bool &Token::isCloseTag() { return _isCloseTag; } | ||
|
||
bool &Token::isCloseAngleBracket() { return _isCloseAngleBracket; } | ||
|
||
bool &Token::endOfFile() { return _eof; } | ||
bool &Token::isCloseStandAloneTag() { return _isCloseStandAloneTag; } | ||
bool &Token::isSlash() { return _isSlash; } | ||
|
||
std::string Token::tagName() { return _tagName; } | ||
|
||
|
||
bool &Token::eol(){ | ||
return _eol; | ||
|
||
} | ||
|
||
void Token::makeOpenTag(std::string name) { | ||
_tagName = name; | ||
isOpenTag() = true; | ||
} | ||
std::string Token::makeOpenTag() { | ||
return _tagName; | ||
} | ||
void Token::makeCloseTag(std::string name) { | ||
_tagName = name; | ||
|
||
isCloseTag() = true; | ||
} | ||
/* | ||
void Token::print() { | ||
std::cout << "[" << std::setw(2) << _lineNumber << ", " << std::setw(3) << _charPos << "] "; | ||
if(isOpenTag()) { | ||
std::cout << "<" << tagName() << std::endl; | ||
} else if(isCloseStandAloneTag()) { | ||
std::cout << "/>" << std::endl; | ||
} else if(isCloseAngleBracket()) { | ||
std::cout << ">" << std::endl; | ||
} else if(isCloseTag()) { | ||
std::cout << "</" << tagName() << std::endl; | ||
} else if(isSlash() ) { | ||
} | ||
else {// more else if's before this else | ||
std::cout << "Unknown" << std::endl; | ||
} | ||
} | ||
*/ | ||
|
||
|
||
|
||
void Token::print() { | ||
|
||
if(isOpenTag()) { | ||
std::cout << "[" << std::setw(2) << _lineNumber << ", " << std::setw(3) << _charPos << "] "; | ||
std::cout << "<" << tagName() << std::endl; | ||
} else if(isCloseStandAloneTag()) { | ||
std::cout << "[" << std::setw(2) << _lineNumber << ", " << std::setw(3) << _charPos << "] "; | ||
std::cout << "/>" << std::endl; | ||
} else if(isCloseAngleBracket()) { | ||
std::cout << "[" << std::setw(2) << _lineNumber << ", " << std::setw(3) << _charPos << "] "; | ||
std::cout << ">" << std::endl; | ||
} else if(isCloseTag()) { | ||
std::cout << "[" << std::setw(2) << _lineNumber << ", " << std::setw(3) << _charPos << "] "; | ||
std::cout << "</" << tagName() << std::endl; | ||
} else if(isSlash() ) { | ||
|
||
} | ||
else {// more else if's before this else | ||
std::cout << "Unknown" << std::endl; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// Template by Ali A. Kooshesh on 1/27/21. | ||
// Additional Code by Garret Mook on 02/06/21. | ||
|
||
#ifndef PROJECT1PHASE1_TOKEN_HPP | ||
#define PROJECT1PHASE1_TOKEN_HPP | ||
|
||
#include <string> | ||
|
||
class Token { | ||
public: | ||
Token(int, int); | ||
bool &isOpenTag(); | ||
bool &isCloseTag(); | ||
|
||
bool &isCloseAngleBracket(); | ||
|
||
bool &endOfFile(); | ||
bool &isCloseStandAloneTag(); | ||
|
||
|
||
std::string tagName(); | ||
//Self Added | ||
|
||
void makeOpenTag(std::string); | ||
void makeCloseTag(std::string); | ||
//Self Added | ||
std::string makeOpenTag(); | ||
bool &isSlash(); | ||
|
||
void print(); | ||
|
||
|
||
|
||
bool &eol(); | ||
|
||
private: | ||
bool _isOpenTag, _isCloseTag, _isCloseAngleBracket, | ||
_eof, _isCloseStandAloneTag, _eol, _isSlash; | ||
std::string _tagName; | ||
int _lineNumber, _charPos; | ||
|
||
|
||
}; | ||
|
||
|
||
#endif //PROJECT1PHASE1_TOKEN_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Template by Ali A. Kooshesh on 1/27/21. | ||
// Additional Code by Garret Mook on 02/06/21. | ||
|
||
//READER/LEXICAL ANALYZER | ||
//Implemenation Of Operations | ||
|
||
#include <iostream> | ||
#include "Tokenizer.hpp" | ||
#include <string> | ||
#include <vector> | ||
// | ||
#include <cstdio> | ||
#include <cmath> | ||
#include <stdio.h> | ||
#include <ctype.h> | ||
|
||
//Initializer List | ||
Tokenizer::Tokenizer(std::string name): lineNumber{1}, | ||
charPosition{1}, | ||
inputFileName{name} { | ||
inputStream.open(inputFileName, std::ios::in); // open the input file. We will make sure that it is open in getToken. | ||
} | ||
//This defines what a Character of interest is to the reader. | ||
bool Tokenizer::charOfInterest(char c) { | ||
// is c the initial (or the sole) character of a token? | ||
return c == '<' || c == '>' || c == '/'; | ||
} | ||
|
||
Token Tokenizer::getToken() { | ||
char c; | ||
//Checks to see if the file we are trying to read is open | ||
if( ! inputStream.is_open()) { | ||
std::cout << "Tokenizer::getToken() called with a stream that is not open." << std::endl; | ||
std::cout << "Make sure that " << inputFileName << " exists and is readable. Terminating."; | ||
exit(2); | ||
} | ||
|
||
//GM: This while loop executes until we find a charOfInterest | ||
while( inputStream.get(c) && ! charOfInterest(c) ) { | ||
// keep track of the line number and the character position here. | ||
if (c == '\n') { | ||
lineNumber++; | ||
|
||
} | ||
else { | ||
charPosition++; | ||
} | ||
|
||
} | ||
|
||
|
||
//GM: After loop stops this executes | ||
Token token(lineNumber, charPosition); | ||
|
||
if( inputStream.eof() ) { | ||
token.endOfFile() = true; | ||
} | ||
// if < is found | ||
else if( c == '<' && inputStream.peek() != '/' && inputStream.peek() != '>') { | ||
if( isalpha(inputStream.peek()) ) { | ||
charPosition++; | ||
std::string needthisforPeek; | ||
while (inputStream.get(c) && isalpha(c) && !isspace(c) && inputStream.peek() != '/') { | ||
needthisforPeek = needthisforPeek + c; | ||
charPosition++; | ||
} | ||
|
||
inputStream.putback(c); | ||
token.makeOpenTag(needthisforPeek); | ||
return token; | ||
} | ||
} | ||
|
||
/* | ||
// if /> is found | ||
else if (c == '/' && inputStream.peek() == '>') { | ||
inputStream.get(c); | ||
token.isCloseStandAloneTag() = true; | ||
} | ||
*/ | ||
|
||
|
||
else if (c == '/') { | ||
if (c == '/' && isalpha(inputStream.peek()) ) { | ||
token.isSlash() = true; | ||
charPosition++; | ||
|
||
} else if (c == '/' && inputStream.peek() == '>') { | ||
inputStream.get(c); | ||
token.isCloseStandAloneTag() = true; | ||
charPosition++; | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
// if > is found | ||
else if( c == '>' ) { | ||
token.isCloseAngleBracket() = true; | ||
charPosition++; | ||
} | ||
// if <tagname is found then it is NOT a closed standalone tag | ||
else if (isalpha(inputStream.peek())) { | ||
token.isCloseStandAloneTag() = false; | ||
charPosition++; | ||
} | ||
|
||
//IF </ is found | ||
else if ( inputStream.good() && c == '<' && inputStream.peek() == '/') { | ||
inputStream.get(c); | ||
charPosition++; | ||
std::string neededToReadPeek; | ||
if (c== '/' && isalpha(inputStream.peek())) { | ||
while (inputStream.good() && inputStream.get(c) && isalpha(c)) { | ||
neededToReadPeek += c; | ||
charPosition++; | ||
} | ||
inputStream.putback(c); | ||
token.makeCloseTag(neededToReadPeek); | ||
charPosition++; | ||
return token; | ||
} | ||
} | ||
|
||
else { | ||
std::cout << "Unknown Character: " << c; | ||
exit(1); | ||
} | ||
|
||
return token; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// Template by Ali A. Kooshesh on 1/27/21. | ||
// Additional Code by Garret Mook on 02/06/21. | ||
|
||
|
||
//READER/LEXICAL ANALZYER | ||
// Definition Of Datatype | ||
|
||
#ifndef PROJECT1PHASE1_TOKENIZER_HPP | ||
#define PROJECT1PHASE1_TOKENIZER_HPP | ||
|
||
#include <string> | ||
#include <fstream> //used for reading writing files | ||
#include "Token.hpp" | ||
|
||
class Tokenizer { | ||
|
||
public: | ||
//Operations(functions) that we perform on the datatype | ||
Tokenizer(std::string); | ||
|
||
Token getToken(); | ||
|
||
private: | ||
int lineNumber, charPosition; | ||
std::string inputFileName; | ||
std::ifstream inputStream; //declare input-stream variable | ||
|
||
bool charOfInterest(char c); | ||
}; | ||
|
||
#endif //PROJECT1PHASE1_TOKENIZER_HPP |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.