0% found this document useful (0 votes)
6 views26 pages

Introduction to Compilers and Programming

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views26 pages

Introduction to Compilers and Programming

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lecture 1: Introduction

What is this course about?

Source Code Compiler Target Code

In computing, a compiler is a computer program that


translates computer code written in one programming
language (the source language) into another language (the
target language) <[Link]>
What is this course about?

Source Code Compiler Target Code

The name "compiler" is primarily used for programs that


translate source code from a high-level programming
language to a lower level language (e.g. assembly language,
object code, or machine code) to create an executable
program.<[Link]>
What is this course about?
• How are programs written in a high-level language translated into
machine code?
• How can we ensure a program is somewhat meaningful?
• How is the program’s memory managed?
• How programming languages are designed?
• Why there are so many different kinds of programming languages?
Why study compilers?
Textbooks and Resources
1. Compliers Principles technique and Tools (Second Edition)
Alfred [Link], Ravi Sethi, Jeffrey [Link]
2. Trình biên dịch
Phan Thị Tươi
3. Introduction to Programming Languages/Grammars
4. Write your own compiler
5. The lox language
Compiler vs Interpreter
temp = v[k] temp = v(k)
High-level Language v[k] = v[k+1] V(k) = v(k+1)
v[k+1] = temp; V(k+1) = temp;

C/C++ Compiler Fortran Compiler


lw $t0, 0($2)
lw $t1, 4($2)
Assembly Language sw $t1, 0($2)
sw $t0, 4($2)
MIPS Assembler
0000 1001 1100 0110 1010 1111 0101 1000
1010 1111 0101 1000 0000 1001 1100 0110
Machine Language 1100 0110 1010 1111 0101 1000 0000 1001
0101 1000 0000 1001 1100 0110 1010 1111
Structure of a compiler
Lexical Analysis

Parsing

Semantic Analysis

Optimization

Code Generation
Lexical Analysis
• First steps: recognize words
- Smallest unit above letters

This is a sentence

ist his asen tence


Lexical Analysis
• Lexical analysis devides program text into “words” or “tokens”

if num1 == num2 then result = 1; else result = 2;

Keywords: if, then, else


Variable names (identifier): num1, num2, result
Constants: 1, 2
Operators: double equals, assignment
Other tokens: semi colons, the punctuation, the separators (blank)
How do we know that is double equals, not two individual equals signs?
How do we know that we want this? A double equal, not two single euqals?
Parsing
• Once words are understood, the next step is to understand sentence
structure.
• Parsing = Diagramming Sentences
• The diagram is a tree
Parsing
This line is a longer sentence.

article noun verb article adjective noun

subject object

sentence
Parsing
if num1 == num2 then result = 1; else result = 2;

num1 == num2 result = 1 result = 2

relation assignment assignment

predicate then-statement else-statement

if-then-else
Semantic Analysis
• Once sentence structure is understood, we can try to understand
“meaning”.

• Compilers perform limited semantic analysis to catch inconsistencies.


Semantic Analysis
Tom said Jerry left his key at home.

Tom said Tom left his key at home?


Semantic Analysis
• Programming languages define {
strict rules to avoid such int Tom= 20;
ambiguities
{
int Tom= 50;
cout << Tom;
}
}
Semantic Analysis
• Compilers perform many semantic checks besides variable bindings

• Example: Tom left her homework at home.

• A ‘type mismatch’ between Tom and her, we know they are different
people.
Optimization
• Automatically modify programs so that they
• Run faster
• Use less memory
• Reduce the amount of power
• Reduce the number of network messages
• Reduce the number of database accesses.
Optimization
A = B * 0 is the same as A = 0
Code Generation
• Produces assembly code (usually)

• A translation into another language (analogous to human translation)

Semantic Code
Lexical Analysis Parsing Optimization
Analysis Generation

Lexical Code
Parsing Semantic Analysis Optimization
Analysis Gen
• Compiler translates from one language to another

Source code Front End Back End Target code

• Front End: Analysis


• Pulls apart program, understand structure and meaning
• Back End: Synthesis
• Puts it back together in a different way
• Why are there so many programming languages?

• Why are there new programming languages?

• What is a good programming language?


Why are there so many programming languages?
Application domains have distinctive
Scientific computing
- need good floating point support,
- need good for arrays and operations on arrays
- need parallelism
Business applications
- need persistence
- report generation
- data analysis
Systems programming
- control of resources
- real time constraints
Why are there new programming languages?
What is a good programming language?

There is no universally accepted metric for language design.

Common questions

Powered by AI

New programming languages are developed to address specific needs in different application domains such as scientific computing, business applications, or systems programming. Each domain has unique requirements like floating-point support, parallelism, persistence, or real-time resource control that may not be efficiently handled by existing languages .

The phrase "Tom left her homework at home" demonstrates a semantic error, as the pronoun 'her' mismatches the subject 'Tom,' leading to a potential type mismatch in programming terms. Semantic analysis in compilers checks for such errors to ensure logical consistency in variable use and operations .

A compiler translates high-level instructions, such as swapping array elements, by first analyzing and generating an abstract representation at the front end. In code generation, this is translated into a sequence of machine-level instructions, such as 'load,' 'store,' and 'exchange' commands specific to the machine's architecture .

The optimization phase can enhance a program by making it run faster, using less memory, reducing power consumption, and minimizing network messages or database accesses. Optimizations can involve removing redundant code, such as replacing 'A = B * 0' with 'A = 0' .

A compiler translates source code written in a high-level programming language into a lower-level language such as assembly or machine code, allowing the program to be executed. This process includes stages like lexical analysis, parsing, semantic analysis, optimization, and code generation .

Lexical analysis involves breaking down the program text into tokens, the smallest units of meaning, while parsing involves understanding the grammatical structure of these tokens. Lexical analysis identifies elements like keywords and operators, whereas parsing constructs a tree structure to represent these relationships .

Business applications require languages that efficiently handle data analysis, persistence, and report generation, whereas scientific computing needs strong support for floating-point arithmetic, arrays, and parallel operations. These differences stem from the distinct nature of tasks each domain prioritizes, influencing their respective language design and features .

The front-end of a compiler performs analysis, parsing the input code to understand its structure and meaning through lexical, syntax, and semantic analysis. The back-end synthesizes this information to translate it into target code, focusing on optimization and code generation .

A 'good' programming language is often considered subjective, as it depends on specific metrics like simplicity, consistency, efficiency, and applicability to certain tasks. No universally accepted metric exists because different application domains prioritize different features, such as performance for scientific computing or data analysis capabilities for business .

Semantic analysis is crucial for ensuring that the program's statements make logical sense according to the language rules, catching inconsistencies like type mismatches and incorrect variable bindings. This step helps prevent errors that are not detectable during lexical or syntactic analysis .

You might also like