Difference between compilers and parsers?

2020-05-12 01:18发布

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

7条回答
神经病院院长
2楼-- · 2020-05-12 02:07

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.

查看更多
迷人小祖宗
3楼-- · 2020-05-12 02:08

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.

查看更多
看我几分像从前
4楼-- · 2020-05-12 02:19

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.

查看更多
该账号已被封号
5楼-- · 2020-05-12 02:20

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.

查看更多
Lonely孤独者°
6楼-- · 2020-05-12 02:21

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.

查看更多
该账号已被封号
7楼-- · 2020-05-12 02:23

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.

查看更多
登录 后发表回答