diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2016-01-28 23:32:25 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2016-01-28 23:32:25 (GMT) |
commit | 16e0fd9d89ef433848678dfc8dd20426844a2868 (patch) | |
tree | 79075ae02c133cea21ffb555b1086aae833b3aac /tools/d2c/args/grammar.y | |
parent | 66c99d59d6a6d533de0bb65488de8243213bcdea (diff) |
Cleaned, rewritten and improved the whole code of the compiler.
Diffstat (limited to 'tools/d2c/args/grammar.y')
-rw-r--r-- | tools/d2c/args/grammar.y | 191 |
1 files changed, 191 insertions, 0 deletions
diff --git a/tools/d2c/args/grammar.y b/tools/d2c/args/grammar.y new file mode 100644 index 0000000..6477c47 --- /dev/null +++ b/tools/d2c/args/grammar.y @@ -0,0 +1,191 @@ + +%{ + +#include "tokens.h" + + +/* Affiche un message d'erreur suite à l'analyse en échec. */ +static int yyerror(right_op_t *, unsigned int *, char *); + + +%} + + +%code requires { + +#include "decl.h" + +} + + +%union { + + right_op_t operand; /* Construction finale */ + + char *string; /* Chaîne de caractères */ + int integer; /* Valeur numérique entière */ + + arg_list_t *args; /* Liste d'arguments */ + arg_expr_t *arg; /* Argument multi-usages */ + ConvUnaryOperation un_op; /* Opération unaire */ + ConvBinaryOperation bin_op; /* Opération bianire */ + +} + + +%define api.pure full + +%parse-param { right_op_t *operand } { unsigned int *init_token } +%lex-param { unsigned int *init_token } + +%code provides { + +#define YY_DECL \ + int args_lex(YYSTYPE *yylvalp, unsigned int *init_token) + +YY_DECL; + +} + + +%token FORCE_EXPR FORCE_CALL ALLOW_ALL +%token NAME +%token NUMBER BINVAL HEXVAL +%token COMMA COLON OP CP +%token NOT AND_LOG EOR + + +%type <string> NAME +%type <integer> NUMBER +%type <string> BINVAL HEXVAL + +%type <operand> call +%type <args> arg_list +%type <arg> arg_expr arg_composed +%type <un_op> arg_expr_un_op +%type <bin_op> arg_expr_bin_op +%type <string> arg_field + + +%% + + +right_op : FORCE_EXPR arg_expr { operand->func = NULL; operand->expr = $2; } + | FORCE_CALL call { *operand = $2; } + | arg_expr { operand->func = NULL; operand->expr = $1; } + | call { *operand = $1; } + +call : NAME OP arg_list CP { $$.func = $1; $$.args = $3; } + +arg_list : arg_expr { $$ = build_arg_list($1); } + | arg_list COMMA arg_expr { $$ = extend_arg_list($1, $3); } + +arg_expr : NAME { $$ = build_arg_expr_from_name($1); } + | NUMBER { $$ = build_arg_expr_from_number($1); } + | BINVAL { $$ = build_arg_expr_from_binval($1); } + | HEXVAL { $$ = build_arg_expr_from_hexval($1); } + | arg_composed { $$ = $1; } + | OP arg_expr CP { $$ = $2; } + | arg_expr_un_op arg_expr { $$ = build_unary_arg_expr($2, $1); } + | arg_expr arg_expr_bin_op arg_expr { $$ = build_binary_arg_expr($1, $3, $2); } + +arg_expr_un_op : NOT { $$ = CUO_NOT; } + +arg_expr_bin_op : AND_LOG { $$ = CBO_AND; } + | EOR { $$ = CBO_EOR; } + +arg_composed : arg_field COLON arg_field { $$ = build_composed_arg_expr($1, $3); } + | arg_composed COLON arg_field { $$ = extend_composed_arg_expr($1, $3); } + +arg_field : NAME { $$ = $1; } + | BINVAL { $$ = $1; } + + +%% + + +/****************************************************************************** +* * +* Paramètres : operand = structure impliquée dans le processus. * +* init_token = éventuel jeton d'initialisation ou NULL. * +* msg = message d'erreur. * +* * +* Description : Affiche un message d'erreur suite à l'analyse en échec. * +* * +* Retour : 0 * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int yyerror(right_op_t *operand, unsigned int *init_token, char *msg) +{ + printf("args yyerror line %d: %s\n", yyget_lineno(), msg); + + return 0; + +} + + +/****************************************************************************** +* * +* Paramètres : operand = structure à constituer à partir de données lues. * +* raw = données brutes à analyser. * +* * +* Description : Interprête des données relatives un opérande de droite. * +* * +* Retour : true si l'opération s'est bien déroulée, false sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool load_args_from_raw_line(right_op_t *operand, const char *raw) +{ + bool result; /* Bilan à faire remonter */ + YY_BUFFER_STATE state; /* Support d'analyse */ + int status; /* Bilan de l'analyse */ + + state = yy_scan_string(raw); + + status = yyparse(operand, (unsigned int []) { ALLOW_ALL }); + + result = (status == 0); + + yy_delete_buffer(state); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : operand = structure à constituer à partir de données lues. * +* raw = données brutes à analyser. * +* * +* Description : Interprête des données relatives à un appel avec arguments. * +* * +* Retour : true si l'opération s'est bien déroulée, false sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool load_call_from_raw_line(right_op_t *operand, const char *raw) +{ + bool result; /* Bilan à faire remonter */ + YY_BUFFER_STATE state; /* Support d'analyse */ + int status; /* Bilan de l'analyse */ + + state = yy_scan_string(raw); + + status = yyparse(operand, (unsigned int []) { FORCE_CALL }); + + result = (status == 0); + + yy_delete_buffer(state); + + return result; + +} |