#include "grammersym.h"
#include <string.h>
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]* {
#include <stdio.h>
%}
-%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;
}
%{
void parser_process(struct Node* node);
char* __dw_strdup(const char* a);
+double __dw_atof(const char* str);
%}
%%
$<node>$.string = $<node>1.string;
$<node>$.nodes = $<node>1.nodes;
$<node>$.ident = $<node>1.ident;
+ $<node>$.type = $<node>1.type;
+ }
+ | NUMBER {
+ $<node>$.string = $<node>1.string;
+ $<node>$.nodes = $<node>1.nodes;
+ $<node>$.ident = $<node>1.ident;
+ $<node>$.number = $<node>1.number;
+ $<node>$.type = $<node>1.type;
}
| command {
$<node>$ = $<node>1;
single_argument
: SPACE argument SPACE {
- $<node>$ = $<node>2;
+ $<node>$.string = $<node>2.string;
+ $<node>$.nodes = $<node>2.nodes;
+ $<node>$.ident = $<node>2.ident;
+ $<node>$.number = $<node>2.number;
+ $<node>$.type = $<node>2.type;
}
| SPACE argument {
- $<node>$ = $<node>2;
+ $<node>$.string = $<node>2.string;
+ $<node>$.nodes = $<node>2.nodes;
+ $<node>$.ident = $<node>2.ident;
+ $<node>$.number = $<node>2.number;
+ $<node>$.type = $<node>2.type;
}
| argument SPACE {
- $<node>$ = $<node>1;
+ $<node>$.string = $<node>1.string;
+ $<node>$.nodes = $<node>1.nodes;
+ $<node>$.ident = $<node>1.ident;
+ $<node>$.number = $<node>1.number;
+ $<node>$.type = $<node>1.type;
}
| argument {
- $<node>$ = $<node>1;
+ $<node>$.string = $<node>1.string;
+ $<node>$.nodes = $<node>1.nodes;
+ $<node>$.ident = $<node>1.ident;
+ $<node>$.number = $<node>1.number;
+ $<node>$.type = $<node>1.type;
}
;
$<node>$.nodes[0]->nodes = NULL;
$<node>$.nodes[0]->ident = NULL;
$<node>$.nodes[0]->string = NULL;
+ $<node>$.nodes[0]->number = $<node>1.number;
+ $<node>$.nodes[0]->type = $<node>1.type;
if($<node>1.ident != NULL) $<node>$.nodes[0]->ident = __dw_strdup($<node>1.ident);
if($<node>1.string != NULL) $<node>$.nodes[0]->string = __dw_strdup($<node>1.string);
if($<node>1.nodes != NULL) $<node>$.nodes[0]->nodes = $<node>1.nodes;
$<node>$.nodes[i]->nodes = NULL;
$<node>$.nodes[i]->ident = NULL;
$<node>$.nodes[i]->string = NULL;
+ $<node>$.nodes[i]->number = $<node>3.number;
+ $<node>$.nodes[i]->type = $<node>3.type;
if($<node>3.ident != NULL) $<node>$.nodes[i]->ident = __dw_strdup($<node>3.ident);
if($<node>3.string != NULL) $<node>$.nodes[i]->string = __dw_strdup($<node>3.string);
if($<node>3.nodes != NULL) $<node>$.nodes[i]->nodes = $<node>3.nodes;
$<node>$.string = NULL;
$<node>$.ident = $<node>1.ident;
$<node>$.nodes = $<node>4.nodes;
+ $<node>$.type = $<node>1.type;
}
| IDENTIFIER '(' arguments ')' {
$<node>$.string = NULL;
$<node>$.ident = $<node>1.ident;
$<node>$.nodes = $<node>3.nodes;
+ $<node>$.type = $<node>1.type;
}
;
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) {
}
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");