fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int scope = 0;
  6. %}
  7.  
  8. %%
  9.  
  10. "{" { scope++; printf("Entering scope level %d\n", scope); }
  11.  
  12. "}" {
  13. printf("Exiting scope level %d\n", scope);
  14. scope--;
  15. }
  16.  
  17. "int"|"float"|"char"|"double" { printf("Data type: %s\n", yytext); }
  18.  
  19. ("int"|"float"|"char"|"double")[ \t]+[a-zA-Z_][a-zA-Z0-9_]*";" {
  20. printf("Variable declared: %s at scope level %d\n", yytext, scope);
  21. }
  22.  
  23. [a-zA-Z_][a-zA-Z0-9_]* {
  24. printf("Identifier: %s (scope %d)\n", yytext, scope);
  25. }
  26.  
  27. [ \t]+ ;
  28.  
  29. \n ;
  30.  
  31. . ;
  32.  
  33. %%
  34.  
  35. int main() {
  36. printf("Enter source code:\n");
  37. yylex();
  38. return 0;
  39. }
  40.  
  41. int yywrap() {
  42. return 1;
  43. }
Success #stdin #stdout #stderr 0.02s 6928KB
stdin
int a;
{
    float b;
    {
        char c;
    }
}
stdout
Standard output is empty
stderr
ERROR: /home/yeU0ul/prog:2:1: Syntax error: Operator expected
ERROR: /home/yeU0ul/prog:43:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? EOF: exit