[DGD] parse string difficulties
Robert Forshaw
iouswuoibev at hotmail.com
Sun Mar 28 19:20:56 CEST 2004
Despite my best efforts I can't understand how parse string is supposed to
work. I thought I *might* know what I was doing, when I put it to the test,
but got some unexpected results.
Right now the reason for me employing parse_string is to interpret
datafiles. The datafiles are meant to contain three things: operators, a
prefix-parameter and a suffix-parameter. Operators consist of one symbol
only and can be: '+', '-', '=', or '.'. A prefix-parameter is optional and
appears behind the operator, a suffix-parameter is also optional and appears
in front of it.
So an example data file might look like this:
.food
.chocolate
weight=8
layers+3
fattening=yes
strange-
smell-noxious
The prefix-parameter, operator and suffix-parameter collectively make a
line, with newlines acting as seperators for these lines.
I want parse_string to interpret the entire datafile in one string, and
allocate an array in the following format: ({ ({ operator, prefix-parameter,
suffix-parameter }), ({ operator, prefix-parameter, suffix-parameter }) ...
}). If a prefix-parameter or suffix parameter is occluded, that respective
element in the returned array will be nil.
So in the example:
.food
.chocolate
weight=8
layers+3
fattening=yes
strange-
smell-noxious
it would return:
({ ({ ".", nil, "food" }), ({ ".", "nil", "chocolate" }), ({ "=", "weight",
"8" }), ({ "+", "layers", "3"}),
({ "=", "fattening", "yes" }), ({ "-", "strange", nil }), ({ "-",
"smell", "noxious" }) })
Call me stupid, call me what you like, but I can't figure out how to
accomplish this using parse_string. I can't even do something much simpler,
like parseing a single line. Here:
ar = parse_string(
"whitespace = /[\b\r\n\t ]+/" +
"word = /[a-zA-Z]+/" +
"operator = /[\.\+\=\-]+/" +
"SENTENCE : operator word operator",
"property+value");
Now this returns nil, but I want it to return ({ "property", "+", "value"
}).
And for some bizzare reason, changing the operator regexp so that it only
detects "+":
ar = parse_string(
"whitespace = /[\b\r\n\t ]+/" +
"word = /[a-zA-Z]+/" +
"operator = /++/" +
"SENTENCE : operator word operator",
"property+value");
Causes a malformed rule error.
I seriously don't know what I'm doing here. It would be most helpful if
someone could show me line for line how to write a grammar that interprets
my datafiles, that would help me relate to what the function is doing.
Anyway, all help is appreciated...
_________________________________________________________________
Express yourself with cool new emoticons http://www.msn.co.uk/specials/myemo
_________________________________________________________________
List config page: http://list.imaginary.com/mailman/listinfo/dgd
More information about the DGD
mailing list