%{ #include #include #include extern int CURRENT_LINE_NUMBER; extern char *yytext; extern void yyerror(const char *s); extern int yylex(); extern FILE *yyin; void free_node(char *str) { if (str) free(str); } %} %union { char *str; } %token IDENTIFIER NUMBER TEXT %token SHORT_DECLARATION LBRACE RBRACE SEMICOLON %token INT %token VAR %type expr declaration %% program: | program statement ; statement: expr SEMICOLON | declaration SEMICOLON | block ; block: LBRACE statements_list RBRACE ; statements_list: | statements_list statement ; expr: // IDENTIFIER SHORT_DECLARATION NUMBER { // printf("Short declaration: %s := %s\n", $1, $3); // free_node($1); // free_node($3); // } ; declaration: IDENTIFIER SHORT_DECLARATION NUMBER { printf("Short declaration: %s := %s\n", $1, $3); free_node($1); free_node($3); } | VAR IDENTIFIER INT { printf("Variable declaration: var %s int\n", $2); free_node($2); } ; %% int main(int argc, char **argv) { if (argc > 1) { FILE *f = fopen(argv[1], "r"); if (!f) { perror("\033[91mFailed to open file\033[0m"); return 1; } yyin = f; } yyparse(); return 0; }