Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Assignment 1

Compiler design
Name :Keshav Bhardwaj
Sap id:500094898
Batch: 3

Q1. What is a) Single pass compilers


b) Multi pass compilers

A) single pass compiler is a type of compiler that passes through the source code of a
program only once and generates the machine code directly. A multi pass compiler is a type
of compiler that processes the source code of a program multiple times, using intermediate
representations and passes, to optimize and generate the machine code.

There are some advantages and disadvantages of using a single pass compiler :
i. Some of the advantages are that it is faster and smaller than a multi pass compiler,
and it is suitable for simple languages and command interpreters.
ii. Some of the disadvantages are that it is less efficient in code optimization and
generation, it has limited scope and requires that the program constants, types,
variables, and procedures are defined before they are used, and it cannot handle
forward references and complex grammars

B) A multi pass compiler is a type of compiler that processes the source code or
abstract syntax tree of a program multiple times, using intermediate representations and
passes, to optimize and generate the machine code.

First Pass is referred as

• Front end
• Analytic part
• Platform independent
Second Pass is referred as

• Back end
• Synthesis Part
• Platform Dependent

Some disadvantages of multi-pass compilers are:

• They are slower than one-pass compilers, as they require more execution time.
• They are more difficult to design and implement than one-pass compilers.
• They may introduce errors or inconsistencies in the intermediate outputs.

Q2. Regular compiler and regular expression with their application to lexical
analyser.

Regular Grammar: A regular grammar is a formal grammar that generates regular


languages. It consists of a set of production rules, typically written in the form of:

A -> αB | β

Where: A is a non-terminal symbol.

α and β are strings of terminal symbols and non-terminal symbols.

|represents alternative choices.

Regular grammars are simple and restricted in expressive power compared to other types of
grammars, like context-free grammars. They are particularly suited for describing patterns that
can be recognized by finite automata.

Regular Expressions:

Regular expressions are a concise and powerful way to describe patterns in strings. They are a
textual representation of regular languages and are composed of a combination of characters and
special symbols that define a search pattern. Common regular expression operators include *
(zero or more repetitions), + (one or more repetitions), ? (zero or one repetition), | (alternation),
and others.

For example, a simple regular expression like a*b matches zero or more occurrences of the
character 'a' followed by 'b'.

Applications in Lexical Analysis: Lexical analysis is the process of breaking down the input
source code into a stream of tokens, which are the smallest meaningful units of code (e.g.,
keywords, identifiers, literals, etc.). Regular grammars and regular expressions are fundamental
tools for implementing lexical analysers or scanners. Here's how they are applied:

Token Recognition: Regular expressions are used to define patterns for tokens. For example,
you can use a regular expression to define the pattern for identifying keywords, identifiers,
integers, and other token types. Each regular expression corresponds to a token type.

Lexical Rules: Regular grammars can be used to specify the lexical rules of a programming
language. These rules define how tokens are formed from the input character stream. Regular
expressions often define these rules.

Efficiency: Regular expressions can be efficiently matched against the input text using automata-
based algorithms like the deterministic finite automaton (DFA) or non-deterministic finite
automaton (NFA). This efficiency is crucial for fast lexical analysis, especially for large source
code files.
Error Reporting: Lexical analysers can use regular expressions to recognize and report lexical
errors, such as unrecognized characters or invalid token patterns.

You might also like