go-analyzer/analyzers/test/test.y

144 lines
2.5 KiB
Plaintext

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
extern int yylineno;
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;
double num;
}
%token SHORT_DECLARATION LBRACE RBRACE SEMICOLON ASSIGN LPAREN RPAREN COMMA
%token VAR FUNC RETURN IDENTIFIER STRING_LITERAL FLOAT_LITERAL NUMBER
%token PLUS MINUS MULT DIV MOD EXP
%token STRING
%token UINT UINT8 UINT16 UINT32 UINT64
%token INT INT8 INT16 INT32 INT64
%left PLUS MINUS
%left MULT DIV MOD
%left EXP
%left UMINUS
%%
program:
| program statement
;
statement:
expr SEMICOLON
| var_declaration SEMICOLON
| func_declaration
| block
;
block:
LBRACE statements_list RBRACE
;
statements_list:
| statements_list statement
;
expr:
RETURN math_expr { }
| IDENTIFIER ASSIGN math_expr { }
| math_expr { }
;
math_expr:
math_expr PLUS math_expr { }
| math_expr MINUS math_expr { }
| math_expr MULT math_expr { }
| math_expr DIV math_expr { }
| math_expr MOD math_expr { }
| math_expr EXP math_expr { }
| MINUS math_expr %prec UMINUS { }
| LPAREN math_expr RPAREN { }
| NUMBER { }
| FLOAT_LITERAL { }
| IDENTIFIER { }
;
/* Остальные правила остаются без изменений */
int_types:
UINT { }
| UINT8 { }
| UINT16 { }
| UINT32 { }
| UINT64 { }
| INT { }
| INT8 { }
| INT16 { }
| INT32 {}
| INT64 { }
;
string_types:
STRING { }
;
type:
int_types { }
| string_types { }
;
literal:
STRING_LITERAL { }
| FLOAT_LITERAL { }
| NUMBER { }
;
arg_declaration:
IDENTIFIER type { }
;
arg_list:
| arg_declaration
| arg_list COMMA arg_declaration
;
return_type:
| type { }
;
func_declaration:
FUNC IDENTIFIER LPAREN arg_list RPAREN return_type block { }
;
var_declaration:
IDENTIFIER SHORT_DECLARATION math_expr { }
| IDENTIFIER SHORT_DECLARATION literal { }
| VAR IDENTIFIER type { }
| VAR IDENTIFIER type ASSIGN math_expr { }
| VAR IDENTIFIER type ASSIGN literal { }
;
%%
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;
}