33

By concept/function/implementation, what are the differences between compilers and parsers?

3
  • 24
    What's the difference between a refrigerator and a compressor.
    – Nifle
    Dec 17, 2009 at 12:39
  • 9
    The refrigerator makes heavy use of the compressor to do it's job but a compressor is by no means a refrigerator. Substitute compiler for refigirator and parser for compressor.
    – Nifle
    Dec 17, 2009 at 12:41
  • 1
    I'd like to think the question the OP meant was:"What's the relationship between compilers and parsers?" (which most answers seem to deal with), but then the word 'difference' is not only in the title, but in the main question body too, in which case Nifle's analogy should be sufficient. ;-)
    – Wim
    Dec 17, 2009 at 14:06

7 Answers 7

61

A compiler is often made up of several components, one of which is a parser. A common set of components in a compiler is:

  • Lexer - break the program up into words.
  • Parser - check that the syntax of the sentences are correct.
  • Semantic Analysis - check that the sentences make sense.
  • Optimizer - edit the sentences for brevity.
  • Code generator - output something with equivalent semantic meaning using another vocabulary.

To add a little bit:

As mentioned elsewhere, small C is a recursive decent compiler that generated code as it parsed. Basically syntactical analysis, semantic analysis, and code generation in one pass. As I recall, it also lexed in the parser.

A long time ago, I wrote a C compiler (actually several: the Introl-C family for microcontrollers) that used recursive descent and did syntax and semantic checking during the parse and produced a tree representation of the program from which code was generated.

Today, I'm working on a compiler that does source -> tokens -> AST -> IR -> code, pretty much as I described above.

4
  • 2
    +1 for noting the different parts and also mentioning how they would relate to text (even though for a theoretical computer scientist a complete program would be a single word :-)).
    – Joey
    Dec 17, 2009 at 12:47
  • 1
    A parser would check for the syntax of sentences being correct, though. And the lexer might already look into a dictionary to see whether the words are made up or are indeed correct words to use. But that task is a little fuzzy between lexers and parsers, though.
    – Joey
    Dec 17, 2009 at 13:03
  • The parser not only checks the syntax, but checking the syntax is more a byproduct of constructing an abstract representation, isn't it? Dec 17, 2009 at 13:25
  • I think it is the other way around: An abstract representation may be a by-product of checking the syntax. Dec 17, 2009 at 13:48
6

A parser just reads a text into an internal, more abstract representation, often a tree or graph of some sort.

A compiler translates such an internal representation into another format. Most often this means converting source code into executable programs. But the target doesn't have to be machine code. It can be another programming language as well; the compiler would still be a compiler. Obviously a compiler needs a parser to actually read its input.

5

A parser is one element of a compiler.

Are you looking for the differences between an interpreter and a compiler?

5

Compiler always have a parser inside. Parser just process the language and return the tree representation of it, compiler generate something from that tree, actual machine codes or another language.

2

A parser takes in raw-data and parses it into a tree structure. This syntax-tree is then passed on to generator, which will turn it into whatever it is supposed to generate.

So, a parser is a part of a compiler.

7
  • parsers do not have to produce a tree structure
    – anon
    Dec 17, 2009 at 12:57
  • Do you have an example for a parser which does not produce a tree? Dec 17, 2009 at 13:03
  • What about a parser for a language that represents arbitrary graphs? Parsers generally just translate something textual into some internal representation. The latter one is most often a tree for languages with formally-defined context-free grammars but it can actually be anything that is convenient to handle.
    – Joey
    Dec 17, 2009 at 13:19
  • 1
    A recursive decent parser does not have to produce a tree. Dec 17, 2009 at 13:22
  • Yes, but do you have a concrete example? Dec 17, 2009 at 13:24
0

In general, parser is a part of the compiler, but compiler is designed to convert the received script generally into machine-readable code or sometimes into another language.

-2

A compiler is a special type of computer program that translates a human readable text file into a form that the computer can more easily understand. At its most basic level, a computer can only understand two things, a 1 and a 0. At this level, a human will operate very slowly and find the information contained in the long string of 1s and 0s incomprehensible. A compiler is a computer program that bridges this gap.

A parser is a piece of software that evaluates the syntax of a script when it is executed on a web server. For scripting languages used on the web, the parser works like a compiler might work in other types of application development environments.Parsers are commonly used in script development because they can evaluate code when the script is executed and do not require that the code be compiled first.

Your Answer

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

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