From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id NAA01923; Mon, 23 Apr 2001 13:51:29 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id NAA05111 for caml-list@pauillac.inria.fr; Mon, 23 Apr 2001 13:51:29 +0200 (MET DST) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id FAA32126 for ; Mon, 23 Apr 2001 05:16:41 +0200 (MET DST) Received: from mail.is.titech.ac.jp (mail.is.titech.ac.jp [131.112.51.131]) by nez-perce.inria.fr (8.11.1/8.10.0) with SMTP id f3N3GdX11496 for ; Mon, 23 Apr 2001 05:16:40 +0200 (MET DST) Received: (qmail 18689 invoked from network); 23 Apr 2001 03:16:32 -0000 Received: from dmznat-9.is.titech.ac.jp (HELO localhost) (131.112.51.189) by mail.is.titech.ac.jp with SMTP; 23 Apr 2001 03:16:32 -0000 Date: Mon, 23 Apr 2001 12:16:50 +0900 (JST) Message-Id: <20010423.121650.71094554.wakita@is.titech.ac.jp> To: caml-list@inria.fr Subject: [Caml-list] symbolic reference to the nonterminals in ocamlyacc action part From: wakita@is.titech.ac.jp X-Mailer: Mew version 1.95b114 on Emacs 20.7 / Mule 4.0 (HANANOEN) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk I added a small feature to ocamlyacc. The attached micro-patch allows one to reference an instance of non-terminal symbol in the action part by the symbolic name (e.g., expr) as well as the occurence position number (e.g., $1). As in original ocamlyacc, an instance of the i'th occurence of the symbol in the grammar rule can be referenced in the action part by $i. An instance of $i$'th occurrence of the symbol, sym, can be referenced in the action part by sym$i$ or by sym followed by $i-1$ number of quote. Example: | expr PLUS expr { Op(Plus, $1, $3) } | expr MINUS expr { Op(Minus, expr1, expr2) } | expr TIMES expr { Op(Times, expr, expr') } | expr DIV expr { Op(Div, expr, $3) } I have rewrote the parser.mly's found in the ocaml distribution using the added features. If you are interested, the full patch file including these examples is found: http://www.is.titech.ac.jp/~wakita/ocaml/ocamlyacc.patch Ken Wakita Tokyo Institute of Technology diff -r -c ocaml-3.01/yacc/reader.c ocaml/yacc/reader.c *** ocaml-3.01/yacc/reader.c Sat Mar 4 00:15:43 2000 --- ocaml/yacc/reader.c Wed Apr 18 18:14:01 2001 *************** *** 1220,1225 **** --- 1220,1226 ---- for (i = nitems - 1; pitem[i]; --i) ++n; for (i = 1; i <= n; i++) { + char uscore = ' '; item = pitem[nitems + i - n - 1]; if (item->class == TERM && !item->tag) continue; fprintf(f, " let _%d = ", i); *************** *** 1229,1234 **** --- 1230,1248 ---- fprintf(f, "peek_val parser_env %d in\n", n - i); else fprintf(f, "(peek_val parser_env %d : '%s) in\n", n - i, item->name); + { + char uscore = isupper(item->name[0]) ? '_' : ' '; + int j, match = 0; + for (j = 1; j <= i; j++) { + if (strcmp((const char *)item->name, + (const char *)pitem[nitems + j - n - 1]->name) == 0) + match++; + } + fprintf(f, " let %c%s%d = _%d in\n", uscore, item->name, match, i); + fprintf(f, " let %c%s", uscore, item->name); + for (j = 1; j < match; j++) fputc('\'', f); + fprintf(f, " = _%d in\n", i); + } } fprintf(f, " Obj.repr((\n"); fprintf(f, "# %d \"%s\"\n", lineno, input_file_name); ------------------- To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr