caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Right recursion with ocamlyacc
@ 2005-02-16  2:10 Jon Harrop
  2005-02-16  2:28 ` [Caml-list] " Eric C. Cooper
  2005-02-16  3:58 ` Markus Mottl
  0 siblings, 2 replies; 7+ messages in thread
From: Jon Harrop @ 2005-02-16  2:10 UTC (permalink / raw)
  To: caml-list


I've just been converting a new Computer Language Shootout submission from 
OCaml to C++ and found that bison falls over very easily with right recursion 
(failing to load a 10^4-element list) but ocamlyacc had no problems (even on 
a 10^5-element list).

Now, I don't know much about yacc internals but I'm curious as to why this 
would be. Does g++ simply consume much bigger stack frames as it recurses 
(>10x the size?) running out much earler, or is there another reason?

Also, assuming this corresponds to non-tail-recursion inside ocamlyacc, could 
a rewrite in CPS eliminate this problem (probably with a performance hit)?

Or am I completely deluded and, in fact, this is a whole-other stack they're 
talking about, and ocamlyacc just happens to allocate a bigger one.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Right recursion with ocamlyacc
  2005-02-16  2:10 Right recursion with ocamlyacc Jon Harrop
@ 2005-02-16  2:28 ` Eric C. Cooper
  2005-02-16  3:58 ` Markus Mottl
  1 sibling, 0 replies; 7+ messages in thread
From: Eric C. Cooper @ 2005-02-16  2:28 UTC (permalink / raw)
  To: caml-list

On Wed, Feb 16, 2005 at 02:10:45AM +0000, Jon Harrop wrote:
> Now, I don't know much about yacc internals but I'm curious as to why this 
> would be. Does g++ simply consume much bigger stack frames as it recurses 
> (>10x the size?) running out much earler, or is there another reason?

See section 5.9 of the Bison info file:

    By defining the macro `YYMAXDEPTH', you can control how deep the
    parser stack can become before a stack overflow occurs.  Define
    the macro with a value that is an integer.  This value is the
    maximum number of tokens that can be shifted (and not reduced)
    before overflow.  It must be a constant expression whose value is
    known at compile time.
    [...]
    The default value of `YYMAXDEPTH', if you do not define it, is
    10000.

-- 
Eric C. Cooper          e c c @ c m u . e d u


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Right recursion with ocamlyacc
  2005-02-16  2:10 Right recursion with ocamlyacc Jon Harrop
  2005-02-16  2:28 ` [Caml-list] " Eric C. Cooper
@ 2005-02-16  3:58 ` Markus Mottl
  2005-02-16  4:35   ` Jon Harrop
  2005-02-16  4:42   ` skaller
  1 sibling, 2 replies; 7+ messages in thread
From: Markus Mottl @ 2005-02-16  3:58 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

On Wed, 16 Feb 2005, Jon Harrop wrote:
> I've just been converting a new Computer Language Shootout submission from 
> OCaml to C++ and found that bison falls over very easily with right recursion 
> (failing to load a 10^4-element list) but ocamlyacc had no problems (even on 
> a 10^5-element list).

Why use right recursive productions in the grammar?  Left recursion runs
in constant stack space so this would be the way to go...

Regards,
Markus

-- 
Markus Mottl    markus.mottl@gmail.com    http://www.ocaml.info


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Right recursion with ocamlyacc
  2005-02-16  3:58 ` Markus Mottl
@ 2005-02-16  4:35   ` Jon Harrop
  2005-02-16  9:18     ` Radu Grigore
  2005-02-16  4:42   ` skaller
  1 sibling, 1 reply; 7+ messages in thread
From: Jon Harrop @ 2005-02-16  4:35 UTC (permalink / raw)
  To: caml-list

On Wednesday 16 February 2005 03:58, Markus Mottl wrote:
> On Wed, 16 Feb 2005, Jon Harrop wrote:
> > I've just been converting a new Computer Language Shootout submission
> > from OCaml to C++ and found that bison falls over very easily with right
> > recursion (failing to load a 10^4-element list) but ocamlyacc had no
> > problems (even on a 10^5-element list).
>
> Why use right recursive productions in the grammar?

