Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- #include "lex.h"
- using namespace std;
- Lex getNextToken(istream &in, int &linenum);
- Token getKeywords(string str, Token orig);
- ostream &operator<<(ostream &out, const Lex &tok);
- string printnamedex(const Lex &tok);
- ostream &operator<<(ostream &out, const Lex &tok) {
- if (tok.GetToken() == ID || tok.GetToken() == INT || tok.GetToken() == STR || tok.GetToken() == ERR) {
- out << printnamedex(tok) << tok.GetLexeme() << ")";
- } else {
- out << printnamedex(tok);
- }
- return out;
- }
- string printnamedex(const Lex &tok) {
- string retval;
- switch (tok.GetToken()) {
- case ID:
- retval = "ID(";
- break;
- case INT:
- retval = "INT(";
- break;
- case STR:
- retval = "STR(";
- break;
- case PRINT:
- retval = "PRINT";
- break;
- case ERR:
- retval = "Error on line ";
- retval += to_string(tok.GetLinenum()+1);
- retval += " (";
- break;
- case LET:
- retval = "LET";
- break;
- case IF:
- retval = "IF";
- break;
- case LOOP:
- retval = "LOOP";
- break;
- case BEGIN:
- retval = "BEGIN";
- break;
- case END:
- retval = "END";
- break;
- case PLUS:
- retval = "PLUS";
- break;
- case MINUS:
- retval = "MINUS";
- break;
- case STAR:
- retval = "STAR";
- break;
- case SLASH:
- retval = "SLASH";
- break;
- case BANG:
- retval = "BANG";
- break;
- case LPAREN:
- retval = "LPAREN";
- break;
- case RPAREN:
- retval = "RPAREN";
- break;
- case SC:
- retval = "SC";
- break;
- case DONE:
- retval = "DONE";
- break;
- }
- return retval;
- }
- Lex getNextToken(istream &in, int &linenum) {
- char newchar = '\0';
- string oper = ";+-!\\*()";
- Token checktoken = ERR;
- string checklex = "";
- int checkline = -1;
- bool flush = false;
- Lex nextLex;
- while (true) {
- in.get(newchar);
- if (in.eof()) {
- nextLex = Lex(DONE, "", linenum);
- return nextLex;
- }
- if (checktoken == STR) {
- if (newchar == '\\') {
- char escaped;
- in.get(escaped);
- if (escaped == 'n') {
- checklex += '\n';
- } else
- checklex += escaped;
- } else if (newchar == '\"') {
- checkline = linenum;
- flush = true;
- } else if (newchar == EOF || newchar == '\n') {
- checktoken = ERR;
- checkline = linenum;
- flush = true;
- } else
- checklex += newchar;
- } else if (checktoken == INT) {
- if (isdigit(newchar))
- checklex += newchar;
- else {
- in.unget();
- checkline = linenum;
- flush = true;
- }
- } else if (checktoken == ID) {
- if (isalnum(newchar))
- checklex += newchar;
- else {
- checkline = linenum;
- flush = true;
- }
- } else if (checktoken == ERR) {
- if (isdigit(newchar)) {
- checklex += newchar;
- checktoken = INT;
- continue;
- }
- if (isalpha(newchar)) {
- checklex += newchar;
- checktoken = ID;
- continue;
- }
- if(isspace(newchar) && newchar != '\n')
- continue;
- switch (newchar) {
- case '\"':
- checktoken = STR;
- break;
- case '/':
- char nextchar;
- nextchar = in.peek();
- if (nextchar == '/') {
- string s;
- while (true) {
- char nextchar;
- in.get(nextchar);
- if (nextchar == '\n') {
- linenum++;
- break;
- }
- };
- } else {
- checktoken = SLASH;
- checkline = linenum;
- flush = true;
- }
- break;
- case ' ':
- break;
- case '\n':
- linenum++;
- break;
- case EOF:
- checktoken = DONE;
- checkline = linenum;
- flush = true;
- break;
- case '+':
- checktoken = PLUS;
- checkline = linenum;
- flush = true;
- break;
- case '-':
- checktoken = MINUS;
- checkline = linenum;
- flush = true;
- break;
- case '*':
- checktoken = STAR;
- checkline = linenum;
- flush = true;
- break;
- case '!':
- checktoken = BANG;
- checkline = linenum;
- flush = true;
- break;
- case '(':
- checktoken = LPAREN;
- checkline = linenum;
- flush = true;
- break;
- case ')':
- checktoken = RPAREN;
- checkline = linenum;
- flush = true;
- break;
- case ';':
- checktoken = SC;
- checkline = linenum;
- flush = true;
- break;
- default:
- checktoken = ERR;
- checklex += newchar;
- checkline = linenum;
- flush = true;
- break;
- }
- }
- if (flush) {
- checktoken = getKeywords(checklex, checktoken);
- nextLex = Lex(checktoken, checklex, checkline);
- return nextLex;
- }
- }
- }
- Token getKeywords(string str, Token orig) {
- if (orig == ID) {
- if (str.compare("print") == 0) {
- return PRINT;
- } else if (str.compare("if") == 0) {
- return IF;
- } else if (str.compare("loop") == 0) {
- return LOOP;
- } else if (str.compare("let") == 0) {
- return LET;
- } else if (str.compare("begin") == 0) {
- return BEGIN;
- } else if (str.compare("end") == 0) {
- return END;
- }
- }
- return orig;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement