19

This question is inspired by the debate in the comments on this Stack Overflow question. The Google Closure Compiler documentation states the following (emphasis added):

The Closure Compiler is a tool for making JavaScript download and run faster. It is a true compiler for JavaScript. Instead of compiling from a source language to machine code, it compiles from JavaScript to better JavaScript.

However, Wikipedia gives the following definition of a "compiler":

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... A language rewriter is usually a program that translates the form of expressions without a change of language.

Based on that, I would say that Google Closure is not a compiler. But the fact that Google explicitly state that it is in fact a "true compiler" makes me wonder if there's more to it. Is Google Closure really a JavaScript compiler?

1
  • 2
    I bet the JS it outputs is a proper subset of all legal JS; in this sense, its output language is "another" language.
    – AakashM
    Nov 20, 2012 at 15:21

5 Answers 5

23

The Closure Compiler is a minifier, an optimiser and a validator all-in-one. That kind of puts it in its own category, because you're correct that a compiler should at least take something that won't run in its current form and turn it into something that will (take TypeScript for an ECMAScript-based example).

But do you blame Google for stretching the terminology? What else were they going to call it? Google Minifier? No, it's more than that, and there are hundreds of those out there. Google Optimiser? It's way more than that. Google Validator? No, it's way more than that too.

So the choice is

  • Call it Google Closure Foogle and introduce a whole new otherwise-meaningless word into the lexicon.
  • Call it Google Closure Minoptivalidator, which is clearer in intent but harder to remember.
  • Call it Google Closure Compiler, which is pretty close to the truth.

It does everything you would expect a compiler to do, with only a semantic difference. And, in the end, all words are defined by their usage, to some extent. So if Google can convince people to call this a compiler, the definition of compiler changes slightly. Certainly not in any way that will cause a problem.

Or, to come back to the earlier example, can you find anything significant about TypeScript that allows it to be called a "true compiler", while Google Closure Compiler should be restricted to "almost a compiler"?

6
  • The point of the question was just to really see what people view as a "compiler". As stated by 0A0D it's just semantics, but I just found myself wondering why Google felt the need to clarify that it was a "true compiler". +1 for "Minoptivalidator"! Nov 20, 2012 at 14:34
  • @JamesAllardice: Again, because if you can convince people that it is a compiler then the definition of compiler stretches to include it. And it's only really fair that it should, in this case. If you define a new word which separates it from compiler by a sematic line, your product gets categorised as "nearly, but not quite."
    – pdr
    Nov 20, 2012 at 14:38
  • 1
    I dont know, I'm kind of liking "Google Foogle" :-) Nov 20, 2012 at 16:29
  • 9
    I vote for Minoptival. It sounds like a dinosaur. Everyone likes dinosaurs, right?
    – Izkata
    Nov 20, 2012 at 18:04
  • 2
    @Izkata: If there's one thing the history of evolution has taught us, it's that Javascript will not be contained. Javascript breaks free, it expands to new territories, and crashes through barriers painfully, maybe even dangerously.
    – pdr
    Nov 20, 2012 at 18:09
9

Based on that, I would say that Google Closure is not a compiler.

Well yes ... but that assumes that Wikipedia is authoritative on this point.

And as a counter to the Wikipedia definition, consider some dictionary definitions (from http://www.thefreedictionary.com/compiler):

2) (Computer Science) - A program that translates another program written in a high-level language into machine language so that it can be executed.

2) (Electronics & Computer Science / Computer Science) a computer program by which a high-level programming language, such as COBOL or FORTRAN, is converted into machine language that can be acted upon by a computer.

A computer program associated with certain programming languages that converts the instructions written in those languages into machine code that can later be executed directly by a computer.

Granted these definitions are all a bit old-fashioned, but it does illustrate that there is no "one true meaning" ... and indeed that meanings of terms like "compiler" change over time. (And, IMO that is just fine, because we don't really need a precise definition for this case.)

Debating whether the Closure compiler is a "true compiler" or not is (IMO) not a fruitful activity. It would be more useful to understand what the Google folks mean by "true compiler" ... in that context.

2
  • 2
    +1, Ironically, JS is both 'machine language' for the web and a 'high-level programming language'.
    – K.Steff
    Nov 20, 2012 at 14:36
  • 3
    +1, and plus another for 'assumes that Wikipedia is authorative on this point'. Too many people blindly accepting whatever happens to be written there. Nov 20, 2012 at 17:46
6

I think the reason why it can be called "a real compiler" is because it builds a complete AST (abstract semantic tree) out of your programs, and uses it to generate a new text.

The fact that both the original and resultant texts are valid JavaScript is mere coincidence.

This is important because there are many tools that only do text manipulations of the code (minifiers, prettifiers, etc) but doesn't do any AST handling, much less code re-generation. Even if those tools are more and more powerful, they're not the same kind of software, and the limitations are different.

3
  • 1
    Thanks, this is an interesting answer. I think your explanation may offer a better definition of "compiler" than Wikipedia and the various dictionaries that others have quoted. Nov 21, 2012 at 8:31
  • Well, I like this answer and voted for it at some point, but I would note that it is perfectly possible to compile (in the traditional high-level-language-to-machine language-sense) without ever building a AST. See Let's Build a Compiler for instance. Now it is certainly true that the compiler from Crenshaw's tutorial is pretty crude, but I don't believe that any one was accuse it of not being a compiler. Jan 10, 2014 at 5:31
  • I haven't read that reference, but I would agree that an explicit AST isn't strictly necessary for compilation. Still, in most cases there's an equivalent structure, either explicit in data, or implicit in execution (maybe the call stack (or its evolution in time) reflects the syntax analysis). My main point is that compiling is not text manipulation, but generating code from an analysis of some source code.
    – Javier
    Jan 10, 2014 at 15:07
4

It's semantics in my opinion. In the traditional sense, it is not a compiler. However, in the same Wikipedia link, it says

A compiler is likely to perform many or all of the following operations: lexical analysis, preprocessing, parsing, semantic analysis (Syntax-directed translation), code generation, and code optimization.

Closure performs some or all of those operations.

A little farther down in the Wikipedia article

However, in practice there is rarely anything about a language that requires it to be exclusively compiled or exclusively interpreted, although it is possible to design languages that rely on re-interpretation at run time.

That being said, my guess is that it was easier to call it the "Closure Compiler" rather than the "Closure Optimizer" because really it is just optimizing JavaScript for browsers, not necessary translating it into another language or bytecode.

Closure Compiler as a noun is a non-sequitur.

1
  • It is just semantics, you're right. The point of the question was really just to see what people view a "compiler" as. I still don't think the Closure Compiler is a "true compiler", but it does perform many similar actions. Nov 20, 2012 at 14:30
3

First, lets get this out of the way source-to-source compiler exists. You cannot say something isn't a compiler because the end result isn't a code of lower level.

While it is correct that Closure Compiler generate AST and have code generation phrase, many Javascript minimizer out there already does that and I would probably not call them compilers neither.

What truly sets Closure Compiler apart is it's application of many well known compiler techniques in error detection and optimization. Here are some examples:

Type system:

Closure Compiler defines an annotated type system. It utilizes type inference techniques that many compiler uses to check your program for errors.

Interprocedural optimizations

Closure Compiler builds call graphs to rename and remove dead code in a whole program level.

Intraprocedural optimizations

Closure Compiler optimizes Javascript by apply different Control Flow Analysis as well as Data Flow analysis. Classic compiler techniques such as: Inlining, Register Allocation, Live Variable Analysis.. and many more are all utilized to squeeze that last byte out of the output Javascript.

Module System

A lesser known part of Closure Compiler that can be used to divide your code into seperate download for faster start-up time. It utilizes many graph algorithms as well. Something is also very common in "traditional" compilers.

All these compiler and static analysis techniques is what sets Closure Compiler apart from other Javascript minimizer. Taking away from the fact that the targeted input and output language is the same, I don't see why it isn't classified as a "true compiler".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.