Title: Introduction to Compilers
1- Introduction to Compilers
- Set 1
2What is a Compiler?
- A compiler is software (a program) that
translates a high-level programming language to
machine language - A simple representation would beSource Code
----gt Compiler -----gt Machine Language (Object
File) - It's not as simple as an assembler translator.
- A compiler has to perform several steps to
produce an object file in machine code form.
3Analysis of the source code
- Lexical Analysis using a program called a SCANNER
- Syntax Analysis using a program called a PARSER
- Semantic Analysis using CODE GENERATION routines
4Lexical Analysis
- Scan the input source code to identify tokens of
the programming language. - Tokens are basic units (keywords, identifier
names, etc.) from which the source code is
constructed - The program to do this is called a SCANNER
- Â
5Syntax Analysis
- Group the tokens identified by the scanner into
grammatical phrases that will be used by the
compiler to generate the output code. - This process is called parsing and is performed
by a parser.
6Synthesis of the target program
- Generate an intermediate representation of the
source program - Code Optimization
- Code Generation
7Generate an intermediate representation
- This is performed by some compilers, but not
necessarily all. - Â
- It should be easy to produce and easy to
translate into the target program.
8Code Optimization
- Improve the intermediate code to produce a faster
running machine code in the final translation. Â -
- Not all compilers include the code optimization
step, which can require a lot of time.
9Linking the programs produced by the compiler
- The linker program links the object files from
the program modules, and any additional library
files to create the executable program. -
- This requires the use of relocatable addresses
within the program to allow the program to run in
different memory locations.
10Grammar 1
1. SENTENCE -gt NOUNPHRASE VERB NOUNPHRASE 2.Â
NOUNPHRASE -gt the ADJECTIVE NOUN 3. NOUNPHRASE
-gt the NOUN 4. VERB -gt pushed 5. VERB -gt
helped 6. ADJECTIVE -gt pretty 7. ADJECTIVE -gt
poor 8. NOUN -gt man 9. NOUN -gt boy 10. NOUN -gt
cat
11- Grammar 1 is an example of a context-free grammar
(the only kind we will deal with). - The grammar consist of 10 productions e.g.
production 3 is - nounphrase -gt the noun
- Here nounphrase is referred to as the lefthand
side (lhs) and the noun is referred to as the
righthand side (rhs)
12- The set of all lhss constitutes the set of
nonterminals of the grammar. - In this case they are SENTENCE,
NOUNPHRASE,VERB, ADJECTIVE, NOUN - All the other symbols occurring in the grammar
(i.e. in some rhs, but never as any lhs) are the
terminals of the grammar. - In this case the,pushed,helped,pretty,poor,
13- The lhs of the first production is called the
goal symbol, in this case sentence. - A derivation of a string in the grammar is a list
of strings starting with the goal symbol, in
which each string, except the first, is obtained
from the preceding one by applying a substitution
of one of its symbols using one of the
productions as a substitution rule
14- A string which has a derivation is said to be
derivable. - Derivable strings that consist entirely of
terminal symbols are called sentences of the
grammar. E.g. - the man helped the poor boy
- is a sentence of Grammar 1.
- The set of all sentences of a grammar is called
the language defined by the grammar
15Grammar 1 (Cont.1)
Derivation of the sentence "the man helped the
poor boy 1.     SENTENCE                     Â
(goal symbol) 2. gt NOUNPHRASE VERB
NOUNPHRASE    (by Rule 1) 3. gt the NOUN VERB
NOUNPHRASE      (Rule 3) 4. gt the man VERB
NOUNPHRASE       (Rule 8) 5. gt the man helped
NOUNPHRASE 6. gt the man helped the ADJECTIVE
NOUN 7. gt the man helped the poor NOUN 8. gtÂ
the man helped the poor boy (this derivation
shows that "the man helped the poor boy is a
sentence in the language defined by the grammar.)
16Grammar 1 (Cont.2)
This derivation may also be represented
diagrammatically by a syntax tree       Â
17Typical format of a grammar for a programming
language
PROGRAM -gt PROGRAM STATEMENT PROGRAM -gt
STATEMENT STATEMENT -gt ASSIGNMENT-STATEMENT STATEM
ENT -gt IF-STATEMENT STATEMENT -gt
DO-STATEMENT ... ASSIGNMENT-STATEMENT -gt
... ... IF-STATEMENT -gt ... ... DO-STATEMENT -gt
... ...
18Grammar 2
A simple grammar for arithmetic statements
1.   E -gt E T 2.   E -gt T 3.   T -gt T
a 4.   T -gt a
19Grammar 2 (Cont.1)
Derivation of a a a 1.       E
Goal Symbol 2. gt   E T Rule 1 3.
gt   E T a Rule 3 4. gt   E a a
Rule 4 5. gt   T a a Rule 2 6.
gt   a a a Rule 4
20Grammar 2 (Cont.2)
Derivation of a a a written in reverse
1.   a a a Given sentential
form 2.   T a a Rule 4 in
reverse 3.   E a a Rule 2 in
reverse 4.   E T a Rule 4 5.   E T
Rule 3 in reverse 6.   E
Rule 1