Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //f(a123,bweqwe,asd223,asdasd , aad, aasdr32,sadasdq3)
- //19.49 start
- //20.04 end
- #include <iostream>
- #include <cctype>
- const char SIGN_FUNC = 'f';
- const char SIGN_L_BRACKET = '(';
- const char SIGN_R_BRACKET = ')';
- const char SIGN_ZAP = ',';
- const char SIGN_SPACE = ' ';
- enum type_of_lex {
- LEX_FUNC,
- LEX_L_BRACKET,
- LEX_R_BRACKET,
- LEX_ZAP,
- LEX_SPACE,
- LEX_IDENT
- };
- using namespace std;
- class lexeme_class {
- public:
- type_of_lex type;
- string value;
- };
- typedef lexeme_class *lexeme;
- lexeme curlex;
- void get_lexeme() {
- char c;
- string value;
- cin.get(c);
- if (isalpha(c)) {
- if (c == SIGN_FUNC) {
- curlex->type = LEX_FUNC;
- return;
- }
- while (isalpha(c) || isdigit(c)) {
- value += c;
- cin.get(c);
- }
- cin.putback(c);
- curlex->type = LEX_IDENT;
- return;
- } else {
- value = c;
- }
- switch (value[0]) {
- case SIGN_L_BRACKET:
- curlex->type = LEX_L_BRACKET;
- break;
- case SIGN_R_BRACKET:
- curlex->type = LEX_R_BRACKET;
- break;
- case SIGN_ZAP:
- curlex->type = LEX_ZAP;
- break;
- case SIGN_SPACE:
- curlex->type = LEX_SPACE;
- break;
- default:
- throw 1;
- }
- }
- void sequence() {
- get_lexeme();
- while (curlex->type == LEX_SPACE) {
- get_lexeme();
- }
- if (curlex->type != LEX_IDENT) {
- throw 2;
- } else {
- get_lexeme();
- while (curlex->type == LEX_SPACE) {
- get_lexeme();
- }
- if (curlex->type == LEX_R_BRACKET) {
- return;
- } else if (curlex->type == LEX_ZAP) {
- sequence();
- } else {
- throw 2;
- }
- }
- }
- void start() {
- if (curlex->type == LEX_FUNC) {
- get_lexeme();
- if (curlex->type == LEX_L_BRACKET) {
- sequence();
- if (curlex->type == LEX_R_BRACKET) {
- return;
- } else {
- throw 2;
- }
- } else {
- throw 2;
- }
- } else {
- throw 2;
- }
- }
- int main() {
- curlex = new lexeme_class;
- try {
- get_lexeme();
- start();
- } catch (int error) {
- switch (error) {
- case 1:
- cout << "Lexical error" << endl;
- return 1;
- case 2:
- cout << "Syntax error" << endl;
- return 1;
- }
- }
- cout << "ok" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement