0% found this document useful (0 votes)
93 views

Theory of Programming Languages: An Overview Lecture # 1

The document discusses the theory of programming languages course, including details about the instructor, textbook, grading breakdown, projects, and goals of the course. It covers key topics like compilers, translation from source code to machine code using intermediate representations, the front-end parsing process involving scanners and parsers, context-free grammars, syntax trees, and abstract syntax trees.

Uploaded by

Hamza Basharat
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Theory of Programming Languages: An Overview Lecture # 1

The document discusses the theory of programming languages course, including details about the instructor, textbook, grading breakdown, projects, and goals of the course. It covers key topics like compilers, translation from source code to machine code using intermediate representations, the front-end parsing process involving scanners and parsers, context-free grammars, syntax trees, and abstract syntax trees.

Uploaded by

Hamza Basharat
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Theory of

Programming
Languages
AN OVERVIEW
LECTURE # 1
The Course
Course Code: CS-0000
Course Title: Theory of Programming Languages
Instructor: Amna Anjum
◦ Email Address: [email protected]

Term (Semester): Spring 2022


Duration: 16 Weeks
Text and Reference Material
1.
Concepts of Programming Languages
Grading
Following is the division of marks:

Mid-Term Exam 20
Assignments and Quizzes 20
Presentation & Project 20
Final Exams. 40

◦ Marks division might change during the semester


Project
Implementation
language: subset of java
Generated code: Intel x86 assembly
Implementation language: C++ / Java
Eight programming assignments

5
Why Take this Course
Understand compilers and languages
Understand the code structure
Understand language semantics
Understand relation between source code and
generated machine code
Become a better programmer
Theory
◦ mathematical models: regular expressions, automata, grammars, graphs
◦ algorithms that use these models

6
What are Compilers
Translate information from one representation to another
Usually information = program

7
Examples
Typical Compilers:
◦ VC, VC++, GCC, JavaC
◦ FORTRAN, Pascal, VB(?)

Translators
◦ Word to PDF
◦ PDF to Postscript

8
Typical Compilation
High-level source code

Compiler

Low-level machine code


9
Source Code
int expr( int n )
{
int d;
d = 4*n*n*(n+1)*(n+1);
return d;
}

10
Source Code
Optimized for human readability
Matches human notions of grammar
Uses named constructs such as variables and procedures

11
Assembly Code
.globl _expr imull %eax,%edx
_expr: movl 8(%ebp),%eax
pushl %ebp incl %eax
movl %esp,%ebp imull %eax,%edx

subl $24,%esp movl %edx,-4(%ebp)


movl -4(%ebp),%edx
movl 8(%ebp),%eax
movl %edx,%eax
movl %eax,%edx
jmp L2
leal 0(,%edx,4),%eax
.align 4
movl %eax,%edx
L2:
imull 8(%ebp),%edx leave
movl 8(%ebp),%eax ret
incl %eax 12
Assembly Code
Optimized for hardware
Consists of machine instructions
Uses registers and unnamed memory locations
Much harder to understand by humans

13
How to Translate
Correctness:
the generated machine code must execute precisely the same computation
as the source code

14
How to Translate
Is there a unique translation? No!
Is there an algorithm for an “ideal translation”? No!

15
How to Translate

Translation is a complex process


source language and generated code are
very different
Need to structure the translation

16
Two-pass Compiler

source Front IR Back machine


code End End code

errors

17
Two-pass Compiler
Use an intermediate representation (IR)
Front end maps legal source code into IR
Back end maps IR into target machine code
Admits multiple front ends & multiple passes
Front end is O(n) or O(n log n)
Back end is NP-Complete (NPC)

18
Front End

Recognizes legal (& illegal) programs


Report errors in a useful way
Produce IR & preliminary storage map

19
The Front-End

source tokens IR
scanner parser
code

Modules errors

Scanner
Parser
20
Scanner

source tokens IR
scanner parser
code

errors

21
Scanner

Maps character stream into words – basic


unit of syntax
Produces pairs –
◦ a word and
◦ its part of speech

22
Scanner
Example
x = x + y
becomes
<id,x>
<assign,=> <id,x>
<id,x>
<op,+>
<id,y> word
token type

23
Scanner

we call the pair


“<token type, word>” a “token”
typical tokens: number, identifier, +, -, new,
while, if

24
Parser

source tokens IR
scanner parser
code

errors

25
Parser
Recognizes context-free syntax and reports
errors
Guides context-sensitive (“semantic”)
analysis
Builds IR for source program

26
Context-Free Grammars
Context-free syntax is specified
with a grammar G=(S,N,T,P)
S is the start symbol
N is a set of non-terminal symbols
T is set of terminal symbols or words
P is a set of productions or rewrite rules
27
Context-Free Grammars

Grammar for expressions


1. goal→ expr
2. expr → expr op term
3. | term
4. term → number
5. | id
6. op → +
7. | -

28
The Front End
For this CFG
S = goal
T = { number, id, +, -}
N = { goal, expr, term, op}
P = { 1, 2, 3, 4, 5, 6, 7}

29
Context-Free Grammars

Given a CFG, we can derive sentences by


repeated substitution
Consider the sentence (expression)
x+2–y

30
Derivation

Production Result
goal
1 expr
2 expr op term
5 expr op y
7 expr – y
2 expr op term – y
4 expr op 2 – y
6 expr + 2 – y
3 term + 2 – y
5 x+2–y 31
The Front End
To recognize a valid sentence in some CFG,
we reverse this process and build up a
parse
A parse can be represented by a tree:
parse tree or syntax tree

32
Parse
Production Result
goal
1 expr
2 expr op term
5 expr op y
7 expr – y
2 expr op term – y
4 expr op 2 – y
6 expr + 2 – y
3 term + 2 – y
5 x+2–y
33
goal
Syntax Tree
expr
x+2-y
expr op term

expr op term – <id,y>

term + <number, 2>

<id,x> 34
Abstract Syntax Trees

The parse tree contains a lot of


unneeded information.
Compilers often use an abstract
syntax tree (AST).

35
Abstract Syntax Trees


+ <id,y>
<id,x> <number,2>

This is much more concise


36
Abstract Syntax Trees

+ <id,y>
<id,x> <number,2>

AST summarizes grammatical structure without the details of derivation

37
Abstract Syntax Trees

+ <id,y>
<id,x> <number,2>
ASTs are one kind of intermediate
representation (IR)
38

You might also like