in reply to Balancing braces without (??{})
Update: I just ran into Text::Balanced, which I suppose does exactly this. It's part of the Parse::RecDescent package. You should already have it if you've installed Switch.
A true regular expression is represented by a connected graph of nodes, and you move from state to state with each input token. It can't count, because it has no memory at all other than knowing what state it's in.
To count braces, you need a "Push-down automata", the next step up. It has a graph and a stack.
So, you could implement a P.D.A. by starting with foreach $char (split ('',$input)) {, switch to the current state, and push/pop a @array for the state. More specifically, push the open symbol so you can check against it for a matching close.
Error if stack underflows. When out of input, make sure stack is also empty else warning. Return list of ballanced position ranges.if $char is an open symbol, push it along with position. else if $char is a close symbol, pop the stack and verify that it's the matching type (error if not). popped position through current position is ballanced range. Save +that on answer list.
—John
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Balancing braces without (??{})
by tshabet (Beadle) on Jul 28, 2001 at 01:04 UTC | |
by John M. Dlugosz (Monsignor) on Jul 28, 2001 at 01:47 UTC | |
by John M. Dlugosz (Monsignor) on Jul 29, 2001 at 00:18 UTC |