r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[Parse] Discussion of PARSE dialect

btiffin
28-Jun-2007
[2051]
I think Steeve just left...but yep he wants a once and once only 
in any order
Steeve
28-Jun-2007
[2052x3]
i'm here
yeah Chris you got the thing
assuming "A" "B" and "C" could be more complex rules
Chris
28-Jun-2007
[2055]
You may want to check Gabriele's 'compile rules' to see if this has 
been solved.  I have another solution, but needs hashed out a little.
Steeve
28-Jun-2007
[2056]
i know how to build a complex solution but i would prefer a new word 
in the dialect , if other peoples think it's a good improvment
Chris
28-Jun-2007
[2057]
I think so -- it's a common enough pattern...
Gabriele
28-Jun-2007
[2058x3]
.333
sorry, cat on keyboard :)
unfortunately the cat did not type a useful parse rule.
Steeve
28-Jun-2007
[2061]
huhu
Chris
28-Jun-2007
[2062]
I thought the zen answer was imminent...
Gabriele
28-Jun-2007
[2063x3]
steve - not sure this should be a new dialect word, since it is a 
very specific case. basically you want all permutations of a set 
of rules.
the simplest way may be to just generate all the permutations.
that is, parse string [a b c | a c b | c a b | c b a | b c a | b 
a c]
Steeve
28-Jun-2007
[2066]
i give another one example: imagine you have a dialect which describes 
a rectangle with coordinates length and color.

You could write [rectangle 10x20 300 red] but [rectangle 30 red 10x20] 
should be correct too (and other combinations).
So a parser should handle this with not too much complication.
Gabriele
28-Jun-2007
[2067x2]
that can also be written as: parse string [a [b c | c b] | b [c a 
| a c] | c [b a | a b]]  (faster)
steve, we do that all the time, and we just take the last value when 
it is put more than once.
Chris
28-Jun-2007
[2069]
Steeve: In that situation, you have a little discretion to ignore 
excess attributes, as VID does.
Steeve
28-Jun-2007
[2070]
but what if i have 10 rules to complete ?
Gabriele
28-Jun-2007
[2071x2]
that is, two pairs is not an error, the parser just ignores one. 
if vid errored out it would be worse.
you make a function that taken a list of rules generates all the 
permutations.
Steeve
28-Jun-2007
[2073x3]
ok, it was a bas example
*bad
yes Gabriele, it's what i do currently
Chris
28-Jun-2007
[2076]
Not bad, we're just forgiving here : )
Steeve
28-Jun-2007
[2077]
but it's uggly
Gabriele
28-Jun-2007
[2078]
i don't see any better approach, because you are parsing for the 
permutations.
Steeve
28-Jun-2007
[2079]
and it's become huge if i have lot of rules to combinate
Gabriele
28-Jun-2007
[2080x2]
if you use the above approach (second example i wrote) it is reasonably 
fast.
otherwise, you need self-modifying rules, or use flags.
Steeve
28-Jun-2007
[2082x2]
yes we talked about other solutions
but i have just posted here because i thought it will be a good improvment 
to have a new word in parse dialect
Gabriele
28-Jun-2007
[2084]
the point is, this has just been asked once in 7 years, so not sure 
how common ;)
Steeve
28-Jun-2007
[2085]
but my problem is not so common, it seems...
Chris
28-Jun-2007
[2086x2]
I haven't asked, but I have come across this too.
The workarounds do seem a little unwieldy, but I'm not sure what 
the complexity in the proposed 'all keyword would be...
Gabriele
28-Jun-2007
[2088]
internally it would probably have to check flags or do the permutations 
on the fly.
Ladislav
28-Jun-2007
[2089]
this is possible, but my guess, that Steeve would call it ugly too:


>> once-only: func [rule] [use [rule'] copy/deep [rule': :rule [rule' 
(rule': [end skip])]]
]

>> rule: [(a': once-only a b': once-only b c': once-only c) 3 [a' 
| b' | c']]

== [(a': once-only a b': once-only b c': once-only c) 3 [a' | b' 
| c']]
Geomol
28-Jun-2007
[2090x3]
Ladislav, that's not ugly, it's beautiful! :-)

I have a question though. Do you need the skip at the end of once-only? 
Wouldn't it work just with
rule': [end]
To do block parsing, I suggest:

>> once-only: func [:rule] [use [rule'] copy/deep [rule': :rule [rule' 
(rule': [end])]]]

>> rule: [(a': once-only 'a b': once-only 'b c': once-only 'c) 3 
[a' | b' | c']]
>> parse [c b a] rule
== true
hmm, the block parsing give wrong result, if there are less than 
3 values to parse.
Ladislav
28-Jun-2007
[2093]
John: [end skip] is a rule that surely fails, while [end] may succeed 
(at end)
Geomol
28-Jun-2007
[2094]
I see. That's also why my block parsing give false result with less 
input. So for block-parsing, it should be:

>> once-only: func [:rule] [use [rule'] copy/deep [rule': :rule [rule' 
(rule': [end skip])]]]
Ladislav
28-Jun-2007
[2095x2]
why, my rule should be usable as-is even for block parsing?
(I don't like fetched arguments)
Geomol
28-Jun-2007
[2097]
>> once-only: func [rule] [use [rule'] copy/deep [rule': :rule [rule' 
(rule': [end skip])]]]

>> rule: [(a': once-only 'a b': once-only 'b c': once-only 'c) 3 
[a' | b' | c']]
>> parse [a b c] rule
** Script Error: Invalid argument: a
Ladislav
28-Jun-2007
[2098]
in that case you can use either:

    a': once-only first ['a]

or

    a': once-only ['a]
Geomol
28-Jun-2007
[2099]
Yup, works. Nice! :-)
Steeve
28-Jun-2007
[2100]
Once (again)

Rebol is amazing, i think i found a simple and elegant dialect

Currently, it's not allowing recursive once usage, but it's obvious 
to do with a stack.


take: func [r] [n: 0 once/1: (length? r) + 1 / 3 once/2/2: r replace/all 
r '.. '.]
.: [(n: n + 1)]
..: [(n: n + 1) end skip]
once: [0 [(if n > 0 [poke once/2/2 n - 1 * 3 + 1  '..] n: 0) []]]


rule: [. "a" | . "b" | . "c"]
parse "CBA" [ (take rule) once]
== true
parse  "BAC" [ (take rule) once]
== true
parse  "CBA" [ (take rule) once]
== true
parse  "BBC" [ (take rule) once]
== true
parse  "CA" [ (take rule) once]
== false
parse  "CABA"[ (take rule) once]
== false
rule2: [. "a" | . "a" | . "b" | . "c"]
parse "CABA"[ (take rule2) once]
== true