Assignment 2
Assignment 2
Computing
Compiler Construction
Class: BSCS
Assignment [02]
Objective:
The objective of this assignment is to provide hands-on experience of System Programming
concepts including:
Regular Languages
Context free languages
pg. 1
Parsing
Questions:
i. Describe the working of modern compilers, including
The discussion of phases deals with the logical organization of a compiler. In an
implementation, activities from several phases may be grouped together into a pass
that reads an input file and writes an output file.
a. Required Phases
Front-end phases of
Lexical analysis - Lexical Analysis or Scanning is the first phase when compiler
scans the source code. This process can be left to right, character by character, and
group these characters into tokens.
The character stream from the source program is grouped in meaningful sequences
called lexemes by identifying the tokens. It makes the entry of the corresponding
tickets into the symbol table and passes that token to next phase, syntax analysis.
Syntax analysis - The second phase of the compiler is syntax analysis or parsing.
The parser uses the first components of the tokens produced by the lexical analyzer
to create a tree-like intermediate representation that depicts the grammatical
structure of the token stream. A typical representation is a syntax tree in which
each interior node represents an operation and the children of the node represent
the arguments of the operation.
pg. 2
Semantic analysis - The semantic analyzer uses the syntax tree and the information
in the symbol table to check the source program for semantic consistency with the
language definition. It also gathers type information and saves it in either the
syntax tree or the symbol table, for subsequent use during intermediate-code
generation. An important part of semantic analysis is type checking, where the
compiler checks that each operator has matching operands.
Helps you to store type information gathered and save it in symbol table or
syntax tree
Allows you to perform type checking
In the case of type mismatch, where there is no exact type of correction rules
which satisfy the desired operation a semantic error is shown
Collects type information and checks for type compatibility
Checks if the source language permits the operands or not
pg. 3
Then, the intermediate instructions are translated into sequences of machine
instructions that perform the same task. A crucial aspect of code generation is the
judicious assignment of registers to hold variables.
b. Optional phases
pg. 4
Conventional architectures of Compiler:
pg. 5
Modern compiler construction tools are:
pg. 6
Decompiler is a computer program that performs the reverse operation to
that of a compiler. That is, it translates program code at a relatively low level
of abstraction into a form having a higher level of abstraction.
Decompiler is an important tool in software reverse engineering. Decompiler
usually do not perfectly reconstruct the original source code and can vary
widely in the intelligibility of their outputs.
pg. 7
The grammar could be:
S AB
AC
C DEC | ε
Ba|b
Da|b
Ea|b
e. a*.b*
pg. 8