Subscribed unsubscribe Subscribe Subscribe

kmyaccで$の扱いを柔軟にするパッチ

php kmyacc

PHP+kmyacc (あるいはperl+kmyacc) を使うときに面倒なのが意味値 (semantic value) を指定するときに使う「$」の扱い。$$ とか $1 とか $<...> とか $-1 とか以外は普通に変数として見てほしいのにそのへんは甘くない。なので強引にパッチあてて回避。WTF factorが急減しましたとさ。

diff -ur kmyacc-4.1.4~/src/token.c kmyacc-4.1.4/src/token.c
--- kmyacc-4.1.4~/src/token.c	2005-07-06 19:30:41.000000000 +0900
+++ kmyacc-4.1.4/src/token.c	2008-10-04 10:24:56.000000000 +0900
@@ -141,6 +141,7 @@
   int c, tag;
   char *p;
   static char token_buff[MAXTOKEN + 4];
+  static int prev_is_dollar = 0;
 
   if (back_token_text) {
     token_type = back_token_type;
@@ -221,7 +222,32 @@
       c = '%';
     }
   }
-  if (issymch(c)) {
+
+  if (c == '$') {
+    if (!prev_is_dollar) {
+      *p++ = '$';
+      c = get();
+      if (c == '$') {
+        unget(c);
+        prev_is_dollar = 1;
+      } else if (!isdigit(c) && issymch(c)) {
+        do {
+          if (p >= token_buff + MAXTOKEN)
+            goto toolong;
+          *p++ = c;
+          c = get();
+        } while (issymch(c));
+        unget(c);
+        tag = NAME;
+      } else {
+        unget(c);
+      }
+    } else {
+      *p++ = '$';
+      prev_is_dollar = 0;
+    }
+  }
+  else if (issymch(c)) {
     while (issymch(c)) {
       if (p >= token_buff + MAXTOKEN)
         goto toolong;
diff -ur kmyacc-4.1.4-php~/src/yacc.c kmyacc-4.1.4-php/src/yacc.c
--- kmyacc-4.1.4-php~/src/yacc.c	2006-02-16 19:26:52.000000000 +0900
+++ kmyacc-4.1.4-php/src/yacc.c	2008-10-04 04:55:34.000000000 +0900
@@ -61,7 +61,7 @@
 {
   fprintf(stderr, "KMyacc - parser generator ver 4.1.4\n");
   fprintf(stderr, "Copyright (C) 1987-1989,1992-1993,2005,2006  MORI Koichiro\n\n");
-  fprintf(stderr, "Usage: %s [-dvltani] [-b y] [-p yy] [-m model] [-L lang] [grammar.y\n", progname);
+  fprintf(stderr, "Usage: %s [-dvltani] [-b y] [-p yy] [-m model] [-L lang] grammar.y\n", progname);
 }