Is the following program in tcl:

#!/usr/bin/tclsh

set p {}

while {[gets stdin c] >= 0} {

set r $p$c

#puts [execute r]

set p {}

regexp {^([^\}]*\{)(?:[^{}]*(?:\{[^{}]*\})?)?$} $r -> p

puts -nonewline $p

flush stdout

}

It is seen that it is possible to open multiple brackets in one command(here "([^\}]*\{)"). Is it possible to do the same for the closing and each closing closed opening exactly one, without changing much code(ideally just a regular expression)?

#!/usr/bin/tclsh

set p {}

while {[gets stdin c] >= 0} {

set r $p$c

#puts [execute r]

set p {}

regexp {^([^\}]*\{)(?:[^{}]*(?:\{[^{}]*\})?)?$} $r -> p

puts -nonewline $p

flush stdout

}

It is seen that it is possible to open multiple brackets in one command(here "([^\}]*\{)"). Is it possible to do the same for the closing and each closing closed opening exactly one, without changing much code(ideally just a regular expression)?

asked October 8th 19 at 01:01

1 answer

answered on October 8th 19 at 01:03

I don't quite understand the question, maybe, but if you need to check the compliance of parentheses in the regular expression, then it is impossible in principle. Regular expressions can only describe regular grammar, it is the third type of grammars, they can disassemble the machine without memory, and matching brackets is the grammar of the second type, more powerful, they require a parser. You can then test the brackets if you have, say, no more than five pairs in the string (i.e. a finite number), but generally, no.

Although modern of the regular season quite powerful with a bunch of additional features, but I don't think there something like add — there are significantly different algorithms.

Although modern of the regular season quite powerful with a bunch of additional features, but I don't think there something like add — there are significantly different algorithms.

Find more questions by tags Abnormal programming