Ma No j2
-
Upload
mayur-gupta -
Category
Documents
-
view
216 -
download
0
Transcript of Ma No j2
-
8/6/2019 Ma No j2
1/19
A
PRESENTATION ON
Chapter 1 2301373: Introduction 1
BASICS OF COMPILER DESIGN
SUBMITTED BY:-HARSHIT KHANNA0115CS081040
-
8/6/2019 Ma No j2
2/19
What is a Compiler?
Acompiler is a computer program
that translates a program in a
source language into an equivalent
program in a target language.
Asource program/code is a
program/code written in thesource language, which is usually a
high-level language.
Atarget program/code is a
program/code written in the target
language, which often is a machinelanguage or an intermediate code.
Chapter 1 2301373: Introduction 2
compilerSourceprogram Targetprogram
Error
message
-
8/6/2019 Ma No j2
3/19
Process ofCompiling
Chapter 1 2301373: Introduction 3
scanner
parser
Semantic analyzer
Intermediate code generator
Code optimization
Code generator
Code optimization
Stream of characters
Stream of tokens
Parse/syntax tree
Annotated tree
Intermediate code
Intermediate code
Target code
Target code
-
8/6/2019 Ma No j2
4/19
Some Data Structures
Symbol table
Literal table
Parse tree
Chapter 1 2301373: Introduction 4
-
8/6/2019 Ma No j2
5/19
Symbol Table
Identifiers are names ofvariables, constants,functions, data types, etc.
Store information associated with identifiers
Information associated with different types ofidentifiers can be different
Information associated with variables are name, type,
address,size (for array), etc.
Information associated with functions are name,type ofreturn value, parameters, address, etc.
Chapter 1 2301373: Introduction 5
-
8/6/2019 Ma No j2
6/19
Symbol Table (contd)
Accessed in every phase ofcompilers
The scanner, parser, and semantic analyzer put
names ofidentifiers in symbol table.
The semantic analyzer stores more information
(e.g. data types) in the table.
The intermediate code generator, code optimizer
and code generator use information in symboltable togenerate appropriate code.
Mostly use hash table for efficiency.
Chapter 1 2301373: Introduction 6
-
8/6/2019 Ma No j2
7/19
Literal table
Store constants and strings used in program
reduce the memory size by reusing constants and
strings
Can be combined with symbol table
Chapter 1 2301373: Introduction 7
-
8/6/2019 Ma No j2
8/19
Parse tree
Dynamically-allocated, pointer-based
structure
Inf
ormation
for di
ffere
nt data types related
to parse treesneed to be stored somewhere.
Nodes are variant records, storing information
for differenttypes ofdata
Nodesstore pointersto informationstored in
other data structure, e.g. symbol table
Chapter 1 2301373: Introduction 8
-
8/6/2019 Ma No j2
9/19
Scanning
A scanner reads a streamofcharacters and
puts them together into some meaningful
(with respect to the source language) units
calledtokens.
It produces a streamoftokens for the next
phase ofcompiler.
Chapter 1 2301373: Introduction 9
-
8/6/2019 Ma No j2
10/19
Parsing
Aparser gets a streamoftokens from thescanner, anddetermines ifthe syntax(structure) ofthe program is correct according
to the (context-free) grammar ofthe sourcelanguage.
Then, it produces a data structure, called aparse tree or an abstract syntax tree, whichdescribes the syntactic structure oftheprogram.
Chapter 1 2301373: Introduction 10
-
8/6/2019 Ma No j2
11/19
Semantic analysis
It gets the parse tree from the parser together withinformation about some syntactic elements
It determines ifthe semantics or meaningofthe programis correct.
This part deals with static semantic. semantic ofprograms that can be checkedby readingofffrom
the programonly.
syntax of the language which cannot be described in context-free grammar.
Mostly, a semantic analyzer does type checking. It modifies the parse tree in order toget that (static)
semantically correct code.
Chapter 1 2301373: Introduction 11
-
8/6/2019 Ma No j2
12/19
Intermediate code generation
An intermediate code generator
takes a parse tree from the semantic analyzer
generates a program in the intermediate language.
In some compilers, a source program is translated
into an intermediate code first and then the
intermediate code is translated into the target
language. In other compilers, a source program is translated
directly into the target language.
Chapter 1 2301373: Introduction 12
-
8/6/2019 Ma No j2
13/19
Intermediate code generation (contd)
Using intermediate code is beneficial whencompilers which translates a single sourcelanguage tomany target languages are required.
The front-endofa compiler scanner to intermediatecode generator can be usedfor every compilers.
Different back-ends code optimizer and codegenerator is requiredfor each target language.
One ofthe popular intermediate code is three-address code. A three-address code instruction isin the formof x= y op z.
Chapter 1 2301373: Introduction 13
-
8/6/2019 Ma No j2
14/19
Code optimization
Replacing an inefficient sequence ofinstructions
with a better sequence ofinstructions.
Sometimes called code improvement.
Code optimization can be done:
after semantic analyzing performedon a parse tree
after intermediate code generation performedon a intermediate code
after code generation performedon a target code
Chapter 1 2301373: Introduction 14
-
8/6/2019 Ma No j2
15/19
Code generation
A code generator
takes either an intermediate code or a parse tree
produces a target program.
Chapter 1 2301373: Introduction 15
-
8/6/2019 Ma No j2
16/19
Error Handling
Error can be found in every phase ofcompilation.
Errors foundduring compilation are calledstatic (or
compile-time)errors.
Errors foundduring execution are calleddynamic (or
run-time)errors
Compilers need todetect, report, and recover
from error found in source programs Error handlers are different in different phases of
compiler.
Chapter 1 2301373: Introduction 16
-
8/6/2019 Ma No j2
17/19
HISTORYOFINTRODUCTIONOF
PARSINGTECHNIQUES
Recursive-descent parsing was introduced.
Nuar designedAlgol60, Pascals ancestor, whichallows recursive call.
Backus-Nuar form (BNF) was used todescribedAlgol60.
LL(1) parsing was proposedby Lewis andStearns.
General LRparsing was inventedby Knuth.
SLRparsing was developedby DeRemer.
Chapter 1 2301373: Introduction 17
-
8/6/2019 Ma No j2
18/19
History (1970s)
LALR was develpoedby DeRemer.
Aho andUllman founded the theory ofLR
parsing techniques. Yacc (Yet Another Compiler Compiler) was
developedby Johnson.
Type inference was studiedby Milner.
Chapter 1 2301373: Introduction 18
-
8/6/2019 Ma No j2
19/19
TH
ANKY
OU
Chapter 1 2301373: Introduction 19