fork(1) download
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4.  
  5. typedef enum {
  6. TOK_UNKNOWN,
  7. TOK_OPERATOR
  8. } TokenType;
  9.  
  10. typedef struct {
  11. TokenType type;
  12. char lexeme[3];
  13. } Token;
  14.  
  15. static int is_op_start(char c) {
  16. return c=='!' || c=='=' || c=='<' || c=='>' || c=='+' || c=='-' || c=='*' || c=='/';
  17. }
  18.  
  19. Token getToken(const char *s, int *i) {
  20. Token tok;
  21. tok.type = TOK_UNKNOWN;
  22. tok.lexeme[0] = '\0';
  23.  
  24. while (s[*i] && isspace((unsigned char)s[*i])) (*i)++;
  25.  
  26. char c = s[*i];
  27. if (!c) {
  28. tok.type = TOK_UNKNOWN;
  29. return tok;
  30. }
  31.  
  32. if (c=='!' || c=='=' || c=='<' || c=='>') {
  33. char c2 = s[*i+1];
  34. if (c=='!' && c2=='=') { tok.type = TOK_OPERATOR; strcpy(tok.lexeme, "!="); *i += 2; return tok; }
  35. if (c=='=' && c2=='=') { tok.type = TOK_OPERATOR; strcpy(tok.lexeme, "=="); *i += 2; return tok; }
  36. if (c=='<' && c2=='=') { tok.type = TOK_OPERATOR; strcpy(tok.lexeme, "<="); *i += 2; return tok; }
  37. if (c=='>' && c2=='=') { tok.type = TOK_OPERATOR; strcpy(tok.lexeme, ">="); *i += 2; return tok; }
  38.  
  39. // If your lexer supports single-char versions of these:
  40. if (c=='<' ) { tok.type = TOK_OPERATOR; strcpy(tok.lexeme, "<"); *i += 1; return tok; }
  41. if (c=='>' ) { tok.type = TOK_OPERATOR; strcpy(tok.lexeme, ">"); *i += 1; return tok; }
  42.  
  43.  
  44. }
  45.  
  46.  
  47. if (c=='+' || c=='-' || c=='*' || c=='/') {
  48. tok.type = TOK_OPERATOR;
  49. tok.lexeme[0] = c;
  50. tok.lexeme[1] = '\0';
  51. *i += 1;
  52. return tok;
  53. }
  54.  
  55. *i += 1;
  56. return tok;
  57. }
  58.  
  59.  
  60.  
  61. int main() {
  62. char input[1000];
  63. int i = 0;
  64.  
  65. printf("Enter a simple C code snippet: ");
  66. fgets(input, sizeof(input), stdin);
  67.  
  68. printf("\nTokens Found:\n");
  69.  
  70. while (input[i] != '\0') {
  71.  
  72. if (isspace(input[i])) {
  73. i++;
  74. continue;
  75. }
  76.  
  77. if (
  78. (input[i] == '+' && input[i+1] == '=') ||
  79. (input[i] == '-' && input[i+1] == '=') ||
  80. (input[i] == '*' && input[i+1] == '=') ||
  81. (input[i] == '/' && input[i+1] == '=') ||
  82. (input[i] == '=' && input[i+1] == '=') ||
  83. (input[i] == '!' && input[i+1] == '=') ||
  84. (input[i] == '>' && input[i+1] == '=') ||
  85. (input[i] == '<' && input[i+1] == '=')
  86. ) {
  87. printf("OPERATOR: %c%c\n", input[i], input[i+1]);
  88. i += 2;
  89. }
  90.  
  91. else if (input[i] == '+' || input[i] == '-' ||
  92. input[i] == '*' || input[i] == '/' ||
  93. input[i] == '=' || input[i] == '>' ||
  94. input[i] == '<' || input[i] == '!') {
  95. printf("OPERATOR: %c\n", input[i]);
  96. i++;
  97. }
  98. else {
  99. i++;
  100. }
  101. }
  102.  
  103. return 0;
  104. }
Success #stdin #stdout 0.01s 5320KB
stdin
a+=b
stdout
Enter a simple C code snippet: 
Tokens Found:
OPERATOR: +=