Tcl 脚本中错误的静态检测
我已经开发了一些代码,并且在 Linux 机器上遇到了 Tcl 解释器错误标记的问题。
#!/usr/bin/tclsh
if {1} {
puts "abc1"
} elseif {} {
puts "abc2"
}
上面的代码不会标记 "elseif"
条件的错误,直到它进入 elseif
条件。 有什么方法可以检查这种无意中发生的打字错误。
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 theelseif
condition.
Is there any way to check this kind of typo error done unintentionally.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Tcl 在编译时不会发现错误,并且在上面的示例中,它可以确定它永远不需要首先检查
elseif
子句;它只是直接首先puts
的问题。现在,如果存在一个重要的第一个条件,则
elseif
表达式中的错误只有在达到时才报告。这就是 Tcl 的语义(尤其是if
命令)的定义方式;执行命令时会报告计算错误(与粗略的主要语法相反)。我可以理解您对此的沮丧,并建议您查看有关静态语法分析工具的 Tcler 的 Wiki 页面 ,这可以为您标记潜在的问题(在几乎总是正确的非常适度的假设下)。特别是,我听说过关于 Frink 和 TDK 中的检查工具(后者是商业工具,但质量非常高)。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 firstputs
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 theif
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).为了详细说明 Donal 的答案,Tcl 在编译时不会发现错误,因为在一般情况下这是不可能完成的,在 if 之前执行的任何代码都可能重新定义了 if 命令,因此它可能是有效的,这是确定这是否有效的唯一方法情况是运行代码(即这是停止问题)
考虑这个脚本:
显然,在不运行程序的情况下静态确定 if {1} 行是否具有正确数量的参数是不可能的
TCL 实际上没有语法,编译器无法检查任何内容,你能做的最好的事情就是 Lint 风格的警告,它只在某些情况下才准确
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:
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
Tcl在编译时不会发现错误,但我们可以使用regexp检查语法。
匹配模式“ elseif { ”,如果存在,检查“}”大括号内是否有任何字符。如果不存在任何内容,则打印一条错误消息。
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.
有tcl静态语法检查器可以发现此类问题。
以下是此类检查的列表: http://wiki.tcl.tk/3162
ttclchecker http://www.xdobry.de/ttclcheck
为这个简短的脚本生成以下错误消息
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