From 90d0a074feb1ec5c2d4b6ac3c74e4a94c79e88ef Mon Sep 17 00:00:00 2001 From: nishi Date: Wed, 5 Jun 2024 02:24:27 +0000 Subject: [PATCH] fixed parser/lexer git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@150 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- Grammar/dw.l | 22 ++++++++++++++++++---- Grammar/dw.y | 45 +++++++++++++++++++++++++++++++++++++++------ Library/parser.c | 8 +++++++- 3 files changed, 64 insertions(+), 11 deletions(-) diff --git a/Grammar/dw.l b/Grammar/dw.l index c109358..d275087 100644 --- a/Grammar/dw.l +++ b/Grammar/dw.l @@ -31,31 +31,45 @@ #include "grammersym.h" #include extern YYSTYPE yylval; + +double __dw_atof(const char* str); %} %% -["][^"]*["] { +["][^"]*["] { yylval.node.string = strdup(yytext + 1); yylval.node.string[strlen(yylval.node.string) - 1] = 0; yylval.node.nodes = NULL; yylval.node.ident = NULL; + yylval.node.number = 123123; + yylval.node.type = 'S'; return (STRING); } -['][^']*['] { +['][^']*['] { yylval.node.string = strdup(yytext + 1); yylval.node.string[strlen(yylval.node.string) - 1] = 0; yylval.node.nodes = NULL; yylval.node.ident = NULL; + yylval.node.type = 'S'; return (STRING); } -[a-zA-Z0-9_\-]+ { +[a-zA-Z_][a-zA-Z0-9_\-]* { yylval.node.ident = strdup(yytext); yylval.node.nodes = NULL; yylval.node.string = NULL; + yylval.node.type = 'I'; return (IDENTIFIER); } -[\(\),] { +-?[0-9]+(\.[0-9]+)? { + yylval.node.ident = NULL; + yylval.node.nodes = NULL; + yylval.node.string = NULL; + yylval.node.number = __dw_atof(yytext); + yylval.node.type = 'N'; + return (NUMBER); +} +[\(\),] { return (yytext[0]); } [ \t\v]* { diff --git a/Grammar/dw.y b/Grammar/dw.y index 3509483..5866638 100644 --- a/Grammar/dw.y +++ b/Grammar/dw.y @@ -31,14 +31,16 @@ #include %} -%token IDENTIFIER STRING SPACE +%token IDENTIFIER STRING NUMBER SPACE %start command %union { struct Node { char* string; char* ident; - int errnum; + int errnum; + char type; + double number; struct Node** nodes; } node; } @@ -46,6 +48,7 @@ %{ void parser_process(struct Node* node); char* __dw_strdup(const char* a); +double __dw_atof(const char* str); %} %% @@ -55,6 +58,14 @@ argument $$.string = $1.string; $$.nodes = $1.nodes; $$.ident = $1.ident; + $$.type = $1.type; + } + | NUMBER { + $$.string = $1.string; + $$.nodes = $1.nodes; + $$.ident = $1.ident; + $$.number = $1.number; + $$.type = $1.type; } | command { $$ = $1; @@ -63,16 +74,32 @@ argument single_argument : SPACE argument SPACE { - $$ = $2; + $$.string = $2.string; + $$.nodes = $2.nodes; + $$.ident = $2.ident; + $$.number = $2.number; + $$.type = $2.type; } | SPACE argument { - $$ = $2; + $$.string = $2.string; + $$.nodes = $2.nodes; + $$.ident = $2.ident; + $$.number = $2.number; + $$.type = $2.type; } | argument SPACE { - $$ = $1; + $$.string = $1.string; + $$.nodes = $1.nodes; + $$.ident = $1.ident; + $$.number = $1.number; + $$.type = $1.type; } | argument { - $$ = $1; + $$.string = $1.string; + $$.nodes = $1.nodes; + $$.ident = $1.ident; + $$.number = $1.number; + $$.type = $1.type; } ; @@ -83,6 +110,8 @@ arguments $$.nodes[0]->nodes = NULL; $$.nodes[0]->ident = NULL; $$.nodes[0]->string = NULL; + $$.nodes[0]->number = $1.number; + $$.nodes[0]->type = $1.type; if($1.ident != NULL) $$.nodes[0]->ident = __dw_strdup($1.ident); if($1.string != NULL) $$.nodes[0]->string = __dw_strdup($1.string); if($1.nodes != NULL) $$.nodes[0]->nodes = $1.nodes; @@ -98,6 +127,8 @@ arguments $$.nodes[i]->nodes = NULL; $$.nodes[i]->ident = NULL; $$.nodes[i]->string = NULL; + $$.nodes[i]->number = $3.number; + $$.nodes[i]->type = $3.type; if($3.ident != NULL) $$.nodes[i]->ident = __dw_strdup($3.ident); if($3.string != NULL) $$.nodes[i]->string = __dw_strdup($3.string); if($3.nodes != NULL) $$.nodes[i]->nodes = $3.nodes; @@ -113,11 +144,13 @@ command $$.string = NULL; $$.ident = $1.ident; $$.nodes = $4.nodes; + $$.type = $1.type; } | IDENTIFIER '(' arguments ')' { $$.string = NULL; $$.ident = $1.ident; $$.nodes = $3.nodes; + $$.type = $1.type; } ; diff --git a/Library/parser.c b/Library/parser.c index 7004025..7f5cade 100644 --- a/Library/parser.c +++ b/Library/parser.c @@ -67,6 +67,8 @@ struct Node* __dw_duplicate_node(struct Node* node) { r->ident = NULL; r->string = NULL; r->nodes = NULL; + r->number = node->number; + r->type = node->type; if(node->ident != NULL) r->ident = __dw_strdup(node->ident); if(node->string != NULL) r->string = __dw_strdup(node->string); if(node->nodes != NULL) { @@ -97,7 +99,11 @@ void __dw_free_node2(struct Node* node, bool top) { } void __dw_print_node(struct Node* node, bool top) { - if(node->string != NULL) { + if(node->type == 'N') { + printf("%f", node->number); + fflush(stdout); + if(top) printf("\n"); + } else if(node->string != NULL) { printf("\"%s\"", node->string); fflush(stdout); if(top) printf("\n"); -- 2.43.0