Parsing question
[1/9] from: gilette:echo at: 14-Apr-2003 12:15
Hello,
I have a parsing question. I am trying to parse a string of PGN format.
it is the formalize way to notee a chess game, for those who don't know.
I want to explicitly say that the space or the newline can end some
sub-pattern. But i have an error message if i use the commented line
below...
Any idea ? Is there a smarter way of doing this ?
digit: charset [#"0" - #"9"]
spacer: #" "
;--- spacer: [#" " | #"^/"] <-- does not work
round_number: [copy x some digit #"." (print join "tour=" x)]
round_played: [copy y to spacer (print join "played=" y)]
round_comment: [#"{" copy comm to #"}" (print comm) to spacer]
round: [round_number round_played round_played opt round_comment]
game: [some round]
rules: [game to end]
parse "1.d3 d5 2.Nf3 Nf6 3.Bd2 c5 4.g3 Nc6 {a comment} 5.Na3 e6 6.b3
Be7" rules
Thank you !
--
Serge Gilette
-------------------------
04 92 28 32 50
[2/9] from: gscottjones:mchsi at: 14-Apr-2003 7:00
From: "Serge Gilette"
> ... I have a parsing question. I am trying to parse a
> string of PGN format. it is the formalize way to notee
> a chess game, for those who don't know.
I didn't know that. Thanks!
> I want to explicitly say that the space or the newline can end some
> sub-pattern. But i have an error message if i use the commented line
> below...
rest of posting:
http://www.escribe.com/internet/rebol/m30984.html
Hi, Serge,
I am no expert on parsing. I am trying to get better, so yours was an
interesting problem. If I recall correctly, one cannot use the parse word
to
with charsets, hence the continual errors. I had to hunt and peck
myself to figure this one out, but the following seems to work. I also
added a bit more to the rules to be sure the last move was parsed.
I am sure that a real parse guru will show *the best way* to do it also.
HTH!
--Scott Jones
digit: charset [#"0" - #"9"]
space: #" "
crlf: #"^/"
spacer: [to space | to crlf | to end]
round_number: [copy x some digit #"." (print join "tour=" x)]
round_played: [copy y spacer (print join "played=" y)]
round_comment: [#"{" copy comm to #"}" (print comm) spacer]
round: [round_number round_played round_played opt round_comment]
game: [some round]
rules: [game to end]
parse "1.d3 d5 2.Nf3 Nf6 3.Bd2 c5 4.g3 Nc6 {a comment} 5.Na3 e6 6.b3 Be7"
rules
[3/9] from: antonr:iinet:au at: 14-Apr-2003 22:05
To consider even spaces and newlines use parse/all :
>> ? parse
USAGE:
PARSE input rules /all /case
DESCRIPTION:
Parses a series according to rules.
PARSE is a native value.
ARGUMENTS:
input -- Input series to parse (Type: series)
rules -- Rules to parse by (Type: block string none)
REFINEMENTS:
/all -- Parses all chars including spaces.
/case -- Uses case-sensitive comparison.
Anton Rolls.
[4/9] from: lmecir:mbox:vol:cz at: 14-Apr-2003 13:41
Hi Serge,
> I have a parsing question. I am trying to parse a string of PGN format.
> it is the formalize way to notee a chess game, for those who don't know.
<<quoted lines omitted: 5>>
> spacer: #" "
> ;--- spacer: [#" " | #"^/"] <-- does not work
you can use:
spacer: charset [#" " #"^/"]
nonspacer: complement spacer
> round_number: [copy x some digit #"." (print join "tour=" x)]
> round_played: [copy y to spacer (print join "played=" y)]
> round_comment: [#"{" copy comm to #"}" (print comm) to spacer]
instead of
to spacer
you can then use
any nonspacer
also see the TO-RULE function, which is defined in http://www.fm.vslib.cz/~ladislav/rebol/parseen.r
and the include.r and build.r files.
> round: [round_number round_played round_played opt round_comment]
> game: [some round]
<<quoted lines omitted: 6>>
> -------------------------
> 04 92 28 32 50
Regards
-Ladislav
[5/9] from: gilette:echo at: 14-Apr-2003 15:50
Thanks for your answer.
Curiously this does not seems to work. The rule round_played has a glutton
comportment and grab eveything even if it includes a space...
digit: charset [#"0" - #"9"]
spacer: charset [#" " #"^/"]
nonspacer: complement spacer
round_number: [copy x some digit #"." (print join "tour=" x)]
round_played: [copy y any nonspacer (print join "played=" y)]
round_comment: [#"{" copy comm to #"}" (print comm) any nonspacer]
round: [round_number round_played round_played opt round_comment]
game: [some round]
rules: [game to end]
parse "1.d3 d5 2.Nf3 Nf6 3.Bd2 c5 4.g3 Nc6 {a comment} 5.Na3 e6 6.b3 Be7" rules
display the following :
tour=1
played=d3 d5 2.Nf3 Nf6 3.Bd2 c5 4.g3 Nc6 {a comment} 5.Na3 e6 6.b3 Be7
played=none
Did i mistyped something ? :-)
--
Serge
-------------------------
04 92 28 32 50
Ladislav Mecir wrote:
[6/9] from: lmecir:mbox:vol:cz at: 14-Apr-2003 17:07
Hi Serge,
> Thanks for your answer.
> Curiously this does not seems to work. The rule round_played has a glutton
> comportment and grab eveything even if it includes a space...
digit: charset [#"0" - #"9"]
spacer: charset [#" " #"^/"]
nonspacer: complement spacer
round_number: [copy x some digit #"." (print join "tour=" x)]
round_played: [copy y any nonspacer (print join "played=" y)]
round_comment: [#"{" copy comm to #"}" (print comm) any nonspacer]
round: [any spacer round_number round_played any spacer round_played any spacer
opt round_comment]
game: [some round]
rules: [game to end]
parse/all "1.d3 d5 2.Nf3 Nf6 3.Bd2 c5 4.g3 Nc6 {a comment} 5.Na3 e6 6.b3 Be7" rules
Is this what you wanted?
Regards
-Ladislav
[7/9] from: gilette:echo at: 14-Apr-2003 16:44
I still don't get it... :-(
I expect the following code to print "aaaa" and not "aaaa qqqq" as it does
why does the some nonspacer grabs also the space ?
spacer: charset [#" " #"^/"]
nonspacer: complement spacer
rules: [copy x some nonspacer (print x)]
print parse "aaaa qqqq" rules
I also expected the following code to print true and not false...
spacer: charset [#" " #"^/"]
nonspacer: complement spacer
rules: [some nonspacer spacer some nonspacer]
print parse "aaaa qqqq" rules
Any explanations that i can understand :-)
Thank you !
--
Serge Gilette
-------------------------
04 92 28 32 50
[8/9] from: gilette::echo::fr at: 14-Apr-2003 18:04
YEsssss thank you :-) i missed the point with the /all :-)
Ladislav Mecir wrote:
>Hi Serge,
>>Thanks for your answer.
<<quoted lines omitted: 15>>
>Regards
>-Ladislav
--
Serge
-------------------------
04 92 28 32 50
[9/9] from: lmecir:mbox:vol:cz at: 14-Apr-2003 19:29
Hi,
> spacer: charset [#" " #"^/"]
> nonspacer: complement spacer
> rules: [copy x some nonspacer (print x)]
> print parse "aaaa qqqq" rules
compare it with:
print parse/all "aaaa qqqq" rules
> I also expected the following code to print true and not false...
>
spacer: charset [#" " #"^/"]
nonspacer: complement spacer
rules: [some nonspacer spacer some nonspacer]
print parse "aaaa qqqq" rules
Compare it with:
print parse/all "aaaa qqqq" rules ; == true
> Any explanations that i can understand :-)
If you want to "manipulate" whitespace, try parse/all instead of just parse
> Thank you !
>
> --
> Serge Gilette
> -------------------------
> 04 92 28 32 50
Regards
-Ladislav
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted