Advertisement
Hridoy3000

token

Dec 11th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. // Returns 'true' if the character is a DELIMITER.
  7. bool isDelimiter(char ch)
  8. {
  9. if (ch == ' ' || ch == '+' || ch == '-' || ch == '*' ||
  10. ch == '/' || ch == ',' || ch == ';' || ch == '>' ||
  11. ch == '<' || ch == '=' || ch == '(' || ch == ')' ||
  12. ch == '[' || ch == ']' || ch == '{' || ch == '}')
  13. return (true);
  14. return (false);
  15. }
  16.  
  17. // Returns 'true' if the character is an OPERATOR.
  18. bool isOperator(char ch)
  19. {
  20. if (ch == '+' || ch == '-' || ch == '*' ||
  21. ch == '/' || ch == '>' || ch == '<' ||
  22. ch == '=')
  23. return (true);
  24. return (false);
  25. }
  26.  
  27. // Returns 'true' if the string is a VALID IDENTIFIER.
  28. bool validIdentifier(char* str)
  29. {
  30. if (str[0] == '0' || str[0] == '1' || str[0] == '2' ||
  31. str[0] == '3' || str[0] == '4' || str[0] == '5' ||
  32. str[0] == '6' || str[0] == '7' || str[0] == '8' ||
  33. str[0] == '9' || isDelimiter(str[0]) == true)
  34. return (false);
  35. return (true);
  36. }
  37.  
  38. // Returns 'true' if the string is a KEYWORD.
  39. bool isKeyword(char* str)
  40. {
  41. if (!strcmp(str, "if") || !strcmp(str, "else") ||
  42. !strcmp(str, "while") || !strcmp(str, "do") ||
  43. !strcmp(str, "break") ||
  44. !strcmp(str, "continue") || !strcmp(str, "int")
  45. || !strcmp(str, "double") || !strcmp(str, "float")
  46. || !strcmp(str, "return") || !strcmp(str, "char")
  47. || !strcmp(str, "case") || !strcmp(str, "char")
  48. || !strcmp(str, "sizeof") || !strcmp(str, "long")
  49. || !strcmp(str, "short") || !strcmp(str, "typedef")
  50. || !strcmp(str, "switch") || !strcmp(str, "unsigned")
  51. || !strcmp(str, "void") || !strcmp(str, "static")
  52. || !strcmp(str, "struct") || !strcmp(str, "goto"))
  53. return (true);
  54. return (false);
  55. }
  56.  
  57. // Returns 'true' if the string is an INTEGER.
  58. bool isInteger(char* str)
  59. {
  60. int i, len = strlen(str);
  61.  
  62. if (len == 0)
  63. return (false);
  64. for (i = 0; i < len; i++) {
  65. if (str[i] != '0' && str[i] != '1' && str[i] != '2'
  66. && str[i] != '3' && str[i] != '4' && str[i] != '5'
  67. && str[i] != '6' && str[i] != '7' && str[i] != '8'
  68. && str[i] != '9' || (str[i] == '-' && i > 0))
  69. return (false);
  70. }
  71. return (true);
  72. }
  73.  
  74. // Returns 'true' if the string is a REAL NUMBER.
  75. bool isRealNumber(char* str)
  76. {
  77. int i, len = strlen(str);
  78. bool hasDecimal = false;
  79.  
  80. if (len == 0)
  81. return (false);
  82. for (i = 0; i < len; i++) {
  83. if (str[i] != '0' && str[i] != '1' && str[i] != '2'
  84. && str[i] != '3' && str[i] != '4' && str[i] != '5'
  85. && str[i] != '6' && str[i] != '7' && str[i] != '8'
  86. && str[i] != '9' && str[i] != '.' ||
  87. (str[i] == '-' && i > 0))
  88. return (false);
  89. if (str[i] == '.')
  90. hasDecimal = true;
  91. }
  92. return (hasDecimal);
  93. }
  94.  
  95. // Extracts the SUBSTRING.
  96. char* subString(char* str, int left, int right)
  97. {
  98. int i;
  99. char* subStr = (char*)malloc(
  100. sizeof(char) * (right - left + 2));
  101.  
  102. for (i = left; i <= right; i++)
  103. subStr[i - left] = str[i];
  104. subStr[right - left + 1] = '\0';
  105. return (subStr);
  106. }
  107.  
  108.  
  109. void parse(char* str)
  110. {
  111. int left = 0, right = 0;
  112. int len = strlen(str);
  113.  
  114. while (right <= len && left <= right) {
  115. if (isDelimiter(str[right]) == false)
  116. right++;
  117.  
  118. if (isDelimiter(str[right]) == true && left == right) {
  119. if (isOperator(str[right]) == true)
  120. printf("'%c' IS AN OPERATOR\n", str[right]);
  121.  
  122. right++;
  123. left = right;
  124. } else if (isDelimiter(str[right]) == true && left != right
  125. || (right == len && left != right)) {
  126. char* subStr = subString(str, left, right - 1);
  127.  
  128. if (isKeyword(subStr) == true)
  129. printf("'%s' IS A KEYWORD\n\n", subStr);
  130.  
  131. else if (isInteger(subStr) == true)
  132.  
  133. printf("'%s' IS AN INTEGER\n", subStr);
  134.  
  135. else if (isRealNumber(subStr) == true)
  136. printf("'%s' IS A REAL NUMBER\n", subStr);
  137.  
  138. else if (validIdentifier(subStr) == true
  139. && isDelimiter(str[right - 1]) == false){
  140. printf("'%s' IS A VALID IDENTIFIER\n", subStr);
  141.  
  142. if(strcmp(subStr, "a")==0)
  143. {
  144.  
  145. }
  146. else if (strcmp(subStr, "b")==0)
  147. {
  148.  
  149. int v1 = 10;
  150. int v2 = 15;
  151. int c = v1+v2;
  152. if(c>10)
  153. {
  154. printf("Yes\n");
  155. }
  156. else{
  157. printf("Yes\n");
  158. }
  159.  
  160.  
  161. }
  162.  
  163. }
  164.  
  165.  
  166. else if (validIdentifier(subStr) == false
  167. && isDelimiter(str[right - 1]) == false)
  168. printf("'%s' IS NOT A VALID IDENTIFIER\n", subStr);
  169. left = right;
  170. }
  171. }
  172. return;
  173. }
  174.  
  175. int main()
  176. {
  177.  
  178. char str[100] = "int a = 8; float b = 2.5; float c = a+b";
  179.  
  180. parse(str);
  181.  
  182.  
  183. return (0);
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement