fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. typedef union {
  7. int ival;
  8. char *string;
  9. } YYSTYPE;
  10.  
  11. YYSTYPE yylval;
  12.  
  13. %token IF ELSE LPAREN RPAREN LBRACE RBRACE SEMICOLON ASSIGN PLUS MINUS TIMES DIVIDE GT LT GE LE EQ NE AND OR
  14. %token NUMBER IDENTIFIER
  15.  
  16. %}
  17.  
  18. %%
  19.  
  20. statement: if_statement
  21. | assignment
  22. | other_statement // For other statements
  23.  
  24. if_statement: IF LPAREN condition RPAREN LBRACE statement_list RBRACE
  25. | IF LPAREN condition RPAREN LBRACE statement_list RBRACE ELSE LBRACE statement_list RBRACE
  26. ;
  27.  
  28. statement_list: statement
  29. | statement_list statement
  30. ;
  31.  
  32. condition: expression relational_operator expression { $$ = ($1 $2 $3); }
  33.  
  34. relational_operator: GT | LT | GE | LE | EQ | NE | AND | OR;
  35.  
  36. assignment: IDENTIFIER ASSIGN expression SEMICOLON {
  37. printf("Assigning %d to %s\n", $3, $1); // Placeholder action
  38. }
  39.  
  40. expression: expression PLUS expression { $$ = $1 + $3; }
  41. | expression MINUS expression { $$ = $1 - $3; }
  42. | expression TIMES expression { $$ = $1 * $3; }
  43. | expression DIVIDE expression { $$ = $1 / $3; }
  44. | NUMBER { $$ = $1; }
  45. | IDENTIFIER { $$ = 0; } // Placeholder
  46. | LPAREN expression RPAREN { $$ = $2; } // Parentheses for grouping
  47. ;
  48.  
  49. other_statement: /* Empty or other statements */
  50. ;
  51.  
  52. %%
  53.  
  54. int yyerror(char *s) {
  55. fprintf(stderr, "Error: %s\n", s);
  56. return 1;
  57. }
  58.  
  59. int main() {
  60. printf("Enter code (Ctrl+D to finish):\n");
  61. yyparse();
  62. return 0;
  63. }
Success #stdin #stdout #stderr 0.04s 6876KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/ATDYly/prog:63:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit