Compiler Design
Lab 1
Spring Semester 2024
Table Of
Compilation Process Lexical Analyzer Syntax and Sematic Analyzers
Compiler Phases Lexemes and Tokens Syntax and Semantic of
Programming Languages
Contents
01.
COMPILATION PROCESS
Compiler Phases
Language
Processing System
▪ The hardware understands a language, which humans
cannot understand.
▪ So, we write programs in high-level language, which is
easier for us to understand and remember.
▪ These programs are then fed into a series of tools and
OS components to get the desired code that can be
used by the machine. This is known as Language
Processing System.
User
Writes a program in C language (high-level language).
C Compiler
Compiles the program and translates it to assembly program (low-level language).
Language Assembler
Translates the assembly program into machine code (object).
Processing
Linker Tool
System (Cont.) Is used to link all the parts of the program together for execution (executable machine code).
Loader
Loads all of them into memory and then the program is executed.
Language Processing System (Cont.)
Compiler Interpreter
▪ Compiler scans the entire program and translates the ▪ Interpreter translates just one statement of the
whole of it. program at a time into machine code.
▪ Compiler takes a lot of time to analyze the source ▪ Interpreter takes very less time to analyze the source
code. However, the overall time taken to execute the code. However, the overall time to execute the
process is much faster. into machine code at once. process is much slower.
▪ Compiler always generates an intermediary object ▪ Interpreter does not generate an intermediary code.
code. It will need further linking. Hence more Hence, an interpreter is highly efficient in terms of its
memory is needed. memory.
▪ Compiler generates the error message only after it ▪ Keeps translating the program continuously till the
scans the complete program and hence debugging is first error is confronted. If any error is spotted, it
relatively harder while working with a compiler. stops working and hence debugging becomes easy.
▪ Compliers are used by programming languages like ▪ Interpreters are used by programming languages like
C and C++ for example. Ruby and Python for example.
Compiler
Phases
02.
Lexical Analyzer
Lexemes and Tokens
Examples of Created Tokens
Lexeme Token
int Reserved word
maximum Identifier
( LPAREN
int Reserved word
x Identifier
, Comma
int Reserved word
Y Identifier
) RPAREN
{ Open bracket
if if
Examples of Created Tokens
(Cont.)
Examples of Nontokens
Lexeme Token
Comment // This will compare 2 numbers
Pre-processor directive #include <stdio.h>
Pre-processor directive #define NUMS 8,9
Whitespace /n /b /t
Example 1
1 {
2 return
{ 3 x
return x + y; 4 +
} 5 y
6 ;
7 }
Lexemes
Example 1
1 {
2 return
{ 3 x
return x + y; 4 +
} 5 y
6 ;
7 }
Lexemes
Example 1 (Cont.)
1 { 1 open bracket
2 return 2 reserved
3 x word
{
4 + 3 identifier
return x + y;
5 y 4 plus_op
}
6 ; 5 identifier
7 } 6 semicolon
7 close bracket
Source Code Lexemes Tokens
Example 2
while (y <= t) y = y - 3 ; will be
represented by the set of pairs:-
Token Lexeme
identifier (id) index, count
constant (const) 2, 17
Example 3 comparison >
A sentence if index > 2 then count := 17 ; assign_op :=
semicolon ;
if if
then then
Example 4
Index = 2 * count + 17 ;
Example 5
Fortran statement: E = M * C ** 2
03.
Syntax and Semantic
Analyzers
Syntax and Semantic of Programming
Languages
Syntax
The form of its expressions, statements, and program
units.
Describing Semantic
The meaning of the expressions, statements, and
Syntax and program units.
Semantic
Syntax Error -
Example 1
#include <iostream>
int main()
{
// invalid operator (<), extraneous semicolon, undeclared variable (x)
std::cout < "Hi there"; << x;
return 0;
}
Syntax Error -
Example 2
#include <iostream>
int main()
{
cout << "Geeks for geeks!"
// missing semicolon at end of statement
return 0;
}
Semantic Error -
Example 1
#include <iostream>
int main()
{
int a = 10 ;
int b = 0 ;
std::cout << a << " / " << b << " = " << a / b;
// division by 0 is undefined
return 0;
}
Semantic Error -
Example 2
#include <iostream>
int main()
{
int a;
a = 10.5;
cout << a << endl;
return 0;
}
Thank You