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

Q#1

Q#2
a) S→0S1∣01S→0S1∣01

This grammar generates strings with equal numbers of 0's and 1's where the 1's follow the 0's.

Every time we use the production S → 01to get the string like 0011,000111,00001111…..

So, This grammar is not ambiguous because each string generated by this grammar has exactly one
parse tree.
b) S→ +SS∣ −SS∣ a
This grammar allows expressions consisting of 'a's combined using '+' and '-'.

Deriving "a + a - a" or “a – a + a”


This grammar is ambiguous because there are strings (like "a + a - a") that can be derived in more
than one way, leading to different parse trees.

c) S->aSbS | bSaS | ∈
This grammar generates strings with a pattern of alternating sequences of 'a's and 'b's, repeated
twice, and nested within each other.

This grammar is ambiguous because If we want to generate the string “abab” from the above
grammar. We can observe that the given string can be derived using two parse trees
Q#3

a)

b)
c)

Q#4

a)
language-processing system offers several advantages:

Efficiency: Compilers can optimize code during the translation process, which leads to more
efficient execution of the program by reducing memory usage and increasing speed.
Error Detection: Language-processing systems can perform thorough checks for errors before the
execution phase. This helps in identifying and resolving issues such as syntax and semantic errors
early in the development process.

Platform Independence: High-level language compilers allow programs to be written in a platform-


independent manner. Once written, the source code can be compiled into machine code that is
tailored for specific hardware, allowing the same program to run on different systems.

Security: Compilers can enhance security by incorporating checks and balances that prevent
unsafe operations, such as buffer overflows and other vulnerabilities.

Portability: With a compiler, the source code of a program can be written in a high-level, human-
readable form, then compiled into the machine language of multiple platforms without changing
the high-level source code, enhancing the portability of applications.

A compiler that produces assembly language rather than machine language is often referred to as
an assembler or an assembly-level compiler. This type of compiler translates high-level language
code into assembly language, which is then converted into machine code by an assembler. This
approach allows for more control over hardware-specific optimizations and is typically used in
systems programming and performance-critical applications.

B)
Q#2
Define interpreter
A language translator that converts a high-level language program into a machine language
program, one line at a time, is referred to as an interpreter. Interpreters converts the codes slower
than compiler. This is because the interpreter can scan and translate only one statement of the
program at a time. Therefore, interpreters convert the source code into machine code during the
execution of the program.

Define Lexical analyzer


Lexical analysis is the starting phase of the compiler. It gathers modified source code that is written
in the form of sentences from the language preprocessor. The lexical analyzer is responsible for
breaking these syntaxes into a series of tokens, by removing whitespace in the source code. If the
lexical analyzer gets any invalid token, it generates an error. The stream of character is read by it and
it seeks the legal tokens, and then the data is passed to the syntax analyzer, when it is asked for.

What are the different phases in compilation process? Write names only
of the phases.
Q#3
Suppose the following programming statement, written in C++
programming language. You are to convert it into tokens and make
abstract syntax tree (AST) x=x+3*5
Q#4
Discuss Directed Acyclic graph (DAG) in details, what are the benefits
that are obtained while using DAG in compilation process. Give one
example to support your concept.
Directed Acyclic Graph :
The Directed Acyclic Graph (DAG) is used to represent the structure of basic blocks, to visualize the
flow of values between basic blocks, and to provide optimization techniques in the basic block. To
apply an optimization technique to a basic block, a DAG is a three-address code that is generated
as the result of an intermediate code generation.

Directed Acyclic Graph Characteristics:


A Directed Acyclic Graph for Basic Block is a directed acyclic graph with the following labels on
nodes.

• The graph’s leaves each have a unique identifier, which can be variable names or constants.

• The interior nodes of the graph are labelled with an operator symbol.

• In addition, nodes are given a string of identifiers to use as labels for storing the computed
value.

• Directed Acyclic Graphs have their own definitions for transitive closure and transitive
reduction.

• Directed Acyclic Graphs have topological orderings defined.

Example
Same Same But year Different
Ambiguous Grammar
An ambiguous grammar in programming and linguistics refers to a type of grammar that can
generate more than one distinct parse tree for the same sentence or string of symbols. This
ambiguity means that there are multiple ways to interpret the structure of the sentence without
altering the sequence of words or symbols
Top-down parser and Bottom-up parser
Top-Down Parsing
Top-Down Parsing technique is a parsing technique which starts from the top level of the parse
tree, move downwards, evaluates rules of grammar. In other words, top-down parsing is a parsing
technique that looks at the highest level of the tree at start and then moves down to the parse tree.

The top-down parsing technique tries to identify the leftmost derivation for an input. It evaluates the
rules of grammar while parsing. Consequently, each terminal symbol in the top-down parsing is
produced by multiple production of grammar rules.

Since top-down parsing uses leftmost derivation, hence in this parsing technique, the leftmost
decision selects what production rule is used to construct the string.
Bottom-up parser
Bottom-Up Parsing technique is again a parsing technique which starts from the lowest level of the
parse tree, move upwards and evaluates the rules of grammar. Therefore, the bottomup parsing
technique makes an attempt to decrease the input string to the start symbol of the grammar.

In bottom-up parsing, the parsing of a tree starts from the leaf node (bottom node) of the parse tree
and works towards the start node of the parse tree. Hence, it works in a bottom-up manner so its
named.

The bottom-up parsing technique makes use of rightmost derivation. The main rightmost decision
is to select when a production rule is used to reduce the string to get the starting symbol of the
parsing tree.
Q#2
Q#3
Q#1

Same Same But year Different

Q#2
Q#3

Ager Tumha Ata to moja b send kr dn a


Email ID: mr.noman.tariq01@gmail.com
Q1: what is the role of symbol table ? Discuss suitable
data structures for symbolic table.
The symbol table is a crucial data structure used in programming languages, particularly during the
compilation process. It plays a significant role in semantic analysis and code generation. Here's a
detailed look at its role and suitable data structures for implementing it:

Role of Symbol Table


Identifier Storage:

The symbol table stores information about identifiers (like variable names, function names, class
names) used in the program. Each entry typically contains the identifier name, type, scope, and
sometimes additional information such as memory addresses.

Scope Management:

It helps manage the scope of identifiers. Different scopes might have identifiers with the same
name but different attributes. The symbol table ensures that the correct identifier is accessed
according to its scope.

Type Checking:

During semantic analysis, the symbol table is used to check for type consistency in expressions
and assignments. It ensures that operations are performed on compatible data types.

Code Generation:

During the code generation phase, the symbol table provides necessary information about memory
locations of variables and the linkage of functions, which is essential for generating correct and
efficient machine code.

Suitable Data Structures for Symbol Table


Hash Table:

This is one of the most common implementations due to its efficient average time complexity for
insertions, deletions, and lookups, which is O(1) on average. Hash tables handle collisions using
techniques like chaining or open addressing.

Binary Search Trees (BST):

A BST can also be used, particularly when a sorted order of elements is beneficial, such as in lexical
scopes. The average time complexity for operations in a balanced BST is O(log n).

Linked Lists:

In simpler implementations or when the number of identifiers is small, linked lists might be used.
They are particularly useful when implementing scopes as a stack of linked lists, where each block
or function scope pushes a new linked list onto the stack.
Trie (Prefix Tree):

Tries are beneficial for fast retrieval of strings and can be particularly useful for autocomplete
features in development environments. They store identifiers in a way that common prefixes are
only stored once, which can be memory efficient.

Stacks:

For managing scopes, especially in block-structured languages like C or Java, stacks can be used
where each block pushes its scope onto the stack and pops it off when exiting the scope.

What are parsing techniques? Briefly discuss various


techniques used in Top-Down parsing ?
Parsing is the process by which a sequence of symbols (often text in a programming language) is
analyzed to determine its grammatical structure with respect to a given formal grammar. Parsing
techniques are critical in computer science, especially in the fields of compilers, natural language
processing, and data interpretation.

Top-Down Parsing
Top-Down Parsing technique is a parsing technique which starts from the top level of the parse
tree, move downwards, evaluates rules of grammar. In other words, top-down parsing is a parsing
technique that looks at the highest level of the tree at start and then moves down to the parse tree.

The top-down parsing technique tries to identify the leftmost derivation for an input. It evaluates the
rules of grammar while parsing. Consequently, each terminal symbol in the top-down parsing is
produced by multiple production of grammar rules.
Since top-down parsing uses leftmost derivation, hence in this parsing technique, the leftmost
decision selects what production rule is used to construct the string.

Top-Down Parsing with Backtracking: Recursive Descent Parser


A Recursive Descent Parser is a type of top-down parser that is implemented using a series of
recursive procedures, one for each non-terminal in the grammar. This parser type may involve
backtracking if it initially chooses the wrong production rule and needs to revert that choice to
make a new one.

Example: Suppose we have a grammar:

• 𝐸→𝐸+𝑇 ∣ 𝑇

• 𝑇→𝑇∗𝐹 ∣ 𝐹
• 𝐹→(𝐸) ∣ id

Top-Down Parsing without Backtracking: LL(1) Parser


Definition: An LL(1) Parser is a more efficient type of top-down parser that uses a single lookahead
token to decide which production rule to apply, thereby eliminating the need for backtracking. It
requires a parsing table that uses the current non-terminal and the lookahead token to make
parsing decisions deterministically.

Example
Uper LL(1) WALA question Use kr lo

Q3: a) Eliminate left recursion from the following :

i) S -> SX | SSh | XS | a

ii) S-> S0S1S | 01


B)
Briefly discuss various phases of a compiler design ?

Lexical Analysis:
This initial phase involves scanning the source code character by character to convert it into
meaningful lexemes, which are then categorized into tokens (such as keywords, operators,
identifiers, literals). The lexical analyzer (or scanner) removes whitespace, comments, and other
non-essential characters, simplifying the input for the next phases. Errors like invalid symbols or
characters are also detected and reported in this stage.

Syntax Analysis (Parsing):


After tokens are created, the syntax analysis phase organizes them into a parse tree that reflects
the grammatical structure of the program based on the language's syntax rules. This tree-based
representation helps ensure that the program's structure is valid (e.g., brackets are correctly paired,
statements are properly terminated). If there are syntax errors, like a missing parenthesis or an
unmatched brace, they are detected here and appropriate error messages are generated.

Semantic Analysis:
Following syntax analysis, the semantic analyzer works with the parse tree to check for semantic
correctness of the program. This involves ensuring that operations are performed on compatible
types, variables are declared before use, and function calls match definitions in terms of number
and type of arguments. The output is an enhanced version of the parse tree, known as an abstract
syntax tree (AST), which captures the logical flow and the meaning of the program without
unnecessary syntactic details.

Intermediate Code Generation:


In this phase, the compiler translates the AST into an intermediate code that is higher-level than
machine code but more abstract than most high-level programming languages. This code could be
in the form of three-address code, or quadruples, or some other form convenient for further
processing. The intermediate representation is crucial as it serves as a bridge between source code
and machine code, making it easier to implement and optimize for multiple target platforms.

Optimization:
The optimization phase attempts to improve the intermediate code so that it runs faster and uses
fewer resources. Optimizations can occur at various levels and may involve removing redundant
code, minimizing memory access, exploiting hardware architecture, and more. This phase is critical
for performance-critical applications but must preserve the original semantics of the program.

Code Generation:
The final phase of the compiler is where the optimized intermediate code is translated into the
machine code specific to the target processor architecture. This involves choosing appropriate
machine instructions, allocating registers, managing memory access, and handling system calls
and interrupts. The output is either an executable or object code ready to be executed by the
hardware.
Q#4
Q#5
MCQs(Paper1)

The graph that shows basic blocks and their successor relationship is
called
a) DAG b) Flow Chart c) Control Graph d) Hamilton graph

The output of a lexical analyzer is


a) Machine code b) Intermediate code c) A stream of tokens d) A parse tree

________ is a graph representation of a derivation


a) The parse tree b) Oct tree c) Binary tree d) None

Which one of the following is a top-down parser?


a) Recursive Descent parser b) Operator precedence parser

c) An LR(k) parser d) An LALR(k) parser

Which phase of compiler is Syntax Analysis?


a) First b) Second c) Third d) None
A grammar for a programming language is a formal description of
a) Syntax b) Semantics c) Structure d) Library

Which of these features of assembler are Machine-Dependent


a) Instruction formats b) Addressing modes c) Program relocation d) All

The fourth Generation computer was made up of?


a) Transistor b) Vacuum tubes c) Chips d) Microprocessor chips

The output of lexical analyzer is


a) A set of RE b) Syntax Tree c) Set of Tokens d) String Character

The symbol table implementation is based on the property of locality of


reference is
a) Linear list b) Search tree c) Hash Table d) Self Organization

Which concept of grammar is used in the compiler


a) Lexical analysis b) Parser c) Code generation d) Code optimization

Which of the following are Lexemes?


a) Identifiers b) Constants c) Keywords d) All
MCQs(Paper1)
The concept of FSA is much used in this part of the compiler
a) Lexical analysis b) Syntax analysis c) Semantic analysis d) Code generation

Parsing is also known as


a) Lexical analysis b) Semantic analysis c) Syntax analysis d) Code generation

In a compiler, the data structure responsible for the management of


information about variables and their attributes is
a) Semantic stack b) Parser table c) Abstract syntax tree d) Symbol table

LR parsing ____ a string to the start symbol by inverting productions.


a) Adds b) shifts c) reduces d) none of these

In ___ certain checks are performed to ensure that components of a


program fit together meaningfully.
a) Hierarchical analysis b) Linear analysis c) Semantic analysis d) none of
these

Shift reduce parsers are


a) Top Down parser b).Bottom up parser c) Both of these d) none of these

tree in which each node represents an operator and children of the node
represent the operands.
a) Abstract syntax b) Parse c) Concrete d) none of these

In ____ compiler, Source program is read by


a) Lexical analysis b) Syntax analysis c) Analyst d) Code generation

is the phase, which supports macro substitution and conditional


compilation.
a) Syntax b) preprocessing c) Semantic analysis- d) none of these

LR stands for
a) Left to right b) left to right reductions c) right to left d) none of these
Intermediate Representation (IR) stores the value of its operand in
a) stack b) queue c) registers d) none of these

Intermediate code generation phase gets input from


a) Syntax b) Semantic analysis c) Lexical analysis d) none of these

The output of a lexical analyzer is


a) Machine code b) Assembly code c) parse tree d) stream of tokens

For a LL parser, grammar must be


a) left factored b) left recursion free c) unambiguous d) all of these

Source program is read


a) character by character b) line by line c) module wise d) page by page

Token for "world" is


a) keyword b) string c) Id d) Literal

The concept of grammar is much used in this part of the compiler


a) Lexical analysis b) Parser c) Code generation d) Intermediate code

Output of parser is
a) Tokens b) Parse tree c) Object code d) Intermediate code

Which of the following are parsers for bottom-up parsing?


a) LALR(k) b) LR(k) c) SLR (k) d) all of these

Which of the following is not a phase of compiler?


a) Lexical analysis b) Syntax analysis c) Training d) Code generation
Reference sites(take help from):

Greeksforgreeks
GitHub
ChatGPT -4
Gemini Pro

(IF FIND ANY MISTAKE CONTACT ME )

• Mr.noman.tariq@outlook.com
• 03700204207(WhatsApp only)

You might also like