I am converting a SMAPI grammar to JSGF. They are pretty similar grammars used in different speech recognition systems. SMAPI uses a question mark they way the rest of the world does, to mean 0 or 1 of the previous thing. JSGF uses square brackets for this. So, I need to convert a string like stuff?
to [stuff]
, and parenthesized strings like ((((stuff)? that)? I)? like)?
to [[[[stuff] that] I] like]
. I have to leave alone strings like ((((stuff) that) I) hate)
. As Qtax pointed out, a more complicated example would be (foo ((bar)? (baz))?)
being replaced by (foo [[bar] (baz)])
.
Because of this, I have to extract every level of a parenthesized expression, see if it ends in a question mark, and replace the parens and question mark with square braces if it does. I think Eric Strom's answer to this question is almost what I need. The problem is that when I use it, it returns the largest matched grouping, whereas I need to do operations on each individual groupings.
This is what I have so far: s/( \( (?: [^()?]* | (?0) )* \) ) \?/[$1]/xg
. When matched with ((((stuff)? that)? I)? like)?
, however, it produces only [((((stuff)? that)? I)? like)]
. Any ideas on how to do this?
I