Notes
Notes - notes.io |
#include <string>
#include <fstream>
#include <streambuf>
using namespace std;
unsigned long hashWord(string str)
{
unsigned long hash = 5381;
int c;
for (char &c : str) {
hash = ((hash << 5) + hash) + c;
}
return hash;
}
bool isNum(const string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
string getValue(string token) {
string reserved[] = { "read", "write", "if", "then", "else", "end", "repeat", "until", "break", "#", "include", "int", "main" };
char specials[9] = { '+', '-', '*', '/' };
if (token == ">" || token == "<" || token == "==" || token == "<=" || token == ">=") {
return "relational";
}
if (token == "+" || token == "-" || token == "*" || token == "/" || token == "%") {
return "arithmetic";
}
if (token == "{" || token == "}" || token == ";" || token == ":" || token == ":=" || token == "(" || token ==")" || token == "," ) {
return "punctuation";
}
for (int i = 0; i < 9; i++) {
if (token == reserved[i]) {
return "keyword";
}
}
if (isNum(token)) {
return "number";
}
else {
return "identifier";
}
}
int main() {
ifstream t("Text.txt");
string str((std::istreambuf_iterator<char>(t)),
istreambuf_iterator<char>());
cout << "nt***LAB 3***n";
cout << "Name of Token" << " Value of Token " << " Hash Value" << endl;
string alphabets = "abcdefghijklmnopqrstuvwxyz";
string nums = "1234567890";
string stack = "";
int length = str.length();
int i = 0;
Start:
if (i < length) {
char current = str[i];
//cout << current;
if (current == ' ' || current == 'n' || current == 't') {
i++;
goto Start;
}
else if (current == '{') {
i++;
goto Comment;
}
else if (isalpha(current)) {
goto ALPHA;
}
else if (isdigit(current)) {
goto NUMBER;
}
else if (current == ':') {
stack.append(1u, current);
i++;
goto OP;
}
else {
stack.append(1u, current);
goto Final;
}
}
NUMBER:
if (i < length) {
char current = str[i];
//cout << current;
if (isdigit(current)) {
stack.append(1u, current);
if (str[i + 1] == ';') {
goto Final;
}
else {
i++;
}
goto NUMBER;
}
else if (current != ' ' || current != 'n' || current != 't') {
stack.append(1u, current);
goto Final;
}
else {
//stack.append(1u, current);
goto Final;
}
}
ALPHA:
if (i < length) {
char current = str[i];
//cout << current;
if (isalpha(current)) {
stack.append(1u, current);
if (str[i + 1] == ';') {
goto Final;
}
i++;
goto ALPHA;
}
else {
//stack.append(1u, current);
goto Final;
}
}
OP
if (i < length) {
char current = str[i];
//cout << current;
if (current == '=') {
stack.append(1u, current);
goto Final;
}
else {
stack.append(1u, current);
goto Final;
}
}
Comment:
if (i < length) {
char current = str[i];
//cout << current;
if (current == '}') {
i++;
goto Start;
}
else {
i++;
goto Comment;
}
}
Final:
if (i < length) {
cout << stack << "t t" << getValue(stack) << "t t" << hashWord(stack) << endl;
stack = "";
i++;
goto Start;
}
system("pause");
return 0;
}
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team