Static detection of errors in Tcl scripts
Asked Answered
A

4

8

I have developed some code, and I'm facing problem with error flagging of the Tcl interpreter on a Linux machine.

#!/usr/bin/tclsh
if {1} {
  puts "abc1"
} elseif {} {
  puts "abc2" 
}

The above code is not flagging error for "elseif" condition until it get into the elseif condition. Is there any way to check this kind of typo error done unintentionally.

Anetteaneurin answered 17/11, 2011 at 6:53 Comment(2)
A good syntax highlighting in your editor might help.Behlke
@Behlke such as...?Anthracosilicosis
R
4

To elaborate on Donal's answer, Tcl does not find errors at compile time because in the general case it cannot be done, any code executed before the if might have redefined the if command, so it could be valid, the only way to determine if this is the case is to run the code (i.e. this is the halting problem)

consider this script:

gets stdin input
if {$input == "fail"} {
  rename if if_
  proc if {arg1 arg2 arg3} {
    puts "ha ha"
  }
}
if {1} { puts "success"}

clearly it is impossible to statically determine if the if {1} line has the right number of arguments without running the program

TCL really has virtually no syntax, there is nothing a compiler can check, the best you can do is Lint style warnings, which will only be accurate in some cases

Rudman answered 17/11, 2011 at 14:27 Comment(0)
F
6

Tcl does not find errors at compilation time, and in the the sample above it can determine that it will never need to examine the elseif clauses in the first place; it just issues that first puts directly.

Now, in the case where there is a non-trivial first condition it is the case that the errors in the elseif expression are not reported until they are reached. This is how the semantics of Tcl — and especially the if command — are defined; errors in evaluation (as opposed to gross major syntax) are reported at the time of execution of the command. I can understand your frustration with this, and suggest that you check out the Tcler's Wiki page on static syntax analysis tools, which can flag up potential problems for you (under very modest assumptions that are virtually always true). In particular, I have heard good things about Frink and the checker tool in TDK (the latter being a commercial tool, but very high quality).

Firedrake answered 17/11, 2011 at 9:7 Comment(2)
Nagelfar is another option for a static Tcl syntax checker.Behlke
@schlenk: +1; I should have remembered to list that as well.Firedrake
R
4

To elaborate on Donal's answer, Tcl does not find errors at compile time because in the general case it cannot be done, any code executed before the if might have redefined the if command, so it could be valid, the only way to determine if this is the case is to run the code (i.e. this is the halting problem)

consider this script:

gets stdin input
if {$input == "fail"} {
  rename if if_
  proc if {arg1 arg2 arg3} {
    puts "ha ha"
  }
}
if {1} { puts "success"}

clearly it is impossible to statically determine if the if {1} line has the right number of arguments without running the program

TCL really has virtually no syntax, there is nothing a compiler can check, the best you can do is Lint style warnings, which will only be accurate in some cases

Rudman answered 17/11, 2011 at 14:27 Comment(0)
H
0

Tcl does not find errors at compilation time, But we can check the syntax using regexp. Match the pattern " elseif { ", If present check whether there is any characters within the "}" curly brace. If nothing present then print an error message.

Hazlip answered 15/10, 2013 at 11:16 Comment(0)
P
0

There are tcl static syntax checker that can find such problems.

Here is the list of such checkes: http://wiki.tcl.tk/3162

The ttclchecker http://www.xdobry.de/ttclcheck produces following error message for this short script

stackoverflow.tcl:4: error in expression missing operator <<{}>>
Palmette answered 2/3, 2015 at 22:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.