Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

805 views
Skip to first unread message

dome...@gmail.com

unread,
Jun 21, 2005, 4:30:19 AM6/21/05
to
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.

Donald Arseneau

unread,
Jun 21, 2005, 6:38:58 AM6/21/05
to
"dome...@gmail.com" <dome...@gmail.com> writes:

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

> couldn't compile regular expression pattern: parentheses () not
> balanced

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


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

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 as...@triumf.ca

Andreas Leitgeb

unread,
Jun 21, 2005, 7:10:09 AM6/21/05
to
dome...@gmail.com <dome...@gmail.com> wrote:
> switch -regexp $current_net {
> [^\[]+\Z {

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).

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

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.

Message has been deleted
Message has been deleted

dome...@gmail.com

unread,
Jun 21, 2005, 12:54:43 PM6/21/05
to
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.

Bruce Hartweg

unread,
Jun 21, 2005, 1:37:38 PM6/21/05
to

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://wiki.tcl.tk/comment

Bruce

Neil Madden

unread,
Jun 22, 2005, 6:37:37 AM6/22/05
to

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

0 new messages