4
votes

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" ?

2
A very simple compiler... not very generic ;) - Oded
If your source language contains only one valid program - "1", and your target language is your own subset of shell script with one command - echoing "Hi" - then yes. Otherwise, no. - Tesserex
It's a great question, but maybe it should be community wiki? - Justin Morgan
I think we are missing the point. The question is not whether given program is a compiler or not, instead where does a compiler definition start, I think it's very subjective depending on related question >"When can you differentiate between input and program (src code)"? - Sanjeevakumar Hiremath
Indeed Sanjeevakumar, I know my sample is plain stupid, but the question is not. There must be an objective criteria to call something a compiler ( or probably the answer is there isn't ) - OscarRyz

2 Answers

3
votes

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.

1
votes

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.