Search Header Logo
Compiling the Future: A Journey into the World of Compilers

Compiling the Future: A Journey into the World of Compilers

Assessment

Presentation

Computers

12th Grade

Practice Problem

Medium

Created by

Margaret Sifuna

Used 1+ times

FREE Resource

23 Slides • 4 Questions

1

Compiling the Future

A journey into the world of compilers


How does source code written by a programmer become binary code that a computer can execute?

2

media

OCR H046/H466

SLR5 Application generation | Translators

From human to machine

As humans, we pick a programming
language and write source code.

Source code is descriptive and easy
for us to understand, read, maintain
and debug.

However, it’s no good for machines –
they need the source code
converted into pure binary so they
can understand and execute it.

This pure binary form of our source
code is called machine code.

The process of converting source
code to machine code is known as
translation.

3

media

OCR H046/H466

SLR5 Application generation | Translators

Low-level language

High-level language

Source code

Translators

Assembly code

Translator:
Assembler

Translator:
Interpreter

Linker

Library

Machine code

(Binary)

Interpreter

Translator:
Compiler

Object code
Intermediate code

4

media

OCR H046/H466

SLR5 Application generation | Translators

Low-level language

High-level language

Source code

Translators

Assembly code

Translator:
Assembler

Translator:
Interpreter

Library

Machine code

(Binary)

Interpreter

Translator:
Compiler

Object code
Intermediate code

5

Introduction to Compilers

  • Compilers are programs that translate source code written in a high-level language into machine code.

  • They perform various tasks such as lexical analysis, syntax analysis, semantic analysis, code optimisation, and code generation.

  • Compilers play a crucial role in software development, enabling efficient execution of programs.

6

Lexical Analysis

  • Lexical analysis is the first phase of a compiler, where the source code is broken down into tokens.

  • Tokens are lexical units such as keywords, identifiers, operators, and literals.

  • Lexical analysis involves scanning the source code, removing whitespace and comments, and identifying tokens.

  • Lexical analysis is crucial for the compiler to understand the structure and meaning of the code.

7

Multiple Choice

What is the first phase of a compiler?

1

Syntax analysis

2

Semantic analysis

3

Lexical analysis

4

Code generation

8

Compilers: Translate and Transform

Trivia:

  • Compilers are like language translators, converting human-readable source code into machine-executable instructions.

  • They analyse, optimise, and transform code to improve performance.

  • Compilers play a crucial role in software development, enabling efficient execution and portability across different platforms.

9

First Phase: Lexical Analysis


Trivia:


Did you know that the first phase of a compiler is similar to how a human reads a sentence by breaking it into words?

10

Multiple Choice

What is the main function of compilers?

1

Translate source code into machine code

2

Debugging programs

3

Creating user interfaces

4

Managing databases

11

Syntax Analysis:

  • Syntax Analysis:

    • Also known as parsing, is the second phase of the compilation process. It involves analysing the structure of the source code to ensure it conforms to the grammar rules of the programming language.

    • Purpose: The main goal is to identify the syntactic structure of the code and determine if it follows the specified syntax of the programming language.

  • It generates a parse tree to represent the program's structure.

12

Syntax Analysis: Example

# Example Expression

expression = "3 + 5 * (7 - 2)"

# Tokenization

tokens = ['3', '+', '5', '*', '(', '7', '-', '2', ')']

