summaryrefslogtreecommitdiff
path: root/yy/kalmia.y
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-01-18 13:03:34 -0600
committersanine <sanine.not@pm.me>2023-01-18 13:03:34 -0600
commit82f47550fe3327cce6f2e0e1bf62e81d9ebcf90c (patch)
tree18c15c14a3f1623a4343d36a2f518ee6f51f204a /yy/kalmia.y
parent8d5389d66ef79b58a0fff32fa2b01b4206bfb311 (diff)
begin reentrant refactor
Diffstat (limited to 'yy/kalmia.y')
-rw-r--r--yy/kalmia.y71
1 files changed, 26 insertions, 45 deletions
diff --git a/yy/kalmia.y b/yy/kalmia.y
index 5d808e2..8afc101 100644
--- a/yy/kalmia.y
+++ b/yy/kalmia.y
@@ -1,12 +1,18 @@
-%{
-#include <stdio.h>
-int yyerror(const char *);
-int yylex();
-
-char attr_buf[1024];
-int attr_i;
-%}
+%define api.pure full
+%locations
+%define parse.error verbose
+%param { yyscan_t scanner }
+%code top {
+ #include <stdio.h>
+}
+%code requires {
+ typedef void* yyscan_t;
+}
+%code {
+ int yylex(YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t scanner);
+ int yyerror(YYLTYPE *yyllocp, yyscan_t unused, const char *msg);
+}
%union {
long lval;
@@ -18,15 +24,13 @@ int attr_i;
%token PROLOG
%token S_TAG_OPEN E_TAG_OPEN TAG_CLOSE EMPTY_TAG_CLOSE
%token <sval> NAME
-%token <cval> CHAR
+%token <sval> ATTR
+%token <sval> TEXT
%token <lval> INTEGER
%token <dval> DOUBLE
%token DATE;
-%define parse.error verbose
-
-
%%
@@ -68,51 +72,28 @@ attributes:
;
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; }
+ ATTR '=' '"' TEXT '"' { printf("attribute: %s=%s\n", $1, $4); }
;
integers:
- INTEGER
- | integers INTEGER
+ INTEGER { printf("%d\n", $1); }
+ | integers INTEGER { printf("%d\n", $2); }
;
doubles:
- DOUBLE
- | doubles DOUBLE
+ DOUBLE { printf("%f\n", $1); }
+ | doubles DOUBLE { printf("%f\n", $2); }
;
%%
-
-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)
+int yyerror(YYLTYPE *yyllocp, yyscan_t unused, const char *msg);
{
- fprintf(stderr, "parse error on line %d: %s\n", line_num, msg);
+ fprintf(
+ stderr, "[%d:%d]: %s\n",
+ yyllocp->first_line, yyllocp->first_column, msg
+ );
return 1;
}