[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [computer-go] SGF parsers



Thanks Gunnar for the code example. My own parser is actually not so far off
from what you have. I just hadn't fully realised to what extent everything
is really just a property. I got a bit intimidated by the example I found
here http://www.red-bean.com/sgf/examples/ff4_ex.sgf

You've swept a few things under the carpet though. The interpretation of the
node-data is in the add_property, which is maybe the biggest part of it all
if you want to support all the examples in the example file above. Also I
see you don't do anything for unnamed properties, I suppose you assume
add_property takes care of that as well.

Technically your routine parses an SGF file, but it doesn't interpret it,
which is where I still see some work to come in. For example: in the
DOM-tree I'd want the move-node to be parent of the comment-node that
belongs to it. Same for markup. But in the SGF file they can appear all
mixed up. And maybe most importantly, variations at the start aren't simply
move variations but different games, for which some game properties can
appear only at the first node.

But the separation of parsing and interpretation is probably a good thing.

> -----Original Message-----
> From: computer-go-bounces@xxxxxxxxxxxxxxxxx
> [mailto:computer-go-bounces@xxxxxxxxxxxxxxxxx]On Behalf Of Gunnar
> Farnebdck
> Sent: Tuesday, December 14, 2004 3:03
> To: tesujisoftware@xxxxxxxxxxxxxxxxx; computer-go
> Subject: Re: [computer-go] SGF parsers
>
>
> Mark wrote:
> > P.S. In my search for definitions of SGF, I came upon an
> example with merged
> > games. That seems to unnecessarily complicate things, is it widely used?
>
> I've come across such files a few times but it seems easier to split
> them externally than handle them internally anyway.
>
> > as making a full SGF reader seems like a considerable amount of
> > work.
>
> It's not very hard, at least not in programming languages with sane
> string support. I won't guarantee the correctness of the code below
> (it doesn't handle every newline convention at least) but it has
> worked well enough for me.
>
> Basically read_property() and read() are methods in an SgfNode class
> which is used to build an sgf tree. The main parsing, including
> recursion, is in the read() method.
>
> /Gunnar
>
> string read_property(string name, string sgfdata)
> {
>     string value = "";
>     int quoted = 0;
>     while (sgfdata != "")
>     {
>         string c = sgfdata[0..0];
>         sgfdata = sgfdata[1..];
>         if (quoted)
>         {
>             if (value != "\n")
>                 value += c;
>             quoted = 0;
>         }
>         else if (c == "\\")
>             quoted = 1;
>         else if (c == "]")
>         {
>             add_property(name, value);
>             return sgfdata;
>         }
>         else
>             value += c;
>     }
>     werror("Unexpected end of sgf data.\n");
>     return sgfdata;
> }
>
> string read(string sgfdata)
> {
>     string property_name = "";
>     int variation = 0;
>     while (sgfdata != "")
>     {
>         string c = sgfdata[0..0];
>         if (c >= "A" && c <= "Z")
>             sscanf(sgfdata, "%[A-Z]%s", property_name, sgfdata);
>         else if (c == "[")
>             sgfdata = read_property(property_name, sgfdata[1..]);
>         else if (c == "(")
>         {
>             variation = 1;
>             sgfdata = sgfdata[1..];
>         }
>         else if (c == ")")
>             return sgfdata[1..];
>         else if (c == ";")
>         {
>             SgfNode child = SgfNode(this_object());
>             if (!variation)
>                 return child->read(sgfdata[1..]);
>             else
>                 sgfdata = child->read(sgfdata[1..]);
>         }
>         else if (c == " " || c == "\t" || c == "\v" || c == "\n")
>             sgfdata = sgfdata[1..];
>         else
>         {
>             werror("Warning: Skipping unexpected character %s.\n", c);
>             sgfdata = sgfdata[1..];
>         }
>     }
>     werror("Unexpected end of sgf data.\n");
>     return sgfdata;
> }
> _______________________________________________
> computer-go mailing list
> computer-go@xxxxxxxxxxxxxxxxx
> http://www.computer-go.org/mailman/listinfo/computer-go/
>

_______________________________________________
computer-go mailing list
computer-go@xxxxxxxxxxxxxxxxx
http://www.computer-go.org/mailman/listinfo/computer-go/