你什么时候可以声称你的程序是一个“编译器”?
根据 wikipedia
编译器是一种计算机程序(或程序集),它将用一种编程语言(源语言)编写的源代码转换为另一种计算机语言(目标语言,通常具有称为目标代码的二进制形式) )。想要转换源代码的最常见原因是创建可执行程序。
但是下面的代码可以被视为编译器吗?
class S {
public static void main( String ... args ) {
if( "1".equals(args[0]) ) {
System.out.println("echo Hi");
}
}
}
我知道这过于简单化了,但是,什么时候你可以说给定的程序实际上是“编译器”?
Accorging to wikipedia
A compiler is a computer program (or set of programs) that transforms source code written in a programming language (the source language) into another computer language (the target language, often having a binary form known as object code). The most common reason for wanting to transform source code is to create an executable program.
But could the following piece of code, be considered a compiler?
class S {
public static void main( String ... args ) {
if( "1".equals(args[0]) ) {
System.out.println("echo Hi");
}
}
}
I know this is an oversimplification, but, when can you say a given program is actually a "compiler" ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是由字符串“1”组成的语言,其语义是打印“Hi!”一种编程语言?我想说不是,所以这不是编译器。
当它接受真正的编程语言并将其转换为另一种不同的语言时,您可以将其称为编译器。这通常涉及解析源语言以得出语义,然后将其更改为其他语言。
Is the language consisting of the string "1" with semantics of printing "Hi!" a programming language? I'd say not, so that isn't a compiler.
When it accepts a real programming language, and transform it into another, distinct, language, then you call it a compiler. This usually involves parsing the source language to come up with a semantic meaning, then changing it into the other language.
人们可能会使用图灵完整性作为标准,但这有点严格(一种特殊用途的编程)语言可能不是图灵完备的),所以我会选择不太严格的东西。
我认为关键在于输出是一系列指令,并且输出和输入之间存在一些非平凡的对应关系。您给出的示例违反了后一个标准,因为无论输入多么复杂,都只有两种可能的输出:
“echo Hi”
并且什么也没有。这种对应关系可以非常接近(如 FORTRAN 和汇编之间),也可以非常遥远(Prolog 或 Lisp 和汇编),但只要可以通过连贯的输入语言产生大量行为,它就是编译器。
One might use Turing-completeness as a criterion, but that's a bit strict (a special-purpose programming language might not be Turing-complete), so I'll go with something a little less rigid.
I think the key is that the output be a series of instructions, and that there be some non-trivial correspondence between that output and the input. The example you give violates the latter criterion, because are only two possible outputs no matter how complex the input is:
"echo Hi"
and nothing.The correspondence can be very close (as between FORTRAN and assembly) or more distant (Prolog or Lisp and assembly), but as long as it's possible to produce a vast number of behaviors through a coherent input language, it's a compiler.