67 lines
1007 B
Plaintext
67 lines
1007 B
Plaintext
%{
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
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 <str> IDENTIFIER NUMBER TEXT
|
|
%token ASSIGN LBRACE RBRACE
|
|
|
|
%type <str> expr
|
|
|
|
%%
|
|
|
|
program:
|
|
| program expr
|
|
| program block
|
|
;
|
|
|
|
block:
|
|
LBRACE content RBRACE
|
|
;
|
|
|
|
content:
|
|
| content TEXT {
|
|
printf("TEXT: '%s'\n", $2);
|
|
free_node($2); }
|
|
| content block
|
|
| content expr
|
|
;
|
|
|
|
expr:
|
|
IDENTIFIER ASSIGN NUMBER {
|
|
printf("Assignment: %s := %s\n", $1, $3);
|
|
free_node($1);
|
|
free_node($3);
|
|
}
|
|
;
|
|
|
|
%%
|
|
|
|
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;
|
|
} |