Parsing regexps and getting the fu**ing error message "parentheses () not balanced"

Parsing regexps and getting the fu**ing error message "parentheses () not balanced"

Post by domelev » Wed, 22 Jun 2005 17:30:19


Hello all!
I'm trying to write a script with this piece of code somewhere:

switch -regexp $current_net {
[^\[]+\Z {
# first case: <net_name> (1bit width)
switch [llength $pins($module_name,$k)] {
.....

The first "switch" saying that I want to parse any regexp with no '['
and going to the end of string $current_net.

Then Tcl says :
couldn't compile regular expression pattern: parentheses () not
balanced
while executing
"switch -regexp $current_net {

etc.

How can I get rid of this message? And it's not the first time I have
such odd problems :-)
Thanks.
 
 
 

Parsing regexps and getting the fu**ing error message "parentheses () not balanced"

Post by Donald Ars » Wed, 22 Jun 2005 19:38:58

" XXXX@XXXXX.COM " < XXXX@XXXXX.COM > writes:



What you show is not the source for this error (except that
it is a fragment with obviously mismatched delimiters).


No, it says (or tries to say) match any string in which "[" is not
the last character. But because of lack-of-quoting, it says even worse:
match any string which does not contain "[Z".

You need to anchor the start (with \A or ^) to force matching the
entire string, and therefore forbid all "[". You should have
luck with:

switch -regexp $current_net {
{\A[^[]+\Z} {

But why don't you reverse the cases

switch -regexp $current_net {
{\[} {
}
default {
#
}


--
Donald Arseneau XXXX@XXXXX.COM

 
 
 

Parsing regexps and getting the fu**ing error message "parentheses () not balanced"

Post by Andreas Le » Wed, 22 Jun 2005 20:10:09


try doubing the first backslash: [^\\[]+\Z

It may have to do with one extra "half" round
of substitution that happens inside the switch
body, namely parsing it as a list:
This will treat backslashes, but not square-brackets,
thus the RE was seen by switch as: [^[]+Z

Btw, you'll likely also need a leading '^', or it might then
match things like "[[[[[[[[[[[[[[[[42", (matching successfully
the substring made of the last two characters).

The problems in the past may be just like this one due to
insufficient protection.

PS: Of course, the content of the switch body is *not*
subject to substitution *before* switch sees it (due to
the braces), but switch itself *then* causes that extra
round - just before it interprets the pattern.
 
 
 

Parsing regexps and getting the fu**ing error message "parentheses () not balanced"

Post by domelev » Thu, 23 Jun 2005 01:54:43

Thanx all, I got it solved now. But I'll keep thinking the way you
place a comment does matter. For instance, a comment line just after
opening the body of the switch -regexp gives out shitty error messages,
which I do not consider as normal.
 
 
 

Parsing regexps and getting the fu**ing error message "parentheses () not balanced"

Post by Bruce Hart » Thu, 23 Jun 2005 02:37:38


because when you place it in the body of the switch statement
it is NOT a comment - it is a series of pattern action pairs -
and if it is not an even number, you have problems. (Actually
even if you have even numbers you can still have problems, just
of a different kind).

For a detailed explanation see http://www.yqcomputer.com/

Bruce
 
 
 

Parsing regexps and getting the fu**ing error message "parentheses () not balanced"

Post by Neil Madde » Thu, 23 Jun 2005 19:37:37


Well, this being Tcl -- write your own switch:

proc myswitch {args} {
set body [lindex $args end]
regsub -all -line {[^\\]?#.*$} $body { } body
uplevel 1 switch [lreplace $args end end $body]
}

myswitch -regexp -- $url {
# Comment in a switch body...
{http://(.*)} { puts "HTTP URL" }
default { puts "Unknown" }
}

Now you can put comments wherever you want in the switch body. The
regexp could be improved to take proper care of the # character. For
instance, something like:

\\# this is a comment

will be missed. But I don't expect this will be a common situation.

-- Neil