aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore5
-rw-r--r--Makefile24
-rw-r--r--properties.l67
3 files changed, 96 insertions, 0 deletions
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;
+}