From e79c6f67ff9276fc2f5a4ab79cd4678aaaa4eb17 Mon Sep 17 00:00:00 2001 From: Alessandro Iezzi Date: Mon, 22 Jul 2024 17:02:03 +0200 Subject: Initial commit --- .gitignore | 5 +++++ Makefile | 24 ++++++++++++++++++++++ properties.l | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 properties.l diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6be102b --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +properties +*.o +*.core +a.out +*-lexer.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..87cd305 --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +LEX = lex +YACC = yacc +CC = cc + +OBJ = properties-lexer.o + +LFLAGS = -ll + + +all: properties +# ${CC} properties-lexer.c properties-parser.c -o properties -ll -ly + +properties-lexer.o: properties.l + ${LEX} properties.l + @mv lex.yy.c properties-lexer.c + ${CC} -c properties-lexer.c + +properties: ${OBJ} + ${CC} ${OBJ} -o $@ ${LFLAGS} + +clean: + @rm -f ${OBJ} *.core a.out + @rm -f properties-lexer.c lex.* + @rm -f properties diff --git a/properties.l b/properties.l new file mode 100644 index 0000000..653c358 --- /dev/null +++ b/properties.l @@ -0,0 +1,67 @@ +%{ +enum { + KEY = 1, + DIV, + VALUE +}; +%} + +%% + +=|: return DIV; +^[ \t]*[a-zA-Z0-9\.]* return KEY; +[ \t]* ; +. return VALUE; + +%% + +struct Properties { + char *key; + char *value; + struct Properties *next; +}; + +int +main(void) +{ + int token; + int next_token = KEY; + struct Properties properties; + properties.next = NULL; + + char *key; + char *value = calloc(2048, sizeof(char)); + int v = 0; + + do { + v = 0; + value = calloc(2048, sizeof(char)); + + token = yylex(); + + if (next_token == VALUE) { + if (token == VALUE) { + do { + *(value + v) = *yytext; + v++; + } while ((token = yylex()) == VALUE); + } + + next_token = KEY; + printf("%s --> %s|\n", key, value); + } + + if (token == KEY && next_token == KEY) { + next_token = DIV; + key = strdup(yytext); + } else if (token == DIV && next_token == DIV) { + next_token = VALUE; + } + } while (token != 0); + + while (properties.next != NULL) { + printf("%s ---> %s\n", properties.key, properties.value); + } + + return EXIT_SUCCESS; +} -- cgit v1.2.3