Title: N'K' Srinathsrinath_nkyahoo'com 1 RVCE
1 - Storage Allocation
- Block Structured Languages
- Compiler Design Options - Division Into
Passes - Interpreters
- P-Code Compilers
- Compiler-Compilers.
N.K. Srinath srinath_nk_at_yahoo.com 1
RVCE
2 Block Structured Languages There are Programming
languages in which blocks are being used. It is
called block structured language. Each block has
the ability to declare its own identifiers. Exampl
e Pascal, C, Java etc.
N.K. Srinath srinath_nk_at_yahoo.com 2
RVCE
3 1. PROCEDURE A 2. VAR X, Y, Z
INTEGER 3. 4. PROCEDURE B
5. VAR W, X, Y REAL 6.
7. PROCEDURE C
2 8.
VAR W, V INTEGER 9.
3 1 10. END
C 11. 12. END
B 13. 14.
PROCEDURE D 15. VAR X, Z
CHAR 16.
4 17. END
D 18. END A
The block is numbered in sequence as and when a
new block is recognized.
N.K. Srinath srinath_nk_at_yahoo.com 3
RVCE
4 Compiler can construct a table for the block
structure.
Name The name of the procedure. Number The
procedure number. Level Gives the nested
depth. Surrounding Tells the number of procedure
surrounding that block.
Symbol table will have the identifiers with the
block number. The block level entry gives the
nesting depth for each block.
N.K. Srinath srinath_nk_at_yahoo.com 4
RVCE
5 The outer block has a level number 1, and each
other block has a level number that is one
greater than that of the surrounding block.
Symbol table Search When an identifier appears
in the source program, the compiler must first
check the symbol table for a definition of that
identifier by the current block. If no such
definition is found, the compiler looks for a
N.K. Srinath srinath_nk_at_yahoo.com 5
RVCE
6 definition by the block that surrounds
the current one, then by the
block that surrounds that and
so on. If not found even in the outermost block
then the an error message is generated.
Variables Access The variables provided in the
surrounding blocks is accessed by using a data
structure called display. Display contains
pointers to the activated records.
N.K. Srinath srinath_nk_at_yahoo.com 6
RVCE
7 It also points to the records for all blocks
that surround the current one in
the source program. Fig (a) shows
the display block for the nested block
shown in previous slide. Here as procedure A is
entered, Display is created and a pointer points
to the Activation record for A in stack.
Fig (a)
N.K. Srinath srinath_nk_at_yahoo.com 7
RVCE
8 When the C procedure calls itself recursively,
another activation record for C is created on the
stack. All the variables declared by C should use
the most
Fig (b)
recent activation record. Variables that
corresponds to the previous invocation of C are
not accessible.
N.K. Srinath srinath_nk_at_yahoo.com 8
RVCE
9 When the procedure C calls D (This is allowed
because the identifier D is defined in procedure
A which also contains C). The activation record
for D has been created on the stack.
Why is that the display now contains only two
pointer?
Fig (c)
N.K. Srinath srinath_nk_at_yahoo.com 9
RVCE
10 The procedure D cannot refer to variables in B or
?C , except through parameters that are passed to
it.
According to the block-structured language,
procedure D can refer only to variables that are
declared by D or some clock that contains D i.e.
procedure A. If procedure D calls procedure B
what will happen?
N.K. Srinath srinath_nk_at_yahoo.com 10
RVCE
11 The procedure B can refer to variables declared
by either B or A, which is reflected in the
contents of the display.
Inference The compiler must include code at the
beginning of a block to initialize the display
for that block. At the end of the block, it must
restore the previous display contents.
N.K. Srinath srinath_nk_at_yahoo.com 11
RVCE
12 Compiler Design Options
- Division into passes
- Single pass compiler
- No intermediate code is being generated.
- The procedure of compiling discussed is
- Lexical Scanner called Parser when needed, a code
generation routine was invoked by the parser.
This Object code produced was not highly
efficient.
N.K. Srinath srinath_nk_at_yahoo.com 12
RVCE
13 - Code optimization techniques
discussed could not be applied
in such a one-pass compiler. - Not all languages can be translated by one
pass compiler. - One pass compilers must have the ability to fix
up forward references in jump instructions, using
techniques discussed for one-pass assembler. - For fast compilation, one pass compiler is
preferred.
N.K. Srinath srinath_nk_at_yahoo.com 13
RVCE
14 Multi-pass compiler Sophisticated code
optimization is obtained by multi-pass
compilers. It is used when the system recourses
(memory) is limited. The requirement of each pass
can be smaller if the work of compilation is
divided into several passes. If the compiler is
divided into many passes, each pass becomes
simpler and therefore, easier to understand,
write and test.
N.K. Srinath srinath_nk_at_yahoo.com 14
RVCE
15 Interpreter - Compilers
- Interpreter
- The process of translating a source program into
some internal form is simpler and faster - Execution of the translated program is much
slower.
- Compiler
- The process of translating a source program
into some internal form is slower than
interpreter. - Executing machine code is much faster.
N.K. Srinath srinath_nk_at_yahoo.com 15
RVCE
16 3)Debugging facilities can be easily
provided. 4) During execution the interpreter
produce symbolic dumps of data values, trace
of program execution related to the source
statement. 5) Program testing can be done
effectively using
3) Provision of bugging facilities are
difficult and complicated. 4) The compiler does
not produce symbolic dumps of date value.
Debugging tools are required for tracing the
program.
N.K. Srinath srinath_nk_at_yahoo.com 16
RVCE
17 interpreter as the operation on different data
can be traced. 6) Easy to handle dynamic
scoping
5) It is difficult to test as the compiler
execution file gives
the final result. 6) Difficult to handle
dynamic scooping
N.K. Srinath srinath_nk_at_yahoo.com 17
RVCE
18 P-Code Compilers
P-Code compilers also called byte code compilers
are very similar in concept to interpreters.
Source program is analyzed and converted into an
intermediate code for a hypothetical computers,
often called pseudo-machine or P-machine. The
process of using such a P-code is shown in fig.
N.K. Srinath srinath_nk_at_yahoo.com 18
RVCE
19 N.K. Srinath srinath_nk_at_yahoo.com 19
RVCE
20 Advantages
- Software portability Not necessary to
generate different code for different computers
because P-code object programs can be
executed on any M/c that has a P-code
interpreter. - It is often much smaller than a corresponding
machine code program would be.
N.K. Srinath srinath_nk_at_yahoo.com 20
RVCE
21 Compiler-Compiler Compiler-Compiler is a
software tool that can be used to help in the
task of compiler construction.
N.K. Srinath srinath_nk_at_yahoo.com 21
RVCE
22 The inputs to the compiler-compiler
are 1. Description of the language. 2.
Grammar for the source language 3.
Semantic or code generation routines. Description
of the language consists of a set of lexical
rules for defining tokens. The grammar and
lexical rules are used to generate a scanner and
a parser directly. Compiler compiler has one
routine for each rule of
N.K. Srinath srinath_nk_at_yahoo.com 22
RVCE
23 the grammar. The routine is called by the parser
each time it recognizes the
language construct described by the associated
rule. Compiler-compiler frequently provide
special languages, notations, data structures,
and other similar facilities that can be used in
the writing of semantic routines. Advantages and
disadvantages Easy to construct and test
compiler. It requires more memory and compile
program slowly
N.K. Srinath srinath_nk_at_yahoo.com 23
RVCE
24 - Questions
- What are the different phases of a compiler and
briefly explain each phase.
N.K. Srinath srinath_nk_at_yahoo.com 24
RVCE
25 2.Suppose the rules of the grammar for ltexpgt and
lttermgt is as follows ltexpgtlttermgtltexpgtlttermgt
ltexpgtDivlttermgt lttermgtltfactorgtlttermgtltfactorgtlt
termgt-ltfactorgt Draw the parse trees for the
following (i) A1B1 (ii) A1-B1G1
(iii)A1DIV(B1G1)-D1
(i) lt term gt lt term gt lt
factor gt lt factor gt
id id ALPHA
BETA
N.K. Srinath srinath_nk_at_yahoo.com 25
RVCE
26 - 3. What are the issues that must be
considered during Lexical phase? - The lexical analyzer has to recognize the
longest possible string. - Ex identifier newva -- n ne new newv
newva -
- There is no end delimiter for the tokens
defined. - Normally we dont return a comment as a token
and the comments are only processed by the
lexical analyzer. -
N.K. Srinath srinath_nk_at_yahoo.com 26
RVCE
27 - Scanner is to operator when it is called by the
parser. - Scanner is responsible for reading the lines of
the source program and possibly for printing the
source listing. - The scanner, except for printing as the output
listing, must ignores comments. - Scanner must look into the language
characteristics. - Example FOTRAN
- Columns 1 - 5 Statement number
- Column 6 Continuation of line
- Column 7-72 Program statement
N.K. Srinath srinath_nk_at_yahoo.com 27
RVCE
28 - Scanners should look into the rules
- for the formation of tokens.
- Example 'READ' Should not be considered as
keyword as it is within quotes. i.e., all string
within quotes should not be considered as token. - Blanks are significant within the quoted
string. - Blanks has important factor to play in
different language
N.K. Srinath srinath_nk_at_yahoo.com 28
RVCE
294. Write an algorithm is to recognize a
token.
Get first Input-character if
Input-character in 'A' .. ' Z' then
Begin while Input-character in 'A' ..
'Z', ' 0'.. ' 9' do Begin get next
input character if Input_character _
then Begin get next
Input_character Last_Char_is_Underscore
true End if _
Else
N.K. Srinath srinath_nk_at_yahoo.com
29 RVCE
30 Last_Char_Is_Underscorefalse
end while if
Last_Char_Is_Underscore then
return(token-error) else
return (Valid_token) end if first is
'A' .. ' Z' else return
(token-error)
N.K. Srinath srinath_nk_at_yahoo.com 30
RVCE
317. List the features of Operator-Precedence
Parsing.
- It is very simple
- Used in languages where virtually all
operators are used. Example SNOBOL. - Three disjoint relations lt. and .gt are used
between certain pairs of terminals. - If alt. b we say that a yields precedence to
b. - if ab we say that a has same precedence as b.
- If a .gtb we say that a takes precedence over b.
N.K. Srinath srinath_nk_at_yahoo.com 31
RVCE
32 8. Show Bottom-up parsing reduction for
a given string using inverting
productions int int int Int T
int T int T T T E E
T int
ET
T int T
E
T
E
T
T
int int int
N.K. Srinath srinath_nk_at_yahoo.com 32
RVCE
33- 9. Explain Shift reduce parsing with an
example. - The operator precedence parsing is bottom up
parsing. - It was developed to shift reduce parsing.
- This method makes use of a stack to store tokens
that have not yet been recognized in terms of the
grammar. - The actions of the parser are controlled by
entries in a table, which is somewhat similar to
the precedence matrix.
N.K. Srinath srinath_nk_at_yahoo.com 33
RVCE
34- The two main actions of shift reducing parsing
are - Shift Push the current token into the stack.
- Reduce Recognize symbols on top of the stack
according to a rule of a grammar - Example begin READ ( id ) . . .
- Steps Token Stream
- 1. . . . begin READ ( id ) . . .
Stack
Begin is first pushes the token on stack
Shift
N.K. Srinath srinath_nk_at_yahoo.com 34
RVCE
352. . . . begin READ ( id ) . . . 3. . . .
begin READ ( id ) . . .
The next token READ is also shifted on to the
stack.
N.K. Srinath srinath_nk_at_yahoo.com 35
RVCE
363. . . . begin READ ( id ) . . .
4. . . . begin READ ( id ) . . .
5. . . . begin READ ( id ) . . .
When it parser examines the token ), the reduce
action is invoked.
N.K. Srinath srinath_nk_at_yahoo.com 36
RVCE
37A set of tokens from the top of the
stack is reduced to anon
terminal
symbol from the grammar. It is pushed onto the
stack, to be reduced later as part of the READ
statement. Note Shift action is taken by an
operator-precedence parser when it encounters the
relations ? and ? . Reduce action is taken
when an operator
precedence parser encounters the relation ?.
N.K. Srinath srinath_nk_at_yahoo.com 37
RVCE
38 10.Write a procedure for READ and the
associated procedure during Recursive
descent parsing. ltreadgt READ
(ltid-listgt) PROCEDURE READ begin FOUND
FALSE if TOKEN 8 READ then
begin advance to next token if TOKEN 20 (
then begin
N.K. Srinath srinath_nk_at_yahoo.com 38
RVCE
39 advance to next token if IDLIST returns
success then if token 21 )
then begin FOUND TRUE
advance to next token end
if ) end if ( end if READ
if FOUND TRUE then return success
else return failure end (READ)
N.K. Srinath srinath_nk_at_yahoo.com 39
RVCE
40 Procedure IDLIST begin FOUND FALSE
if TOKEN 22 id then
begin FOUND TRUE
advance to Next token while
(TOKEN 14 ,) and (FOUND TRUE) do
begin advance to next token if TOKEN 22
id then advance to next token
N.K. Srinath srinath_nk_at_yahoo.com 40
RVCE
41 else FOUND
FALSE end while end if id if FOUND
TRUE then return success else
return failure end IDLIST
N.K. Srinath srinath_nk_at_yahoo.com 41
RVCE
42 11. List the different phases of a compiler
as a collection of front-end
and a back-end. The front-end comprises of those
phases or at times also parts of the phases which
depend on the source language and are independent
of the target machine. These include lexical
analysis, syntactic analysis, creation of
symbol table, semantic analysis and
generation of intermediate code.
N.K. Srinath srinath_nk_at_yahoo.com 42
RVCE
43It also includes some amount of error handling
and code optimization that goes along with these
phases.
- The back-end generally includes those phases of
the compiler which depend on the target machine. - They do not depend on the source language, just
the intermediate language. - Backend includes
- code optimization,
- code generation along with error handling and
- symbol-table operations.
N.K. Srinath srinath_nk_at_yahoo.com 43
RVCE
44 12. What is the advantage of intermediate
code? Give an explanation supporting
your answer. 13. Translate
the expression (ab) (cd) (abc) into
quadruples. 14. List the different machine
dependent code optimization. 15. List the
different machine independent code optimization.
N.K. Srinath srinath_nk_at_yahoo.com 44
RVCE
45 16. How does the compiler manage
to allocate the data if the
information
of the array size is not known during compilation
time? 17. Discuss with example the static and
dynamic storage allocation in compilers. 18.
Explain with a block diagram the process of
managing the storage when the function is called
recursively. 19 Discuss the process of accessing
variables in block structure language.
N.K. Srinath srinath_nk_at_yahoo.com 45
RVCE
46 ANY MORE QUESTIONS ?
N.K. Srinath srinath_nk_at_yahoo.com 46
RVCE