views:

342

answers:

6

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

+2  A: 

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.

Joey
+2  A: 

A parser is one element of a compiler.

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

ChrisF
+2  A: 

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.

vava
+10  A: 

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 decent 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.

Richard Pennington
+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
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
The parser not only checks the syntax, but checking the syntax is more a byproduct of constructing an abstract representation, isn't it?
Sven Lilienthal
I think it is the other way around: An abstract representation may be a by-product of checking the syntax.
Richard Pennington
A: 

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.

valli
A: 

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.

Sven Lilienthal
parsers do not have to produce a tree structure
anon
Do you have an example for a parser which does not produce a tree?
Sven Lilienthal
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
A recursive decent parser does not have to produce a tree.
Richard Pennington
Yes, but do you have a concrete example?
Sven Lilienthal
Sure, the recursive descent Small C compiler I wrote about 25 years ago. But seriously, recursive descent is a well known technique - do you doubt its existence?
anon
@Richard: Thanks.
Sven Lilienthal
@Neil: No, just not refreshing before commenting and therefore missing the last comment
Sven Lilienthal