caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Eric Cooper <ecc@cmu.edu>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] ocamlyacc help
Date: Sat, 10 Sep 2005 10:14:56 -0400	[thread overview]
Message-ID: <20050910141455.GA11148@localhost> (raw)
In-Reply-To: <ad8cfe7e05091003403ac29028@mail.gmail.com>

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 <char>	OP
%token <float>	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


      reply	other threads:[~2005-09-10 14:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-10 10:40 Jonathan Roewen
2005-09-10 14:14 ` Eric Cooper [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050910141455.GA11148@localhost \
    --to=ecc@cmu.edu \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).