



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




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.
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.
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.
A Compiler can have many phases and passes.
program.
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;
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 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ā;
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
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
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.