122 lines
1.9 KiB
Plaintext
122 lines
1.9 KiB
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 STRING_LITERAL
|
|
%token SHORT_DECLARATION LBRACE RBRACE SEMICOLON ASSIGN
|
|
%token VAR
|
|
|
|
%token STRING
|
|
%token UINT UINT8 UINT16 UINT32 UINT64
|
|
%token INT INT8 INT16 INT32 INT64
|
|
|
|
|
|
%type <str> expr declaration literal
|
|
|
|
%%
|
|
|
|
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);
|
|
// }
|
|
;
|
|
|
|
// Типы
|
|
// Группирует все возможные типы
|
|
int_types:
|
|
UINT
|
|
| UINT8
|
|
| UINT16
|
|
| UINT32
|
|
| UINT64
|
|
| INT
|
|
| INT8
|
|
| INT16
|
|
| INT32
|
|
| INT64
|
|
;
|
|
|
|
string_types:
|
|
STRING
|
|
;
|
|
|
|
type:
|
|
int_types
|
|
| string_types
|
|
;
|
|
//
|
|
|
|
// Литералы
|
|
literal:
|
|
STRING_LITERAL
|
|
| NUMBER
|
|
;
|
|
//
|
|
|
|
declaration:
|
|
IDENTIFIER SHORT_DECLARATION literal {
|
|
printf("Short declaration: %s := %s\n", $1, $3);
|
|
free_node($1);
|
|
free_node($3);
|
|
}
|
|
| VAR IDENTIFIER type {
|
|
printf("Variable declaration: var %s type\n", $2);
|
|
free_node($2);
|
|
}
|
|
| VAR IDENTIFIER type ASSIGN literal {
|
|
printf("Var with assign declaration: var %s type = %s\n", $2, $5);
|
|
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;
|
|
} |