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

Thursday, 24 August 2017

How a Compiler Translates a Programming Language Statement?

Lets assume a statement m=a+b*c-4 written in some programming language (e.g. C ). So for running this statement we have to translate the statement into binary code or machine language which will then gets executed. The gcc or Turbo C compiler translate the above statement with the help of following six phases.

  1. Lexical Analysis: Every high level programming language is made up of some constructs like operators, keywords, expression, methods, syntax and the rules to write any statement. To translate the statement first the machine must understands the meaning of all the components of the statement m=a+b*c-4. These components are called as tokens means the meaningful constructs of any programming language. m, a, b, c are identifiers; =, +, - are operators. So these constructs are called as tokens. Identifiers, keywords, functions, delimiters, numbers etc are the tokens. Lexical analysis convert the statement into statement of tokens with lexeme. Lexeme means the pattern for a particular type of token. e.g. = is a lexeme of type operator token and m is a lexeme of type identifier token with a pattern represented by Regular Expression. The regular expression for digit (here 4) is [0-9]+.  So the lexical expression is like id=id+id*id-digit. The identifiers are stored in symbol table to record the attributes of the identifier like its data type, value etc which will be used in further phases.

Sunday, 16 April 2017

Running Time of a Parallel Program and a Serial Program. What, Why, How?

The Running time of any computer program is depends on the number of steps or lines of codes of the program which is depends upon the algorithm of that code. For solving any real world problem we have to formulate that problem and design the algorithm. While designing the algorithm, it is very important to analyse the algorithm based on its time complexity. The efficient algorithm is fast means its time complexity is less. So to minimise the time complexity the programmer has to design an algorithm with minimum number of steps.

There are two aspects for designing the program. The programmer can design it in serial way or in parallel way. In the serial program the next step is depends on the previous step, so there is the dependency criteria for the design. However,  in parallel program the dependency is less or no. So in parallel program we can easily divided the program into parts and these parts can execute independently and parallel, results in less running time and complexity.

How to Design Parallel Programs? See Here .....

Tuesday, 4 April 2017

Syntax Analysis using YACC

YACC (Yet Another Compiler Compiler) written in C is a compiler for checking the input format given to the program written in any programming language.
Syntax Analysis is the second phase of the compiler which is machine independent. The input to this phase is the tokens and the output is the syntax tree also called as parse tree. Parsing (another name for Syntax Analysis) is the process of constructing the parse tree according to the rules specified by the programming language.
For example consider the following statement
3+4, so if we specify the rule as "digit op digit" in infix format then the input 3+4 is correct and it will give the correct result that is 7.
If we give the input as 34+ then the parser gives the syntax error as the rule is specified for infix format and not for the postfix format.

Saturday, 1 April 2017

How to define Tokens for C Language using LEX

LEX is a compiler to test the rules for the tokens for any given programming language. LEX is written in C. Token generation is the first phase of the Compiler called as Lexical Analysis. The input for this phase is the program statements written in High Level Language like C, C++ or JAVA etc and the output is the Tokens means meaningful constructs of any programming language.

For example, consider the following statement written in C language.

int a=2;

then the tokens are keyword, variable, operator, digit and the delimiter. int, a, = and 2 are called as the lexemes for the above tokens.