What is the step by step working of a compiler?

 The compiler operates in various stages, and each stage converts the source program from one representation to another.

There are 6 steps(phases) in a compiler. Each step (phases) helps to convert high level to machine code.

  1. Lexical Analysis
  2. Syntax Analysis
  3. Semantic Analysis
  4. Intermediate Code Generator
  5. Code Optimizer
  6. Code generator

If you want to learn all 6 phases of the compiler I gave you a link and check out this link -

What are the Phases of Compiler Design? Compiler operates in various phases each phase transforms the source program from one representation to another. Every phase takes inputs from its previous stage and feeds its output to the next phase of the compiler. There are 6 phases in a compiler. Each of this phase help in converting the high-level langue the machine code. The phases of a compiler are: Lexical analysis Syntax analysis Semantic analysis Intermediate code generator Code optimizer Code generator Phases of Compiler All these phases convert the source code by dividing into tokens, creating parse trees, and optimizing the source code by different phases. In this tutorial, you will learn: Phase 1: Lexical Analysis Lexical Analysis is the first phase when compiler scans the source code. This process can be left to right, character by character, and group these characters into tokens. Here, the character stream from the source program is grouped in meaningful sequences by identifying the tokens. It makes the entry of the corresponding tickets into the symbol table and passes that token to next phase. The primary functions of this phase are: Identify the lexical units in a source code Classify lexical units into classes like constants, reserved words, and enter them in different tables. It will Ignore comments in the source program Identify token which is not a part of the language Example : x = y + 10 Tokens X identifier = Assignment operator Y identifier + Addition operator 10 Number Phase 2: Syntax Analysis Syntax analysis is all about discovering structure in code. It determines whether or not a text follows the expected format. The main aim of this phase is to make sure that the source code was written by the programmer is correct or not. Syntax analysis is based on the rules based on the specific programing language by constructing the parse tree with the help of tokens. It also determines the structure of source language and grammar or syntax of the language. Here, is a list of tasks performed in this phase: Obtain tokens from the lexical analyzer Checks if the expression is syntactically correct or not Report all syntax errors Construct a hierarchical structure which is known as a parse tree Example Any identifier/number is an expression If x is an identifier and y+10 is an expression, then x= y+10 is a statement. Consider parse tree for the following example (a+b)*c In Parse Tree Interior node: record with an operator filed and two files for children Leaf: records with 2/more fields; one for token and other information about the token Ensure that the components of the program fit together meaningfully Gathers type information and checks for type compatibility Checks operands are permitted by the source language Phase 3: Semantic Analysis Semantic analysis checks the semantic consistency of the code. It uses the syntax tree of the previous phase along with the symbol table to verify that the given source code is semantically consistent.

Comments

Popular posts from this blog

proassignment help backlink data index 41

Programming shark backlink data index 67

programming shark backink data index 31