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

Whats wrong with that code(II)?

4 views
Skip to first unread message

Gerhard Reithofer

unread,
Nov 19, 2009, 11:22:48 AM11/19/09
to
Hi TCLers,
I've studied Tcl language syntax several times, but I don't see my
fault:

% set obrace 3
3
% if {$obrace<0} {return "missing '}'"}
extra characters after close-brace

Why doest this code throw an error?
TCL-Rule [4]: If the first character of a word is double-quote (“"”)
then the word is terminated by the next double-quote character.

"missing '}'" is ONE word and the interpreter should not look "inside"
the string.

What did I oversee?

Nevertheless
return "missing '\}'"
is working as expected.

--
Gerhard Reithofer
Tech-EDV Support Forum - http://support.tech-edv.co.at

Mark Janssen

unread,
Nov 19, 2009, 12:23:07 PM11/19/09
to
On Nov 19, 5:22 pm, Gerhard Reithofer <gerhard.reitho...@tech-

You are experiencing a common form of Tcl syntax blindness here. The
word doesn't start at the first ", it starts at the first { so the
actual word is:

{return "missing '}

Which indeed has characters after the close brace.

Regards,
Mark

Ronnie Brunner

unread,
Nov 19, 2009, 12:22:52 PM11/19/09
to
Gerhard Reithofer wrote:
> Hi TCLers,
> I've studied Tcl language syntax several times, but I don't see my
> fault:
>
> % set obrace 3
> 3
> % if {$obrace<0} {return "missing '}'"}
> extra characters after close-brace
>
> Why doest this code throw an error?
> TCL-Rule [4]: If the first character of a word is double-quote (“"”)
> then the word is terminated by the next double-quote character.
>
> "missing '}'" is ONE word and the interpreter should not look "inside"
> the string.
>
> What did I oversee?
>
> Nevertheless
> return "missing '\}'"
> is working as expected.
>

But Tcl-Rule [6] says: [6] Braces.
If the first character of a word is an open brace (“{”) and rule
[5] does not apply, then the word is terminated by the matching close
brace (“}”). Braces nest within the word: for each additional open brace
there must be an additional close brace (however, if an open brace or
close brace within the word is quoted with a backslash then it is not
counted in locating the matching close brace). No substitutions are
performed on the characters between the braces except for
backslash-newline substitutions described below, nor do semi-colons,
newlines, close brackets, or white space receive any special
interpretation. The word will consist of exactly the characters between
the outer braces, not including the braces themselves.


And since in Tcl-Rule [4] there is no talk about braces being treated as
ordinary characters within a quoted string, it's obvious that Tcl-Rule
[6] takes precedence as there was a word opened with '{' first

hth
Ronnie
--
Ronnie Brunner | ronnie....@netcetera.ch
phone +41-44-247 79 79 | fax +41-44-247 70 75
Netcetera AG | 8040 Zürich | Switzerland | http://netcetera.ch

Elchonon Edelson

unread,
Nov 19, 2009, 12:44:39 PM11/19/09
to
Gerhard Reithofer wrote:
> Hi TCLers,
> I've studied Tcl language syntax several times, but I don't see my
> fault:
>
> % set obrace 3
> 3
> % if {$obrace<0} {return "missing '}'"}
> extra characters after close-brace
>
> Why doest this code throw an error?
> TCL-Rule [4]: If the first character of a word is double-quote (“"”)
> then the word is terminated by the next double-quote character.
>
> "missing '}'" is ONE word and the interpreter should not look "inside"
> the string.
>
> What did I oversee?
>
> Nevertheless
> return "missing '\}'"
> is working as expected.
>

What you overlooked is rule 6: If the first character of a word is an
open brace (“{”) ... then the word is terminated by the matching close
brace (“}”). Braces nest within the word ... (however, if an open brace

or close brace within the word is quoted with a backslash then it is not
counted in locating the matching close brace). No substitutions are

performed on the characters between the braces ... *nor do semi-colons,
newlines, close brackets, or white space* receive any special
interpretation. The word will consist of *exactly the characters between
the outer braces*, not including the braces themselves.

I have elided a few pieces of the rule, but the main point is still
clear: The word begins with “{”, the space after “return” received no
special interpretation and is simply part of the word. The double-quote
is therefore not the first character of anything, and is also simply
part of the word. The word ends at the close-brace that is enclosed in
single-quotes, and is the string “return "missing '”. This is
immediately followed by “'"}” with no intervening whitespace, which is
the error message that you're getting.

Your solution, “return "missing '\}'"” is, as you can see above, the
correct way to escape the close-brace.

-EE

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Bryan Oakley

unread,
Nov 19, 2009, 1:34:08 PM11/19/09
to
On Nov 19, 10:22 am, Gerhard Reithofer <gerhard.reitho...@tech-

edv.co.at> wrote:
> Hi TCLers,
> I've studied Tcl language syntax several times, but I don't see my
> fault:
>
> % set obrace 3
> 3
> % if {$obrace<0} {return "missing '}'"}
> extra characters after close-brace
>
> Why doest this code throw an error?
> TCL-Rule [4]: If the first character of a word is double-quote (“"”)
> then the word is terminated by the next double-quote character.
>
> "missing '}'" is ONE word and the interpreter should not look "inside"
> the string.
>
> What did I oversee?

First word: if
second word: {$obrace<0}
third word: {return "missing '}

immediately after that closing brace of the third word is the
character ', hence the error message.

Remember: when a word starts with a brace it continues to the closing
brace, and double quotes are ignored with the opening and closing
brace.

Gerhard Reithofer

unread,
Nov 19, 2009, 7:19:54 PM11/19/09
to
Hi all,

On Thu, 19 Nov 2009, Bryan Oakley wrote:

> On Nov 19, 10:22 am, Gerhard Reithofer <gerhard.reitho...@tech-
> edv.co.at> wrote:
> > Hi TCLers,
> > I've studied Tcl language syntax several times, but I don't see my
> > fault:
> >
> > % set obrace 3
> > 3
> > % if {$obrace<0} {return "missing '}'"}
> > extra characters after close-brace

...

> First word: if
> second word: {$obrace<0}
> third word: {return "missing '}
>
> immediately after that closing brace of the third word is the
> character ', hence the error message.
>
> Remember: when a word starts with a brace it continues to the closing
> brace, and double quotes are ignored with the opening and closing
> brace.

thanks, it really was the "common Tcl syntax blindness" ;)

But what about the following:
if {$o eq "{"} {puts "doesn't work"}

Word 1: if
Word 2: {$c eq "{"}
Word 3: {puts "doensn't work"}

If I interprete that correct, is any double quote ignored within
braces - therefore the "matching" closing brace is missing.

A few solutions:
if { $o eq "\{" } {puts "1 works"}
if [expr { $o eq "\{" }] {puts "2 works"}
if {[expr { $o eq "\{" }]} {puts "3 works"}
if [expr \"$o\" eq \"{\" ] {puts "4 works"}

In other words, it is necessary to quote ANY brace within a braced
block, the only exception is a single command without braces (see 4.)

puts "{ is ok"
if 1 {puts "{ is not"}

Ronnie Brunner

unread,
Nov 20, 2009, 4:29:07 AM11/20/09
to
Gerhard Reithofer wrote:
> But what about the following:
> if {$o eq "{"} {puts "doesn't work"}
>
> Word 1: if
> Word 2: {$c eq "{"}
> Word 3: {puts "doensn't work"}

Nope:
Word 1: if
Word 2: <not complete>

Rule [6] again: "Braces nest within the word: for each additional open
brace there must be an additional close brace [...]" which is not the
case for your '{' in double quotes

if {$o eq "\{"} {puts "works"}

is what you want

> In other words, it is necessary to quote ANY brace within a braced
> block, the only exception is a single command without braces (see 4.)

Nope, as long as they are matched: no need to escape them

if {$o eq "{}"} {puts "works too"}

hth
Ronnie
--
Ronnie Brunner | ronnie....@netcetera.ch
phone +41-44-247 79 79 | fax +41-44-247 70 75

Netcetera AG | 8040 Z�rich | Switzerland | http://netcetera.ch

Gerhard Reithofer

unread,
Nov 20, 2009, 2:02:54 PM11/20/09
to
On Fri, 20 Nov 2009, Ronnie Brunner wrote:
> Gerhard Reithofer wrote:
> > But what about the following:
> > if {$o eq "{"} {puts "doesn't work"}
> >
> > Word 1: if
> > Word 2: {$c eq "{"} Word 3: {puts "doensn't work"}
>
> Nope:
> Word 1: if
> Word 2: <not complete>
>
> Rule [6] again: "Braces nest within the word: for each additional open brace
> there must be an additional close brace [...]" which is not the case for your
> '{' in double quotes
>
> if {$o eq "\{"} {puts "works"}
>
> is what you want
>
> > In other words, it is necessary to quote ANY brace within a braced block,
> > the only exception is a single command without braces (see 4.)
>
> Nope, as long as they are matched: no need to escape them
>
> if {$o eq "{}"} {puts "works too"}

ACK
I thought of the typical single braces (e.g. for parsing, ...)

THX

0 new messages