Language processing
Language processing
fundamentals
Language processing
fundamentals
• Language processing =
– Analysis of the source program +
Synthesis of the target program
Intermediate
Representation
(IR)
• Second pass now would read the intermediate
representation generated by previous pass, instead of
reading source program
+ a, real +
a, real
(a) (b)
:=
(c)
Example: i: integer;
a,b: real;
a := b+i;
Symbol Table:
No. Symbol Type Length Address
1 i int
2 a real
3 b real
4 i* real
5 temp real
Intermediate code
1. Convert (id, #1) to real, giving (id, #4)
2. Add (id, #4) to (id, #3) giving (id, #5)
3. Store (id, #5) in (Id, #2)
Lexical Analysis (Scanning)
• Identifies lexical units in a source statement.
• Classifies units into different classes e.g id’s,
constants, reserved id’s etc and enters them into
different tables
• Token contains
– Class code and number in class
Id #2 Op #5 Id #3 Op #3 Id #1 Op #10
Syntax Analysis (Parsing)
• Processes tokens for a statement to determine its
grammatical structure and build intermediate code that
represents structure.
• A tree is chosen for the intermediate code because it can
represent the hierarchical structure.
• Determines the statement class such as assignment
statement, if stmt etc.
e.g.:- a , b : real; and a = b +i ;
real :=
a b a +
b i
Semantic Analysis
• Determines the meaning of the SP
Lexical
errors Scanning
tokens
Symbol table
Syntax Constant table
errors Parsing
Other table
Parse tree
IC
IR
Figure: Front end of a Toy Compiler
Back End
1. Memory Allocation
Calculated from its type, length.
No. Symbol Type Length Address
1 i int 2000
2 a real 2001
3 b real 2002
• Back end computes the memory requirement of
a variable from its type, length found in the
symbol table, and allocates memory to it.
IC
Memory allocation
Symbol table
Code generation
Target
program