fork download
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. // Returns 'true' if the character is a DELIMITER.
  10. bool isDelimiter(char ch)
  11. {
  12. if (ch == ' ' || ch == '+' || ch == '-' || ch == '*' ||
  13. ch == '/' || ch == ',' || ch == ';' || ch == '>' ||
  14. ch == '<' || ch == '=' || ch == '(' || ch == ')' ||
  15. ch == '[' || ch == ']' || ch == '{' || ch == '}')
  16. return (true);
  17. return (false);
  18. }
  19.  
  20. // Returns 'true' if the character is an OPERATOR.
  21. bool isOperator(char ch)
  22. {
  23. if (ch == '+' || ch == '-' || ch == '*' ||
  24. ch == '/' || ch == '>' || ch == '<' ||
  25. ch == '=')
  26. return (true);
  27. return (false);
  28. }
  29.  
  30. // Returns 'true' if the string is a VALID IDENTIFIER.
  31. bool validIdentifier(char* str)
  32. {
  33. if (str[0] == '0' || str[0] == '1' || str[0] == '2' ||
  34. str[0] == '3' || str[0] == '4' || str[0] == '5' ||
  35. str[0] == '6' || str[0] == '7' || str[0] == '8' ||
  36. str[0] == '9' || isDelimiter(str[0]) == true)
  37. return (false);
  38. return (true);
  39. }
  40.  
  41.  
  42. // Returns 'true' if the string is a KEYWORD.
  43. bool isKeyword(char* str)
  44. {
  45. if (!strcmp(str, "if") || !strcmp(str, "else") ||
  46. !strcmp(str, "while") || !strcmp(str, "do") ||
  47. !strcmp(str, "break") ||
  48. !strcmp(str, "continue") || !strcmp(str, "int")
  49. || !strcmp(str, "double") || !strcmp(str, "float")
  50. || !strcmp(str, "return") || !strcmp(str, "char")
  51. || !strcmp(str, "case") || !strcmp(str, "char")
  52. || !strcmp(str, "sizeof") || !strcmp(str, "long")
  53. || !strcmp(str, "short") || !strcmp(str, "typedef")
  54. || !strcmp(str, "switch") || !strcmp(str, "unsigned")
  55. || !strcmp(str, "void") || !strcmp(str, "static")
  56. || !strcmp(str, "struct") || !strcmp(str, "goto"))
  57. return (true);
  58. return (false);
  59. }
  60.  
  61. // Returns 'true' if the string is an INTEGER.
  62. bool isInteger(char* str)
  63. {
  64. int i, len = strlen(str);
  65.  
  66. if (len == 0)
  67. return (false);
  68. for (int i = 0; i<len; i++) {
  69. if (str[i] != '0' &&str[i] != '1' &&str[i] != '2'
  70. &&str[i] != '3' &&str[i] != '4' &&str[i] != '5'
  71. &&str[i] != '6' &&str[i] != '7' &&str[i] != '8'
  72. &&str[i] != '9' || (str[i] == '-' &&i> 0))
  73. return (false);
  74. }
  75. return (true);
  76. }
  77.  
  78. // Returns 'true' if the string is a REAL NUMBER.
  79. bool isRealNumber(char* str)
  80. {
  81. int i, len = strlen(str);
  82. bool hasDecimal = false;
  83.  
  84. if (len == 0)
  85. return (false);
  86. for (int i = 0; i<len; i++) {
  87. if (str[i] != '0' &&str[i] != '1' &&str[i] != '2'
  88. &&str[i] != '3' &&str[i] != '4' &&str[i] != '5'
  89. &&str[i] != '6' &&str[i] != '7' &&str[i] != '8'
  90. &&str[i] != '9' &&str[i] != '.' ||
  91. (str[i] == '-' &&i> 0))
  92. return (false);
  93. if (str[i] == '.')
  94. hasDecimal = true;
  95. }
  96. return (hasDecimal);
  97. }
  98.  
  99. // Extracts the SUBSTRING.
  100. char* subString(char* str, int left, int right)
  101. {
  102. int i;
  103. char* subStr = (char*)malloc(
  104. sizeof(char) * (right - left + 2));
  105.  
  106. for (i = left; i<= right; i++)
  107. subStr[i - left] = str[i];
  108. subStr[right - left + 1] = '\0';
  109. return (subStr);
  110. }
  111.  
  112. // Parsing the input STRING.
  113. void parse(char* str)
  114. {
  115. int left = 0, right = 0;
  116. int len = strlen(str);
  117.  
  118. while (right <= len&& left <= right) {
  119. if (isDelimiter(str[right]) == false)
  120. right++;
  121.  
  122. if (isDelimiter(str[right]) == true && left == right) {
  123. if (isOperator(str[right]) == true)
  124. printf("'%c' IS AN OPERATOR\n", str[right]);
  125.  
  126. right++;
  127. left = right;
  128. } else if (isDelimiter(str[right]) == true &&left != right
  129. || (right == len&&left != right)) {
  130. char* subStr = subString(str, left, right - 1);
  131.  
  132. if (isKeyword(subStr) == true)
  133. printf("'%s' IS A KEYWORD\n", subStr);
  134.  
  135. else if (isInteger(subStr) == true)
  136. printf("'%s' IS AN INTEGER\n", subStr);
  137.  
  138. else if (isRealNumber(subStr) == true)
  139. printf("'%s' IS A REAL NUMBER\n", subStr);
  140.  
  141. else if (validIdentifier(subStr) == true
  142. &&isDelimiter(str[right - 1]) == false)
  143. printf("'%s' IS A VALID IDENTIFIER\n", subStr);
  144.  
  145. else if (validIdentifier(subStr) == false
  146. &&isDelimiter(str[right - 1]) == false)
  147. printf("'%s' IS NOT A VALID IDENTIFIER\n", subStr);
  148. left = right;
  149. }
  150. }
  151. return;
  152. }
  153.  
  154. // DRIVER FUNCTION
  155. int main()
  156. {
  157. // maximum legth of string is 100 here
  158. char str[100] = "int a = b + 1c; ";
  159.  
  160. parse(str); // calling the parse function
  161.  
  162. return (0);
  163. }
  164.  
  165.  
Success #stdin #stdout 0.01s 5436KB
stdin
Standard input is empty
stdout
'int' IS A KEYWORD
'a' IS A VALID IDENTIFIER
'=' IS AN OPERATOR
'b' IS A VALID IDENTIFIER
'+' IS AN OPERATOR
'1c' IS NOT A VALID IDENTIFIER