Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <locale>
- #include <unordered_set>
- using namespace std;
- unordered_set<string> keywords = {
- "procedure",
- "real",
- "var",
- "string",
- "begin",
- "if",
- "then",
- "else",
- "delete",
- "end",
- };
- unordered_set<string> delims = {
- ",",
- "+",
- ":",
- ")",
- "(",
- ";",
- "[",
- "]",
- "<",
- ">",
- "'",
- };
- enum type
- {
- keyword,
- delim,
- delim2,
- ident,
- number,
- str,
- error,
- };
- struct Lexem {
- Lexem(const string &value, type type, int line, int number)
- : value(value)
- , type(type)
- , line(line)
- , number(number)
- {}
- string value;
- type type;
- int line;
- int number;
- };
- int main()
- {
- int numberind = 0;
- vector<Lexem> lexems;
- fstream f("code.txt", ios::in);
- fstream p("runtime_output.txt", ios::out);
- string temp;
- int linecount = 1;
- p << "this is start" << endl;
- while (!f.eof())
- {
- while (isspace(f.peek()))
- {
- if (f.peek() == '\n')
- {
- linecount++; p << "end of line" << endl;
- }
- f.get();
- }
- if (f.eof())
- break;
- if (isalpha(f.peek()))
- {
- do {
- temp += tolower(f.get());
- } while (isalpha(f.peek()));
- type t = ident;
- if (keywords.find(temp) != keywords.end()) {
- t = keyword;
- }
- lexems.push_back(Lexem(temp, t, linecount, number));
- p << temp << " line:" << linecount << endl;
- }
- else if (isdigit(f.peek()))
- {
- do {
- temp += f.get();
- } while (isdigit(f.peek()));
- lexems.push_back(Lexem(temp, number, linecount, number));
- }
- else //массив делимторов
- {
- if (f.peek() == ':')
- {
- type t = delim;
- temp += f.get();
- if (f.peek() == '=') {
- temp += f.get();
- t = delim2;
- }
- lexems.push_back(Lexem(temp, t, linecount, number));
- }
- else if (f.peek() == '<')
- {
- type t = delim;
- temp += f.get();
- if (f.peek() == '>') {
- temp += f.get();
- t = delim2;
- }
- lexems.push_back(Lexem(temp, t, linecount, number));
- }
- else if (f.peek() == '\'')
- {
- lexems.push_back(Lexem("'", delim, linecount, number));
- f.get();
- while (f.peek() != '\'')
- {
- temp += f.get();
- }
- lexems.push_back(Lexem(temp, str, linecount, number));
- lexems.push_back(Lexem("'", delim, linecount, number));
- f.get();
- }
- else
- {
- type t = error;
- temp += f.get();
- if (delims.find(temp) != delims.end()) {
- t = delim;
- }
- else {
- p << "We found error! Line:" << linecount << " UNKNOWN SIMBOL!!!!" << temp << endl;
- }
- lexems.push_back(Lexem(temp, t, linecount, number));
- }
- }
- temp.clear();
- }
- for (int i = 0; i < lexems.size(); i++) {
- cout << lexems[i].type << '\t' << lexems[i].value << endl;
- }
- cout << "0=keywords, 1=delimiters, 2=double delimiters, 3=identificator, 4=constants, 5=strings, 6=errors" << endl;
- f.close(); //закрывать файловую переменную. файл нам еще пригодится!
- int inditerator = 0;
- for (int i = 0; i < lexems.size(); i++) {
- if (lexems[i].type == 3)
- cout <<'#'<< inditerator++<< '\t' << lexems[i].value << endl;
- }
- int groupterator = 0;
- cout << "___________________" << endl;
- for (int i = 0; i < lexems.size(); i++) {
- if (lexems[i].type == 4)
- cout<<"#"<<groupterator++<< '\t' << lexems[i].value << endl;
- }
- int iicounter = 0;
- for (int i = 0; i < lexems.size(); i++) {
- if (lexems[i].type == 5)
- cout<<'#'<<groupterator++<< '\t' <<"'" <<lexems[i].value<<"'" << endl;
- }
- cout << "_________UNKNOWN SYMBOLS__________" << endl;
- for (int i = 0; i < lexems.size(); i++) {
- if (lexems[i].type == 6)
- cout << lexems[i].type << '\t' << lexems[i].line << '\t' << lexems[i].value << endl;
- }
- int icounter = 0;
- cout << "__________ento keyword_________" << endl;
- for (int i = 0; i < lexems.size(); i++) {
- if (lexems[i].type == 0)
- cout <<"#"<<icounter++<<" "<< '\t' << lexems[i].value << endl;
- }
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement