<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2002/07/895c189fc1924ba0778af55006e0dbc8"
  from="John Prevost &lt;j.prevost@c...&gt;"
  author="John Prevost"
  date="2002-07-12T02:52:48"
  subject="Re: [Caml-list] What&apos;s wrong with my parser ?"
  prev="2002/07/7185ceed122bb848370dcb84ee239d6e"
  next="2002/07/b0c7298c9c0bead77bcf8ed319d71bf4"
  prev-in-thread="2002/07/7185ceed122bb848370dcb84ee239d6e"
  next-in-thread="2002/07/4646ea3840468a1a992752add8269c3d"
  prev-thread="2002/07/4e07fd5ea13c7b76f164f1ee5265fa6c"
  next-thread="2002/07/b0c7298c9c0bead77bcf8ed319d71bf4"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] What&apos;s wrong with my parser ?">
<msg 
  url="2002/07/1fdaa5e902b0c73415d567249ed998ad"
  from="Nicolas FRANCOIS &lt;nicolas.francois@f...&gt;"
  author="Nicolas FRANCOIS"
  date="2002-07-12T02:33:12"
  subject="[Caml-list] What&apos;s wrong with my parser ?">
<msg 
  url="2002/07/7185ceed122bb848370dcb84ee239d6e"
  from="John Prevost &lt;j.prevost@c...&gt;"
  author="John Prevost"
  date="2002-07-12T02:42:17"
  subject="Re: [Caml-list] What&apos;s wrong with my parser ?">
</msg>
<msg 
  url="2002/07/895c189fc1924ba0778af55006e0dbc8"
  from="John Prevost &lt;j.prevost@c...&gt;"
  author="John Prevost"
  date="2002-07-12T02:52:48"
  subject="Re: [Caml-list] What&apos;s wrong with my parser ?">
<msg 
  url="2002/07/4646ea3840468a1a992752add8269c3d"
  from="Daniel de Rauglaudre &lt;daniel.de_rauglaudre@i...&gt;"
  author="Daniel de Rauglaudre"
  date="2002-07-12T07:14:24"
  subject="Re: [Caml-list] What&apos;s wrong with my parser ?">
</msg>
</msg>
<msg 
  url="2002/07/2883b78ed3d95e9729ba61b17cb910fc"
  from="Nicolas FRANCOIS &lt;nicolas.francois@f...&gt;"
  author="Nicolas FRANCOIS"
  date="2002-07-13T11:19:24"
  subject="Re: [Caml-list] What&apos;s wrong with my parser ?">
</msg>
</msg>
</thread>

<contents>
&gt;&gt;&gt;&gt;&gt; "nf" == Nicolas FRANCOIS (AKA El Bofo) &lt;nicolas.francois@free.fr&gt; writes:

    nf&gt; I define a polynom parser like this : let rec parse = let

  let rec parse =
    let parse_puissance = parser
      | [&lt; ''^'; e = parse_int &gt;] -&gt; e
      | [&lt; &gt;] -&gt; 1
    in
    let parse_x = parser
      | [&lt; ''X'; e = parse_puissance &gt;] -&gt; e
      | [&lt; &gt;] -&gt; 0
    in
    let parse_coeff = parser
      | [&lt; n = R.parse &gt;] -&gt; n
      | [&lt; &gt;] -&gt; R.one
    in
    let parse_monome = parser
      | [&lt; n = R.parse; e = parse_x &gt;] -&gt;
	  monome n e
      | [&lt; e = parse_x &gt;] -&gt;
	  monome R.one e
    in
      parser
	| [&lt; ''+'; m = parse_monome; p = parse &gt;] -&gt;
	    m ++ p
	| [&lt; ''-'; m = parse_monome; p = parse &gt;] -&gt;
	    (opp m) ++ p
	| [&lt; m = parse_monome; p = parse &gt;] -&gt;
	    m ++ p
	| [&lt; &gt;] -&gt; zero


    nf&gt; parse_int and the R.parse functions work OK, and I'll add all
    nf&gt; the "_=parse_space" after. But :

# let p1 = P.parse (Stream.of_string "1+X");;
Stack overflow during evaluation (looping recursion?).

    nf&gt; What's the problem ?

Ahh.  I didn't look closely enough.  The problem can be seen by
looking at what happens when you try to parse the empty stream:

in parse:
  [&lt; &gt;] -&gt; cases 1 and 2 fail, tries [&lt; m = parse_monome; p = parse &gt;]
    in m = parse_monome:
      [&lt; &gt;] -&gt; I assume R.parse fails, so it tries [&lt; e = parse_x &gt;]
        in parse_x
          [&lt; &gt;] -&gt; case 1 fails, case 2 [&lt; &gt;] succeeds
          parse_x returns 0
      parse_monome returns monome R.one 0
  [&lt; m = parse_monome; ... &gt;] succeeded, move to [&lt; ...; p = parse &gt;]

And at this point, parse is called on [&lt; &gt;] again and it loops.
Unfortunately, I can't remember how to explicitly check for the end of
the stream--and it's been removed from the O'Caml manual.  That's what
you have to do to avoid this.  A quick and dirty hack is to have a
special symbol (";", for instance) at the end of every phrase.

John.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

</contents>

</message>

