Navigation

What is a compiler? Is it a hardware or software? and how is it created such that ti understand programming languages

A compiler is a special program which converts high level programming code into machine code or lower level code which understood by machine & produce output like C/C++ compiler converts code into machine code
What is a compiler Is it a hardware or software

A compiler is a special program which converts high level programming code into machine code or lower level code which understood by machine & produce output like C/C++ compiler converts code into machine code (object file).

A compiler is likely to perform many or all of the following operations: preprocessing, lexical analysis, parsing, semantic analysis (syntax-directed translation), conversion of input programs to an intermediate representation, code optimization and code generation.



Preprocessing: Some languages, e.g., C, require a preprocessing phase which supports macro substitution and conditional compilation. Typically the preprocessing phase occurs before syntactic or semantic analysis; e.g. in the case of C, the preprocessor manipulates lexical tokens rather than syntactic forms. However, some languages such as Scheme support macro substitutions based on syntactic forms.

Lexical analysis: breaks the source code text into small pieces called tokens. Each token is a single atomic unit of the language, for instance a keyword, identifier or symbol name. The token syntax is typically a regular language, so a finite state automaton constructed from a regular expression can be used to recognize it. This phase is also called lexing or scanning, and the software doing lexical analysis is called a lexical analyzer or scanner. This may not be a separate step – it can be combined with the parsing step in scannerless parsing, in which case parsing is done at the character level, not the token level.

Syntax analysis: involves parsing the token sequence to identify the syntactic structure of the program. This phase typically builds a parse tree, which replaces the linear sequence of tokens with a tree structure built according to the rules of a formal grammar which define the language's syntax. The parse tree is often analyzed, augmented, and transformed by later phases in the compiler.
Semantic analysis: is the phase in which the compiler adds semantic information to the parse tree and builds the symbol table. This phase performs semantic checks such as type checking (checking for type errors), or object binding (associating variable and function references with their definitions), or definite assignment (requiring all local variables to be initialized before use), rejecting incorrect programs or issuing warnings. Semantic analysis usually requires a complete parse tree, meaning that this phase logically follows the parsing phase, and logically precedes the code generation phase, though it is often possible to fold multiple phases into one pass over the code in a compiler implementation.


In layman's terms: Compiler is the translator for the different language.

So, Compiler is the software that translates the program from High Level program to Machine Level program, i.e: From C/C++ to Assembly Code.

Every programming language has some predefined keywords built into it. Like, for C, int, float, while, for, union, etc.

How is it created such that it can understand programming languages?

A compiler should do:

  • Parsing: the source text is converted to an abstract syntax tree (AST).
  • Resolution of references to other modules (C postpones this step till linking).
  • Semantic validation: weeding out syntactically correct statements that make no sense, e.g. unreachable code or duplicate declarations.
  • Equivalent transformations and high-level optimization: the AST is transformed to represent a more efficient computation with the same semantics. This includes e.g. early calculation of common sub-expressions and constant expressions, eliminating excessive local assignments, etc.
  • Code generation: the AST is transformed into linear low-level code, with jumps, register allocation and the like. Some function calls can be in-lined at this stage, some loops unrolled, etc.
  • Peephole optimization: the low-level code is scanned for simple local inefficiencies which are eliminated.

To actually make one, you’d have to have knowledge about system programming (read the books).


>> Also you can read: What is a kernel? Is it hardware or software
مشاركة

أضف تعليق: