Docsity

compiler construction and its phases, Exercises of Compiler Construction

Toggle sidebar
bg1
Compiler
A compiler is a special program that process statements written in a
particular programming language and turns them into the machine language or
ā€œCodeā€ that a computer’s processor uses.
Typically, a programmer writes language statements in a language such as
Pascal or C one line at a time using an editor. The file that is created contains
what are the source statements. The programmer then runs the appropriate
language compiler, specifying the name of the file that contains the source
statements.
A compiler can broadly be divided into two phases based on the way
compile.
1) Front End/Analysis Phase
2) Back End/Synthesis Phase
Front End of Compiler
Front end also known as the analysis phase of the compiler in which the
compiler reads the source program, divides it into core parts and then checks for
lexical, grammar and syntax errors. The analysis phase generates an intermediate
representation of the source program and symbol table, which should be fed to
the backend as input.
pf3
pf4
pf5

Partial preview of the text

Download compiler construction and its phases and more Exercises Compiler Construction in PDF only on Docsity!

Compiler

A compiler is a special program that process statements written in a particular programming language and turns them into the machine language or ā€œCodeā€ that a computer’s processor uses. Typically, a programmer writes language statements in a language such as Pascal or C one line at a time using an editor. The file that is created contains what are the source statements. The programmer then runs the appropriate language compiler, specifying the name of the file that contains the source statements. A compiler can broadly be divided into two phases based on the way compile.

  1. Front End/Analysis Phase
  2. Back End/Synthesis Phase

Front End of Compiler

Front end also known as the analysis phase of the compiler in which the compiler reads the source program, divides it into core parts and then checks for lexical, grammar and syntax errors. The analysis phase generates an intermediate representation of the source program and symbol table, which should be fed to the backend as input.

Backend of Compiler

Back-end also known as synthesis phase of the compiler, this phase generates the target program with the help of intermediate source code representation and symbol table. It will get the analysis phase input (intermediate representation and symbol table) and produces the targeted machine level code.

Compilation Process

A Compiler can have many phases and passes.

 Pass: A pass refers to the traversal of compiler through the entire

program.

 Phase: A phase of a compiler is a distinguishable stage, which takes input

from the previous stage, processes and yields output that can be used as input for the next stage. A pass can have more than one phase. The compilation process is a sequence of various phases. Each phase takes input from its previous stage, has its own representation of source program, and feeds its output to the next phase of the compiler. Let us understand the phases of a compiler.

Lexemes are said to be a sequence of characters (alphanumeric) in a token. There are some predefined rules for every lexeme to be identified as a valid token. For example in C language a variable declaration line is as int value = 100;

Syntax Analysis

The next phase is called the syntax analysis of parsing. It takes the token produced by lexical analyses as input and generates a parse tree (or syntax tree). In this phase, token arrangements are checks if the expression made by the tokens is syntactically correct. It takes all the token one by one and uses Context Free Grammar to construct parse tree. For Example the production rules for the Grammar of a Language are S -> cAd A -> bc|a And the input string is ā€œcadā€ Now the parser attempts to construct syntax tree from this grammar for the given input string. It uses the given production rules and applies those as need to generate the string

Semantic Analysis

Semantic analysis checks whether the parse tree constructed follows the rules of language. For example, assignment of values is between compatible data types, and adding string to an integer. Also, the semantic analyzer keeps tracks of identifiers, their types and expressions; whether identifiers are declared before use or not etc. The semantic analyzer produces an annotated syntax as an output. It judge whether the syntax structure constructed in the source program derives any meaning or not. CFG + Semantic rules = Syntax Directed Definitions

For Example int a = ā€œvalueā€;

Intermediate Code Generation

After semantic analysis the compiler generates an intermediate code of the source code for the target machine. It represents a program for some abstract machine. It is in between the high-level language and the machine language. This intermediate code should be generated in such a way that it makes it easier to be translated into the target machine code. For Example a = b + c * d; The intermediate code generator will try to divide this expression into sub- expressions and then generate the corresponding code. r1 = c * d; r2 = b + r1; a = r

Code Optimization

The next phase does code optimization of the intermediate code. Optimization can be assumed as something that removes unnecessary code lines, and arranges the sequence of statements in order to speed up the

Difference between Front End and

Back End

The front-end deals with the language itself: scanning, parsing, the parse- tree. The back end deals with the target system: object code formats, the machine code itself, The two things don’t have all that much to do with each other, and for a portable compiler it is highly desirable to use the same front- end with multiple backend, one per target. Separating the parser from the executable code generation makes it much easier to port a compiler from one processor architecture to another. The intermediate language is better than going straight to assembly for a particular target for a number of reasons: ļ‚· By abstracting the hardware and providing a "logical number" of registers we are independent of the final "physical number" of registers and hardware layout. For example, the native ADD instruction may be stack based, take 1-operand, take 2-operands, or even 3 operands. At this higher level we don't need to know, or care, about the lower level hardware implementation. ļ‚· The internal language can be optimized if you have an optimizer, and ļ‚· Is generic enough to be used on multiple targets if you have a wish to target different processors.