summaryrefslogtreecommitdiff
path: root/yy/kalmia.y
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-01-16 13:35:04 -0600
committersanine <sanine.not@pm.me>2023-01-16 13:35:04 -0600
commit8d5389d66ef79b58a0fff32fa2b01b4206bfb311 (patch)
tree02bd7a8c80d5c4897a6e29d5efcccc9ea01082e4 /yy/kalmia.y
parente7073c34d2ca92663d98bbb1912ad5f6e615e87f (diff)
add initial parser
Diffstat (limited to 'yy/kalmia.y')
-rw-r--r--yy/kalmia.y118
1 files changed, 118 insertions, 0 deletions
diff --git a/yy/kalmia.y b/yy/kalmia.y
new file mode 100644
index 0000000..5d808e2
--- /dev/null
+++ b/yy/kalmia.y
@@ -0,0 +1,118 @@
+%{
+#include <stdio.h>
+int yyerror(const char *);
+int yylex();
+
+char attr_buf[1024];
+int attr_i;
+%}
+
+
+%union {
+ long lval;
+ double dval;
+ char *sval;
+ char cval;
+}
+
+%token PROLOG
+%token S_TAG_OPEN E_TAG_OPEN TAG_CLOSE EMPTY_TAG_CLOSE
+%token <sval> NAME
+%token <cval> CHAR
+%token <lval> INTEGER
+%token <dval> DOUBLE
+%token DATE;
+
+
+%define parse.error verbose
+
+
+%%
+
+
+document: PROLOG element;
+
+elements:
+ element
+ | elements element
+ ;
+
+element:
+ empty_tag
+ | start_tag end_tag
+ | start_tag content end_tag
+ ;
+
+content:
+ elements
+ | integers
+ | doubles
+ | DATE
+ ;
+
+empty_tag:
+ S_TAG_OPEN NAME attributes EMPTY_TAG_CLOSE { printf("empty tag: %s\n", $2); }
+ ;
+
+start_tag:
+ S_TAG_OPEN NAME attributes TAG_CLOSE { printf("enter tag: %s\n", $2); }
+ ;
+
+end_tag:
+ E_TAG_OPEN NAME TAG_CLOSE { printf("exit tag: %s\n", $2); }
+ ;
+
+attributes:
+ | attribute
+ | attributes attribute;
+ ;
+
+attribute:
+ NAME '=' '"' chars '"' { attr_buf[attr_i] = 0; printf("attribute: %s=%s\n", $1, attr_buf); attr_i = 0; }
+ ;
+
+chars:
+ | CHAR { attr_buf[attr_i] = $1; attr_i += 1; }
+ | chars CHAR { attr_buf[attr_i] = $2; attr_i += 1; }
+ ;
+
+
+integers:
+ INTEGER
+ | integers INTEGER
+ ;
+
+doubles:
+ DOUBLE
+ | doubles DOUBLE
+ ;
+
+
+%%
+
+
+extern FILE *yyin;
+extern int line_num;
+
+int main()
+{
+ attr_i = 0;
+ line_num = 0;
+ yyin = fopen("in.xml", "r");
+ if (yyin == NULL) {
+ fprintf(stderr, "could not open file!\n");
+ return -1;
+ }
+ do {
+ yyparse();
+ } while (!feof(yyin));
+
+ return 0;
+}
+
+
+int yyerror(const char *msg)
+{
+ fprintf(stderr, "parse error on line %d: %s\n", line_num, msg);
+ return 1;
+}