There was no justification for it, right recursion just happened to be most 
natural in this case. I've never worried about it before since it never came 
up.

> Left recursion runs in constant stack space so this would be the way to
> go... 

Yes, left recursion is definitely the way to go in general. But has anyone 
ever had a problem with right-recursion when using ocamlyacc? Is there a 
theoretical reason why this is not a problem with ocamlyacc?

When this came up, it occurred to me that (since switching to OCaml) I've 
neglected the stock advice of avoiding right recursion. And I'm not in the 
habit of parsing small files. :-)

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Right recursion with ocamlyacc
  2005-02-16  3:58 ` Markus Mottl
  2005-02-16  4:35   ` Jon Harrop
@ 2005-02-16  4:42   ` skaller
  1 sibling, 0 replies; 7+ messages in thread
From: skaller @ 2005-02-16  4:42 UTC (permalink / raw)
  To: Markus Mottl; +Cc: Jon Harrop, caml-list

On Wed, 2005-02-16 at 14:58, Markus Mottl wrote:
> On Wed, 16 Feb 2005, Jon Harrop wrote:
> > I've just been converting a new Computer Language Shootout submission from 
> > OCaml to C++ and found that bison falls over very easily with right recursion 
> > (failing to load a 10^4-element list) but ocamlyacc had no problems (even on 
> > a 10^5-element list).
> 
> Why use right recursive productions in the grammar?  Left recursion runs
> in constant stack space so this would be the way to go...

Kind of tricky to represent right associative binary
operator with left recursion .. right recursion is 
much more natural:

rterm: atom rterm { Rterm ($1, $2) }

The thing is, right assoc requires terms to
be stacked up one way or another. Left recursion
defers the problem to client code, and must introduce
intermediate term structure to do that.

Why would I believe my own data structures
would perform more efficiently than the parser stack?

-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Right recursion with ocamlyacc
  2005-02-16  4:35   ` Jon Harrop
@ 2005-02-16  9:18     ` Radu Grigore
  2005-02-18 15:27       ` Radu Grigore
  0 siblings, 1 reply; 7+ messages in thread
From: Radu Grigore @ 2005-02-16  9:18 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

On Wed, 16 Feb 2005 04:35:23 +0000, Jon Harrop <jon@jdh30.plus.com> wrote:

> Yes, left recursion is definitely the way to go in general. But has anyone
> ever had a problem with right-recursion when using ocamlyacc? Is there a
> theoretical reason why this is not a problem with ocamlyacc?

In parsing.ml you will find a function approprately named growstacks.
For very big lists you will get an exception from Array.make. In my
experiment this happened after about 3x10^6 elements. OTOH bison/flex
use AFAIK a fixed stack and fails (as you said) on smaller lists.

-- 
regards,
 radu
http://rgrig.idilis.ro/


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Right recursion with ocamlyacc
  2005-02-16  9:18     ` Radu Grigore
@ 2005-02-18 15:27       ` Radu Grigore
  0 siblings, 0 replies; 7+ messages in thread
From: Radu Grigore @ 2005-02-18 15:27 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

On Wed, 16 Feb 2005 11:18:21 +0200, Radu Grigore <radugrigore@gmail.com> wrote:
> In parsing.ml you will find a function approprately named growstacks.

And, BTW the stack size is doubled which is not a very good strategy.
With the simplest memory model (one contiguous segment) one can see
that if the array is allocated first at address 0 and then at the
lowest address that can hold it then the base address will keep
increasing. This assumes that there aren't other big data structures
in memory that get collected.

The same issue prompted a change in the implementation of STL in MSVC
7.0. IIRC the grow factor was reduced from 2 to 1.5. In theory to
allow reuse it is sufficient to use a factor less than the golden
ration.

-- 
regards,
 radu
http://rgrig.idilis.ro/


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2005-02-18 15:27 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-02-16  2:10 Right recursion with ocamlyacc Jon Harrop
2005-02-16  2:28 ` [Caml-list] " Eric C. Cooper
2005-02-16  3:58 ` Markus Mottl
2005-02-16  4:35   ` Jon Harrop
2005-02-16  9:18     ` Radu Grigore
2005-02-18 15:27       ` Radu Grigore
2005-02-16  4:42   ` skaller

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).