# Parsing and Building Parse Tree
parse_tree = { 'expression': { 'term': { 'factor': { 'primary': {'NUMBER': '3'} }

13

media

OCR H046/H466

SLR5 Application generation | Translators

Interpreter

bbc.godbolt.org

It then tries to translate line
30, but there is a syntax error
here, so the program fails.

BBC Basic Simulator: bbc.godbolt.org

14

media

OCR H046/H466

SLR5 Application generation | Translators

Interpreter

bbc.godbolt.org

If we run it, lines 20 and 30 are
both translated and run.

You can see it outputs the
value of counter here.

BBC Basic Simulator: bbc.godbolt.org

15

media

OCR H046/H466

SLR5 Application generation | Translators

Interpreter

This is exactly the same
program as the one we just
saw in Python – this time,
written in an early high-level
language called BBC Basic.

bbc.godbolt.org

BBC Basic Simulator: bbc.godbolt.org

16

Multiple Choice

What is the second phase of the compiling process?

1

Lexical analysis

2

Syntax analysis

3

Semantic analysis

4

Code generation

17

Semantic Analysis

Trivia:
This phase ensures that the code follows the rules of the programming language and detects any semantic errors.
It plays a crucial role in producing correct and efficient code.
Lexical analysis is the first phase, which breaks the code into tokens.

18

Semantic Analysis:

  • Semantic analysis is a crucial phase in the compilation process.

  • It gives meaning to the code by checking its correctness and validity.

  • During this phase, the compiler analyses the code's structure, types, and context.

  • It detects and reports semantic errors that may cause runtime issues.

  • Static type checking and symbol table construction are key tasks performed in semantic analysis.

19

Multiple Choice

What is the main purpose of semantic analysis in the compilation process?

1

To check the correctness and validity of the code

2

To analyse the code's structure, types, and context

3

To detect and report semantic errors that may cause runtime issues

4

To perform dynamic type checking and symbol table construction

20

Semantic Analysis:

  • To analyse the code's structure, types, and context. It ensures code correctness and detects semantic errors that may cause runtime issues.

  • It performs dynamic type checking and constructs the symbol table.

  • Without it, code execution could lead to unexpected results.

21

media

OCR H046/H466

SLR5 Application generation | Translators

Low-level language

High-level language

Source code

Translators

Assembly code

Translator:
Assembler

Translator:
Interpreter

Linker

Library

Machine code

(Binary)

Interpreter

Translator:
Compiler

Object code
Intermediate code

22

media

OCR H046/H466

SLR5 Application generation | Translators

Low-level language

High-level language

Source code

Translators

Assembly code

Translator:
Assembler

Translator:
Interpreter

Intermediate code

Linker

Library

Machine code

(Binary)

Interpreter

Translator:
Compiler

Object code

23

media
media
media

OCR H046/H466

SLR5 Application generation | Translators

Compiler

Our code will not run while we
have a syntax error.

An error on line 2 is preventing our
program from running.

24

media

OCR H046/H466

SLR5 Application generation | Translators

Compiler

This error needs to be corrected
before the program will run.

25

media

OCR H046/H466

SLR5 Application generation | Translators

Interpreter

bbc.godbolt.org

If we correct the error on line
30 and run the program again
it executes without errors.

BBC Basic Simulator: bbc.godbolt.org

26

Code Optimisation

  • Definition:

    The process of improving the efficiency and performance of a program by making it faster and smarter.

  • Techniques:

    Loop unrolling, constant folding, function inlining, and dead code elimination.

  • Benefits:

    Reduced execution time, improved memory usage, and enhanced overall program performance.

27

media

OCR H046/H466

SLR5 Application generation | Translators

Translators: Assemblers, compilers and interpreters

Assembler

Compiler

Interpreter

Description

Translates assembly language into machine code.
Takes basic commands and operations from

assembly code and converts them into binary
code that can be recognised by a specific type of
processor.

The translation process is typically a one-to-one

process from assembly code to machine code.

Translates source code from high-level
languages into object code and then
machine code to be processed by the CPU.

The whole program is translated into
machine code before it is run.

Translates source code from high-level
languages into machine code, ready to be
processed by the CPU.

The program is translated line by line as the
program is running.

Advantages

Programs written in machine language can be
replaced with mnemonics, which are easier to
remember.

Memory-efficient.

Speed of execution is faster.

Hardware-oriented.

Requires fewer instructions to accomplish the
same result.

No need for translation at run-time.

Speed of execution is faster.

Code is usually optimised.

Original source code is kept secret.

Easy to write source code, as the program
will always run, stopping when it finds a
syntax error.

Code does not need to be recompiled when
code is changed.

It is easy to try out commands when the
program has paused after finding an error –
this makes interpreted languages very easy
for beginner programmers to learn to write
code.

Disadvantages

Long programs written in such languages cannot
be executed on small computers.

It takes lot of time to code or write the program,
as it is more complex in nature.

Difficult to remember the syntax.

Lack of portability between computers of
different makes.

Source code is easier to write in a high-level
language, but the program will not run with
syntax errors, which can make it more
difficult to write the code.

Code needs to be recompiled when the code
is changed.

Designed for a specific type of processor.

Translation software is required at run-time.

Speed of execution is slower.

Code is not optimised.

Source code is required.

Compiling the Future

A journey into the world of compilers


How does source code written by a programmer become binary code that a computer can execute?

Show answer

Auto Play

Slide 1 / 27

SLIDE