Compiler Construction: BY Ahsan Khan Email: Ahsan@Cuiatd - Edu.Pk
Compiler Construction: BY Ahsan Khan Email: Ahsan@Cuiatd - Edu.Pk
AHSAN KHAN
EMAIL: [email protected]
Basic introduction to course
Lectures 32 – Theory
32 – LAB
Linked List
shown in the below image
Data
Structure
// A linked list node
class Node
{
public:
int data;
Node *next;
};
// A simple CPP program to introduce
// a linked list
#include <bits/stdc++.h>
using namespace std;
int main()
{
Node* head = NULL;
Node* second = NULL;
Node* third = NULL;
// allocate 3 nodes in the heap
head = new Node();
second = new Node();
third = new Node();
/* Three blocks have been allocated dynamically.
We have pointers to these three blocks as head,
second and third
head second third
| | |
| | |
+---+-----+ +----+----+ +----+----+
| # | # | | # | # | | # | # |
+---+-----+ +----+----+ +----+----+
head->data = 1; // assign data in first node
head->next = second; // Link first node with
// the second node
/* data has been assigned to the data part of first
block (block pointed by the head). And next
pointer of the first block points to second.
So they both are linked.
head second third
| | |
| | |
+---+---+ +----+----+ +-----+----+
| 1 | o----->| # | # | | # | # |
+---+---+ +----+----+ +-----+----+
*/
// assign data to second node
// assign data to second node
second->data = 2;
// Link second node with the third node
second->next = third;
/* data has been assigned to the data part of the second
block (block pointed by second). And next
pointer of the second block points to the third
block. So all three blocks are linked.
head second third
| | |
| | |
+---+---+ +---+---+ +----+----+
| 1 | o----->| 2 | o-----> | # | # |
+---+---+ +---+---+ +----+----+ */
third->data = 3; // assign data to third node
third->next = NULL;
/* data has been assigned to the data part of the third
block (block pointed by third). And next pointer
of the third block is made NULL to indicate
that the linked list is terminated here.
We have the linked list ready.
head
|
|
+---+---+ +---+---+ +----+------+
| 1 | o----->| 2 | o-----> | 3 | NULL |
+---+---+ +---+---+ +----+------+
Note that only the head is sufficient to represent
Computer Programing language
High level
language Programs written in a high-level language tend to be shorter than equivalent
programs written in machine language.
Another advantage of using a high-level level language is that the same program
can be compiled to many different machine languages and, hence, be brought to
run on many different machines.
Conversion of high level to machine code
Language processing Steps
1.The C++ preprocessor copies the contents of the included header files into the
source code file, generates macro code, and replaces symbolic constants
defined using #define with their values.
2.The expanded source code file produced by the C++ preprocessor is compiled
into the assembly language for the platform.
3.The assembler code generated by the compiler is assembled into the object
code for the platform.
4.On compilation of source code, the machine code generated for different
processors like Intel, AMD, an ARM is different. To make code portable, the
source code is first converted to Object Code. It is an intermediary code
(similar to machine code) that no processor will understand. At run time, the
object code is converted to the machine code of the underlying platform
5. The object code file generated by the assembler combined with functions
from the standard library archive files by the linker to produce an executable
file. By default, this executable file is named a.out. In this case, we have used
the -o option to specify the name of the executable file as prog1.
Step 1: Preprocessor
• Word to PDF
• PDF to Postscript
Step 03: Assembler
Interpreter Compiler
Interpreters usually take less amount of time to analyze the source Compilers usually take a large amount of time to analyze the source
code. However, the overall execution time is comparatively slower code. However, the overall execution time is comparatively faster than
than compilers. interpreters.
Structure of
1. Analysis
compiler
2. Synthesis
Analysis phase of compiler
The synthesis part constructs the desired target program from the intermediate
representation and the information in the symbol table.
It will get the analysis phase input(intermediate representation and symbol table)
and produces the targeted machine level code.
This is also called as the back end of a compiler.