You are on page 1of 5

Q#1: This assignment covers the basic understanding of compiler construction it’s

common terms and all of its phase’s basics


a. Explain the following:

I. Symbol Table:
Symbol Table is a data structure created and maintained by the compiler. It represent
data in tabular form. It stores information about the scope and binding information
about names, information about instances of various entities such as variable and
function names, classes, objects.

II. Reserved Word Table:


A reserved word (also known as a reserved identifier) is a word that cannot be used as
an identifier, such as the name of a variable, function, or label. Appropriated for special
use.
example: "print" is a reserved word because it is a function in many languages to show
text on the screen.

a. Symbol Table:
Lexical Syntax Semantic Intermediate Code Target Code
Analyzer: Analyzer: Analyzer: Code Optimization: generation:
generation:
Creates new Adds Uses Refers Uses Generates
table entries information available symbol table information code by using
in the table. regarding information for knowing present in the address
i.e. like attribute in the table how much symbol table information
entries about type, scope, to check for and what for machine- of identifier
tokens. dimension, semantics i.e. type of run- dependent present in
line of to verify that time is optimization. the table.
reference, expressions allocated and
use, etc in and table helps in
the table. assignments adding
are temporary
semantically variable
correct(type information.
checking)
and update it
accordingly.

b. Interpret the given statement from all the phases of compiler and mention
output of each phase after passes through it phases.
Symbol Table:
E -> T = E/T
T -> T + F/F
F -> F – G/G
G -> G/D /D
D -> D/L
L -> a/b/5/c/d/id

Result = (a - b) + (c / d) + 5

Lexical Analysis:
Id = (id – id) + (id / id) + 5

Syntax Analysis:

E -> T = T
=> T + F
=> T + F + F
=> F + G + G
=> F - G + G/D + D
=> G - G + D/D + L
=> D - D + L/L + 5
=> L - L + c/d + 5
=> id - id + id/id + 5

Result = (a - b) + (c / d) + 5

E
T = T

T + F

T + F G

F G D

F - G G D L
/

G
D D L id

D L L id

L id id

id

Semantic Analysis:

Id1 +

- / inttoreal

Id2 id3 id4 id5 5


Intermediate Code generation:
x = id4 / id5
y=x+5
z = (id2 – id3) + y

Code Optimization:
x = (id4 / id5) + 5
z = x + (id2 – id3)

Target Code generation:


MOV R0 , id2
MOV R1 , id3
SUB R1 , R0
MOV R3 , id4
MOV R4 , id5
DIV R4 , R3
MOV R5 , 5
ADD R4 , R5
ADD R1 , R4

a. Interpret the given statement from all the phases of compiler and mention output of
each phase after passes through it phases.

Token Lexeme Attributes


Identifier A Entry point ‘A’ in the table
Operator = Assignment operator/equal
Number 100 Number/integer value
Punctuation ; semicolon
Token Lexeme Attributes
keyword Int Reserved word
keyword main Reserved word
punctuation ( parentheses
punctuation ) parentheses
punctuation { parentheses
keyword int Reserved word
identifier A Entry point ‘A’ in the table
punctuation , comma
identifier b Entry point ‘b’ in the table
punctuation ; semicolon
identifier a Entry point ‘a’ in the table
operator = Assignment operator/equal
number 10 Number/integer value
punctuation ; semicolon
keyword return Reserved word
number 0 Number/integer value
punctuation ; semicolon
punctuation } parentheses

You might also like