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