/
davinchi2kxx
/
projectSeminarTask2
Обзор
Документация
Войти
/
davinchi2kxx
/
projectSeminarTask2
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
main.cpp
115 строк
3 KB
davinchi
first_commit
17 фев 2025, 23:26
17 фев 2025, 23:26
037596c
Код
Авторство
О чём код?
#include <iostream> #include <string> #include <vector> #include <cctype> using namespace std; vector<string> tokenize(const string &input) { vector<string> tokens; string current; for (char ch : input) { if (isalnum(ch)) { current += ch; } else { if (!current.empty()) { tokens.push_back(current); current.clear(); } if (!isspace(ch)) { tokens.push_back(string(1, ch)); } } } if (!current.empty()) { tokens.push_back(current); } return tokens; } bool isIdentifier(const string &str) { if (str.empty() || !isalpha(str[0])) return false; for (char ch : str) if (!isalnum(ch)) return false; return true; } bool isValidType(const string &str) { string types[] = {"integer", "long", "variant", "string"}; for (const string &t : types) if (str == t) return true; return false; } bool fsm(const string &input) { enum State { START, DIM, IDENTIFIER, COMMA, AS, TYPE, END }; State state = START; vector<string> tokens = tokenize(input); for (const string &token : tokens) { switch (state) { case START: case END: if (token == "dim") { state = DIM; } else { return false; } break; case DIM: if (isIdentifier(token)) { state = IDENTIFIER; } else { return false; } break; case IDENTIFIER: if (token == ",") { state = COMMA; } else if (token == "as") { state = AS; } else { return false; } break; case COMMA: if (isIdentifier(token)) { state = IDENTIFIER; } else { return false; } break; case AS: if (isValidType(token)) { state = TYPE; } else { return false; } break; case TYPE: if (token == ",") { state = COMMA; } else if (token == ";") { state = END; } else { return false; } break; } } return state == END || state == TYPE; } int main() { string input; cout << "Enter a declaration: "; getline(cin, input); if (fsm(input)) { cout << "Valid declaration" << endl; } else { cout << "Invalid declaration" << endl; } return 0; }