diff options
author | 2024-07-24 11:19:52 +0200 | |
---|---|---|
committer | 2024-07-24 11:19:52 +0200 | |
commit | 031bb53552b5b2675b1c3afb57a27b28cb5d379e (patch) | |
tree | 3a3f7d7e2719f53f4d7bdc8dc97de09b92aa7645 | |
parent | 9e881b6ee3b556f8e68c814adb1b51f7f0abe056 (diff) | |
download | properties-031bb53552b5b2675b1c3afb57a27b28cb5d379e.tar.gz properties-031bb53552b5b2675b1c3afb57a27b28cb5d379e.zip |
Add grammar rules to parse the value of a property
In the lexer, I've done a string duplication, 'cause the value
wasn't transfered to the parser.
The VALUE token keep a single character, so, in the parser I need
to concatenate every single character to a string.
-rw-r--r-- | properties.l | 4 | ||||
-rw-r--r-- | properties.y | 30 |
2 files changed, 30 insertions, 4 deletions
diff --git a/properties.l b/properties.l index 67a5c2a..eb6ff81 100644 --- a/properties.l +++ b/properties.l @@ -5,8 +5,8 @@ %% =|: return DIV; -^[ \t]*[a-zA-Z0-9\.]* return KEY; +^[ \t]*[a-zA-Z0-9\.]* yylval.value = strdup(yytext); return KEY; [ \t]* ; -. return VALUE; +. yylval.value = strdup(yytext); return VALUE; %% diff --git a/properties.y b/properties.y index 69d683d..a765c8a 100644 --- a/properties.y +++ b/properties.y @@ -2,19 +2,45 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> %} %token KEY DIV VALUE +%union +{ + char *value; +} + %% -value: VALUE - | VALUE value +value: VALUE { + $$.value = calloc(2, sizeof(char)); + strcat($$.value, $1.value); + } + | value VALUE { + char *s = strdup($$.value); + $$.value = calloc(strlen(s) + strlen($2.value) + 1, sizeof(char)); + strcat($$.value, s); + strcat($$.value, $2.value); + } ; %% +extern FILE *yyin; + +int +main(void) +{ + do { + yyparse(); + } while(!feof(yyin)); + + return EXIT_SUCCESS; +} + void yyerror(char *s) { |