From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 81383BDE6 for ; Sat, 10 Sep 2005 16:15:15 +0200 (CEST) Received: from ux9.sp.cs.cmu.edu (UX9.SP.CS.CMU.EDU [128.2.220.166]) by concorde.inria.fr (8.13.0/8.13.0) with SMTP id j8AEFEf1008669 for ; Sat, 10 Sep 2005 16:15:15 +0200 Received: from c-24-3-154-200.hsd1.pa.comcast.net ([24.3.154.200]) by ux9.sp.cs.cmu.edu id aa23629; 10 Sep 2005 10:14 EDT Received: from ecc by stratocaster.home with local (Exim 4.52) id 1EE688-0002ty-3y for caml-list@yquem.inria.fr; Sat, 10 Sep 2005 10:14:56 -0400 Date: Sat, 10 Sep 2005 10:14:56 -0400 To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] ocamlyacc help Message-ID: <20050910141455.GA11148@localhost> Mail-Followup-To: caml-list@yquem.inria.fr References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.9i From: Eric Cooper X-Miltered: at concorde with ID 4322EA72.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 ocamlyacc:01 char:01 lalr:01 parsing:01 token:01 char:01 token:01 expr:01 expr:01 lalr:01 parsers:01 stack:01 ocaml:01 wrote:01 On Sat, Sep 10, 2005 at 10:40:28PM +1200, Jonathan Roewen wrote: > I want to parse a string of format: > > (OP FLOAT*)* and generate a (char * float list) list. > > The problem I have is I don't know how to generate further tuples. (Re)read the Dragon book on LALR parsing! %token OP %token FLOAT %start expr %type <(char * float list) list> expr %% expr: op_list { List.rev $1 } ; op_list: /* empty */ { [] } | op_list OP float_list { ($2, List.rev $3) :: $1 } ; float_list: /* empty */ { [] } | float_list FLOAT { $2 :: $1 } ; Note that it's good practice to write rules for "lists of things" in a left-recursive style, because that allows LALR parsers to handle arbitrarily-long lists without stack overflow. In OCaml, where the natural semantic action for a list is to build a list, you probably want to build the list backwards (i.e. $2 :: $1 instead of $1 @ [$2]), and then reverse it where it's used (if that's even necessary). -- Eric Cooper e c c @ c m u . e d u