Advertisement
ApemeM

Untitled

May 15th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. //f(a123,bweqwe,asd223,asdasd , aad, aasdr32,sadasdq3)
  2. //19.49 start
  3. //20.04 end
  4. #include <iostream>
  5. #include <cctype>
  6.  
  7. const char SIGN_FUNC = 'f';
  8. const char SIGN_L_BRACKET = '(';
  9. const char SIGN_R_BRACKET = ')';
  10. const char SIGN_ZAP = ',';
  11. const char SIGN_SPACE = ' ';
  12.  
  13. enum type_of_lex {
  14. LEX_FUNC,
  15. LEX_L_BRACKET,
  16. LEX_R_BRACKET,
  17. LEX_ZAP,
  18. LEX_SPACE,
  19. LEX_IDENT
  20. };
  21.  
  22. using namespace std;
  23.  
  24. class lexeme_class {
  25. public:
  26. type_of_lex type;
  27. string value;
  28. };
  29.  
  30. typedef lexeme_class *lexeme;
  31. lexeme curlex;
  32.  
  33. void get_lexeme() {
  34. char c;
  35. string value;
  36. cin.get(c);
  37. if (isalpha(c)) {
  38. if (c == SIGN_FUNC) {
  39. curlex->type = LEX_FUNC;
  40. return;
  41. }
  42. while (isalpha(c) || isdigit(c)) {
  43. value += c;
  44. cin.get(c);
  45. }
  46. cin.putback(c);
  47. curlex->type = LEX_IDENT;
  48. return;
  49. } else {
  50. value = c;
  51. }
  52.  
  53. switch (value[0]) {
  54. case SIGN_L_BRACKET:
  55. curlex->type = LEX_L_BRACKET;
  56. break;
  57. case SIGN_R_BRACKET:
  58. curlex->type = LEX_R_BRACKET;
  59. break;
  60. case SIGN_ZAP:
  61. curlex->type = LEX_ZAP;
  62. break;
  63. case SIGN_SPACE:
  64. curlex->type = LEX_SPACE;
  65. break;
  66. default:
  67. throw 1;
  68. }
  69. }
  70.  
  71. void sequence() {
  72. get_lexeme();
  73. while (curlex->type == LEX_SPACE) {
  74. get_lexeme();
  75. }
  76. if (curlex->type != LEX_IDENT) {
  77. throw 2;
  78. } else {
  79. get_lexeme();
  80. while (curlex->type == LEX_SPACE) {
  81. get_lexeme();
  82. }
  83. if (curlex->type == LEX_R_BRACKET) {
  84. return;
  85. } else if (curlex->type == LEX_ZAP) {
  86. sequence();
  87. } else {
  88. throw 2;
  89. }
  90. }
  91. }
  92.  
  93. void start() {
  94. if (curlex->type == LEX_FUNC) {
  95. get_lexeme();
  96. if (curlex->type == LEX_L_BRACKET) {
  97. sequence();
  98. if (curlex->type == LEX_R_BRACKET) {
  99. return;
  100. } else {
  101. throw 2;
  102. }
  103. } else {
  104. throw 2;
  105. }
  106. } else {
  107. throw 2;
  108. }
  109.  
  110. }
  111.  
  112. int main() {
  113. curlex = new lexeme_class;
  114. try {
  115. get_lexeme();
  116. start();
  117. } catch (int error) {
  118. switch (error) {
  119. case 1:
  120. cout << "Lexical error" << endl;
  121. return 1;
  122. case 2:
  123. cout << "Syntax error" << endl;
  124. return 1;
  125. }
  126. }
  127. cout << "ok" << endl;
  128. return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement