When can you claim your program is a "compiler"?
Accorging to wikipedia
A compiler is a computer program (or set of programs) that transfo开发者_如何学Gorms 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" ?
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.
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.
精彩评论