Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

Assignment 2

The document discusses compiler construction concepts including phases of a compiler like lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. It describes the working of each phase and tools used in modern compiler construction like scanner generators, parser generators, syntax-directed translation engines, automatic code generators, and data-flow engines. It also discusses the purpose of a decompiler and provides context-free grammars for various regular expressions.

Uploaded by

Sarp M
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Assignment 2

The document discusses compiler construction concepts including phases of a compiler like lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. It describes the working of each phase and tools used in modern compiler construction like scanner generators, parser generators, syntax-directed translation engines, automatic code generators, and data-flow engines. It also discusses the purpose of a decompiler and provides context-free grammars for various regular expressions.

Uploaded by

Sarp M
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Department of

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.

The primary functions of this phase are:

 Identify the lexical units in a source code


 Classify lexical units into classes like constants, reserved words, and enter
them in different tables. It will Ignore comments in the source program
 Identify token which is not a part of the language

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.

List of tasks performed in this phase:


 Obtain tokens from the lexical analyzer
 Checks if the expression is syntactically correct or not
 Report all syntax errors
 Construct a hierarchical structure which is known as a parse tree

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.

Functions of Semantic analyses phase are:

 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

Intermediate Code Generation - In the process of translating a source program into


target code, a compiler may construct one or more intermediate representations,
which can have a variety of forms.
Syntax trees are a form of intermediate representation; they are commonly used
during syntax and semantic analysis.
After syntax and semantic analysis of the source program, many compilers
generate an explicit low-level or machine-like intermediate representation, which
we can think of as a program for an abstract machine.

Intermediate Representation should have two important properties:


 Easy to produce
 Easy to translate into the target machine.
might be grouped together into one pass.

Back-end pass consisting of Code Generation for a particular target machine.

Code Generation - The code generator takes as input an intermediate


representation of the source program and maps it into the target language. If the
target language is machine code, registers or memory locations are selected for
each of the variables used by the program.

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

Code optimization might be an optional pass.

Some compilers have a machine-independent optimization phase between the


front end and the back end.

Code Optimization - The machine-independent code-optimization phase attempts


to improve the intermediate code so that better target code will result. Usually
better means faster, but other objectives may be desired, such as shorter code, or
target code that consumes less power.

For example, a straightforward algorithm generates the intermediate code, using an


instruction for each operator in the tree representation that comes from the
semantic analyzer.
The primary functions of this phase are:

 It helps you to establish a trade-off between execution and compilation


speed.
 Improves the running time of the target program
 Generates streamlined code still in intermediate representation
 Removing unreachable code and getting rid of unused variables
 Removing statements which are not altered from the loop

c. Modern architectures of 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.

pg. 4
Conventional architectures of Compiler:

In the traditional compiler architecture, type checking is a separate phase


that takes place between parsing and code generation:

ii. What are Modern compiler construction tools?

pg. 5
Modern compiler construction tools are:

 Scanner generators - Scanner generator generates lexical analyzers from a


regular expression description of the tokens of a language.
o Input - This tool takes regular expressions as input.
o Output - Lexical Analyzer

Example - Flex for C or C++, Jlex for Java.

 Parser generators - Parser generator takes the grammatical description of a


programming language and produces a syntax analyzer.
o Input - Grammatical description of a programming language
o Output - Syntax analyzers which automatically generates source code
which can parse streams of characters with the help of a grammar.
 Syntax-directed translation engines - These software tools offer an
intermediate code by using the parse tree. It has a goal of associating one or
more translations with each node of the parse tree.
o Input - Parse tree.
o Output - Intermediate code.
 Automatic code generators - Code-generator takes a collection of rules that
define the translation of each operation of the intermediate language into the
machine language for a target machine.
o Input - Intermediate code
o Output - Machine code
 Data-flow engines - This tool is helpful for code optimization. Here,
information is supplied by the user, and intermediate code is compared to
analyze any relation. It is also known as data-flow analysis. It helps you to
find out how values are transmitted from one part of the program to another
part.
 Compiler-construction toolkits - provides an integrated set of routines for
constructing various phases of a compiler.

iii. What is the purpose of Decompiler?


 Compilers are used to generate executables from high-level source code
(such as C# or Java) that can be decompiled into a high-level language
using Decompiler.

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.

iv. Convert the following RE to equivalent CFGs


a. (a)(a|b)*
The grammar could be:
S  aA
A  aA | bA | ε
b. (a|b)(a|b)* (a|b)
The grammar could be:
S  CAB
A  aA | bA | ε
Ba|b
Ca|b
c. ((a|b)(a|b))*
The grammar could be:
SA
A  CBA | ε
Ba|b
Ca|b
d. ((a|b)(a|b))* (a|b)

pg. 7
The grammar could be:
S  AB
AC
C  DEC | ε
Ba|b
Da|b
Ea|b
e. a*.b*

The grammar could be:


S  AB
A  aA
A ε
B  bB
Bε

pg. 8

You might also like