Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between compilers and parsers?

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

like image 691
Dhanapal Avatar asked Dec 17 '09 12:12

Dhanapal


People also ask

Is parser a compiler?

Parser is a compiler that is used to break the data into smaller elements coming from lexical analysis phase.

What is difference between interpreter and parser?

A parser, in turn, takes a sequence of tokens and produces an abstract syntax tree (AST) of a language. The rules by which a parser operates are usually specified by a formal grammar. An interpreter is a program that interprets the AST of the source of a program on the fly (without compiling it first).

What is meant by parser?

A parser is a compiler or interpreter component that breaks data into smaller elements for easy translation into another language. A parser takes input in the form of a sequence of tokens, interactive commands, or program instructions and breaks them up into parts that can be used by other components in programming.

What is the difference between a compiler and interpreter in compilation carefully explain what a parser does?

Both compiler and interpreters do the same job which is converting higher level programming language to machine code. However, a compiler will convert the code into machine code (create an exe) before program run. Interpreters convert code into machine code when the program is run.


2 Answers

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.

like image 69
Richard Pennington Avatar answered Nov 21 '22 19:11

Richard Pennington


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.

like image 39
vava Avatar answered Nov 21 '22 20:11

vava