fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. void lexer(char *input) {
  6. int i = 0;
  7. int len = strlen(input);
  8.  
  9. printf("\nTokens Found:\n");
  10.  
  11. while (i < len) {
  12.  
  13. if (isspace(input[i])) {
  14. i++;
  15. continue;
  16. }
  17.  
  18. if (i + 1 < len) {
  19. char two[3] = {input[i], input[i+1], '\0'};
  20.  
  21. if (strcmp(two, "+=") == 0 || strcmp(two, "-=") == 0 ||
  22. strcmp(two, "/=") == 0 || strcmp(two, "*=") == 0 ||
  23. strcmp(two, ">=") == 0 || strcmp(two, "<=") == 0 ||
  24. strcmp(two, "==") == 0 || strcmp(two, "!=") == 0) {
  25.  
  26. printf("OPERATOR: %s\n", two);
  27. i += 2;
  28. continue;
  29. }
  30. }
  31.  
  32. if (input[i] == '+' || input[i] == '-' || input[i] == '*' ||
  33. input[i] == '/' || input[i] == '=' || input[i] == '>' ||
  34. input[i] == '<' || input[i] == '!') {
  35.  
  36. printf("OPERATOR: %c\n", input[i]);
  37. i++;
  38. continue;
  39. }
  40.  
  41. i++;
  42. }
  43. }
  44.  
  45. int main() {
  46. char input[] = "x += 5; y -= 2; z *= 3; w /= 4; a == b; c != d; e >= f; g <= h; i = j + k - l * m / n > o < p ! q";
  47.  
  48. printf("Testing Lexer with Hardcoded Input:\n");
  49. printf("%s\n", input);
  50. lexer(input);
  51. return 0;
  52. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Testing Lexer with Hardcoded Input:
x += 5; y -= 2; z *= 3; w /= 4; a == b; c != d; e >= f; g <= h; i = j + k - l * m / n > o < p ! q

Tokens Found:
OPERATOR: +=
OPERATOR: -=
OPERATOR: *=
OPERATOR: /=
OPERATOR: ==
OPERATOR: !=
OPERATOR: >=
OPERATOR: <=
OPERATOR: =
OPERATOR: +
OPERATOR: -
OPERATOR: *
OPERATOR: /
OPERATOR: >
OPERATOR: <
OPERATOR: !