Showing posts with label Compiler. Show all posts
Showing posts with label Compiler. Show all posts

Token

Token is a sequence of characters of finite length have a new collective meaning.

Grammar

grammar  is a set of  rules  that defines a legal class of strings in a formal language. A grammar
  is a four-tuples (N, Σ,P,S)
grammar G is written as follows:
G = <N, Σ,P,S>

where:
N - a finite set of non terminal symbols  
Σ - a finite set of terminal symbols
P - a finite set of production rules of the form y->x,where y and x are 

     in (NUΣ) and y contains at least one element in  N
S - is a designated goal system in N


 N and Σ are disjoint. 

Language

A language is some subset of  Σ*, where Σ  is its alphabet.

Discuss the steps involved in the analysis of a source program with the help of a block diagram/ language processing system



The steps involved in the analysis of source program are given below.

images Source program acts as an input to the preprocessor. Preprocessor modifies the source code by replacing the header files with the suitable content. Output (modified source program) of the preprocessor acts as an input for the compiler.
images Compiler translates the modified source program of high-level language into the target program. If the target program is in machine language, then it can be executed directly. If the target program is in assembly language, then that code is given to the assembler for translation. Assembler translates the assembly language code into the relocatable machine language code.
images Relocatable machine language code acts as an input for the linker and loader. Linker links the relocatable code with the library files and the relocatable objects, and loader loads the integrated code into memory for the execution. The output of the linker and loader is the equivalent machine language code for the source code.


When did first compiler start to appear?

Towards the end of the 1950s machine-independent programming languages were first proposed. Subsequently several experimental compilers were developed.

The first compiler was written by Grace Hopper, in 1952, for the A-0 programming language.

The FORTRAN team led by John Backus at IBM is generally credited as having introduced the first complete compiler in 1957.

COBOL was an early language to be compiled on multiple architectures, in 1960.

Now compiler can be implemented even as a student project.

Write short notes on compiler construction tools.

For the construction of a compiler, the compiler writer uses different types of software tools that are known as compiler construction tools

Compiler-construction tools are,
  1. Parser generators  
  2. Scanner generators
  3. Syntax-directed translation engines
  4.  Automatic code generators
  5.  Data-flow engines
images Scanner generators: They automatically produce lexical analyzers or scanners.
images Parser generators: They produce syntax analyzers or parsers.
images Syntax-directed translation engines: They produce a collection of routines, which traverses the parse tree and generates the intermediate code.
images Code generators: They produce a code generator from a set of rules that translates the intermediate language instructions into the equivalent machine language instructions for the target machine.
images Data-flow analysis engines: They gather the information about how the data is transmitted from one part of the program to another. For code optimization, data-flow analysis is a key part.
images Compiler-construction toolkits: They provide an integrated set of routines for construction of the different phases of a compiler.

Show the different phases of compiler with figure.

Compiler translates an input source program written in any high-level programming language into an equivalent target program in machine language. As compilation is a complex process, it is divided into several phases. A phase is a reasonably interrelated procedure that takes input in one representation and produces the output in another representation. The structure of compiler comprises various phases as shown in Figure




                                                         Figure 1.7 Phases of a Compiler
images Lexical analysis phase: Lexical analysis (also known as scanning) is the first phase of a compiler. Lexical analyzer or scanner reads the source program in the form of character stream and groups the logically related characters together that are known as lexemes. For each lexeme, a token is generated by the lexical analyzer. A stream of tokens is generated as the output of the lexical analysis phase, which acts as an input for the syntax analysis phase. Tokens can be of different types, namely, keywords, identifiers, constants, punctuation symbols, operator symbols, etc. The syntax for any token is:
images
where token_name is the name or symbol which is used during the syntax analysis phase and value is the location of that token in the symbol table.
images Syntax analysis phase: Syntax analysis phase is also known as parsing. Syntax analysis phase can be further divided into two parts, namely, syntax analysis and semantic analysis.
Syntax analysis: Parser uses the token_name token from the token stream to generate the output in the form of a tree-like structure known as syntax tree or parse tree. The parse tree illustrates the grammatical structure of the token stream.
Semantic analysis: Semantic analyzer uses the parse tree and symbol table for checking the semantic consistency of the language definition of the source program. The main function of the semantic analysis is type checking in which semantic analyzer checks whether the operator has the operands of matching type. Semantic analyzer gathers the type information and saves it either in the symbol table or in the parse tree.
images Intermediate code generation phase: In intermediate code generation phase, the parse tree representation of the source code is converted into low-level or machine-like intermediate representation. The intermediate code should be easy to generate and easy to translate into machine language. There are several forms for representing the intermediate code. Three address code is the most popular form for representing intermediate code. An example of three address code language is given below.
images
images Code optimization phase: Code optimization phase, which is an optional phase, performs the optimization of the intermediate code. Optimization means making the code shorter and less complex, so that it can execute faster and takes lesser space. The output of the code generation phase is also an intermediate code, which performs the same task as the input code, but requires lesser time and space.
images Code generation phase: Code generation phase translates the intermediate code representation of the source program into the target language program. If the target program is in machine language, the code generator produces the target code by assigning registers or memory locations to store variables defined in the program and to hold the intermediate computation results. The machine code produced by the code generation phase can be executed directly on the machine.
Symbol table management: A symbol table is a data structure that is used by the compiler to record and collect information about source program constructs like variable names and all of its attributes, which provide information about the storage space occupied by a variable (name, type, and scope of the variables). A symbol table should be designed in an efficient way so that it permits the compiler to locate the record for each token name quickly and to allow rapid transfer of data from the records.
Error handler: Error handler is invoked whenever any fault occurs in the compilation process of source program.
Both the symbol table management and error handling mechanisms are associated with all phases of the compiler.

Define Compiler

A compiler is a computer program (or set of programs) that transforms source code written in a programming language (the source language) into another computer language (the target language, often having a binary form known as object code).
 The most common reason for wanting to transform source code is to create an executable program.
A compiler is a program that translates a source program written in some high-level programming language (such as Java) into machine code for some computer architecture (such as the Intel Pentium architecture).