fork download
  1.  
  2. %{
  3. #include <stdio.h>
  4. %}
  5.  
  6. // Define regular expressions for different types of tokens
  7. datatype int|float|double|string|char
  8. identifier [a-zA-Z_][a-zA-Z0-9_]*
  9. number [0-9]+
  10. string \"([^\\\"]|\\.)*\"
  11. character '[^']'
  12. operator \+|\-|\*|\/|\%|\=\= // Operators (+, -, *, /, %, ==)
  13.  
  14. %%
  15.  
  16. // Action rules for handling tokens
  17.  
  18. // Match datatypes
  19. {datatype} { printf("This is a datatype: %s\n", yytext()); }
  20.  
  21. // Match identifiers
  22. {identifier} { printf("This is an identifier: %s\n", yytext()); }
  23.  
  24. // Match numbers
  25. {number} { printf("This is a number: %s\n", yytext()); }
  26.  
  27. // Match strings
  28. {string} { printf("This is a string: %s\n", yytext()); }
  29.  
  30. // Match characters
  31. {character} { printf("This is a character: %s\n", yytext()); }
  32.  
  33. // Match operators
  34. {operator} { printf("This is an operator: %s\n", yytext()); }
  35.  
  36. // Ignore spaces, tabs, and newlines
  37. [ \t\n\r]+ { /* Ignore whitespaces */ }
  38.  
  39. // Catch all unmatched characters
  40. . { printf("Unknown token: %s\n", yytext()); }
  41. %%
  42.  
  43. int main() {
  44. // Start the Lex scanner
  45. yylex();
  46. return 0;
  47. }
  48.  
  49. int yywrap() {
  50. return 1; // End of input
  51. }
  52.  
Success #stdin #stdout #stderr 0.03s 6912KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/3GkSmN/prog:3:1: Syntax error: Operator expected
ERROR: /home/3GkSmN/prog:51:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit