Unit 1 Introduction of Compiler
Unit 1 Introduction of Compiler
PREPARED BY :
PROF. ANAND N. GHARU
ASSISTANT PROFESSOR
COMPUTER DEPARTMENT
3/17/2019 SUBJECT – COMPILER
PROF. ANAND(BE
GHARU COMPUTER SPPU-2019)
CONTENTS
• COMPILER
• INTERPRETER
• ANALYSIS SYNTHESIS MODEL
• LANGUAGE PROCESSINGSYSTEM
• COMPILER PROCESSINBRIEF
• “Compilation”
– Translation of a program written in a source
language into a semantically equivalent program
written in a target language
Input
Source Target
Compiler
Program Program
• “Interpretation”
– Performing the operations implied by the source
program
Source
Program
Interpreter Output
Input
Error messages
Breaks up the source If the analysis part The analysis part also
program into detects that the source collects information
constituent pieces and program is either about the source
imposes a syntactically ill formed program and stores it
grammatical structure or semantically in a data structure
on them. It then uses unsound, then it must called a symbol table,
this structure to create provide informative which is passed along
an intermediate messages, so the user with the intermediate
representation of the can take corrective representation to the
source program. action. synthesis part.
Perform optimization
10
What and how many passes a compiler does over the source
program is an important designdecision.
12
Syntactic Analyzer
calls calls
14
15
16
Parameter
Constant
NAME Record
RecordField
Procedure
Array and files
17
18
19
• Basic idea
– keep track of what memory is referenced and
when it is no longer accessible, reclaim the
memory
• Example
– linked list
p 1 0
Object p = new Object;
p.count++;
Object q = new Object; q
q.count++;
p.count--;
if(p.count == 0) 1 2
collect p
p = q;
p.count++;
Interpreters
31
32
Preprocessor
Assembler
Target Assembly Program Libraries and
Linker Relocatable
Relocatable Object Code Object Files
33
Preprocessor
Compiler
Assembler
Linker
34
get next
token
symbol
table
Generate Errors
• Send Tokens to Parser
38
41
Character-at-a-time I/O
Block / Buffered I/O
Block/Buffered I/O
Utilize Block of memory
Stage data from source to buffer block at a time
Maintain two blocks - Why (Recall OS)?
Asynchronous I/O - for 1 block
While Lexical Analysis on 2nd block
Block 1 Block 2
lex
source lex.yy.c
Lex compiler
program
lex.l
C
C
lex.yy.c a.out
compiler
compile
r
letter [A-Za-z]
digit [0-9]
id {letter}({letter}|{digit})*
......
%%
if { return(IF);}
Rules
then { return(THEN);}
{id} { yylval = install_id(); return(ID); }
......
%%
Auxiliary
install_id()
{ /* procedure to install the lexeme to the ST */
•To run lex on a source file, use the command: lex source.l
•This produces the file lex.yy.c which is the C source for the
lexical analyzer.
%%
[0123456789]+ printf("NUMBER\n");
[a-zA-Z][a-zA-Z0-9]* printf("WORD\n");
{WS} /* do nothing */
. printf(“UNKNOWN\n“);
%%
My Blog : anandgharu.wordpress.com