0% found this document useful (0 votes)
57 views83 pages

CD Work Book-Even Sem 2-0-2-0

Uploaded by

Dhanunjay p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views83 pages

CD Work Book-Even Sem 2-0-2-0

Uploaded by

Dhanunjay p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 83

COMPILER DESIGN

22CS2235

STUDENT ID: ACADEMIC YEAR: 2023-24


STUDENT NAME:
Table of Contents

1. Session 01: Elimination of white spaces and comments ............................................ 1


2. Session 02: Building Lexical Analyzer .................................................................... 8
3. Session 03: Lexical Analyzer using the LEx tool ................................................... 19
4. Session 04: Elimination of Left Recursion from given CFG .................................... 29
5. Session 05: Program to compute FIRST set ........................................................... 38
6. Session 06: Program to compute FOLLOW set ...................................................... 47
7. Session 07: Construction Of LL(1) Parsing table ................................................... 57
8. Session 08: Build Shift-Reduce parser ................................................................... 63
9. Session 09: Implementation of LR parser ................................................................ 68
10. Session 10: Quadruple notation .............................................................................. 72
11. Session 11: Code optimization techniques ............................................................. 76
A.Y. 2023-24 LAB/SKILL CONTINUOUS EVALUATION
S.No Date Experiment Name Pre- In-Lab (25M) Post- Viva Total Faculty
Lab Program/ Data Analysis Lab Voce (50M) Signature
(10M) Procedure and and (10M) (5M)
(5M) Results Inference
(10M) (10M)
1. Elimination of white spaces and
comments
2.
Building Lexical Analyzer
3. Lexical Analyzer using the LEx
tool
4. Elimination of Left Recursion
from given CFG
5. Program to compute FIRST set

6. Program to compute FOLLOW


set
7. Construction Of LL(1) Parsing
table
8. Build Shift-Reduce parser

9. Implementation of LR parser

10. Quadruple notation

11. Code optimization techniques


Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Session 1:
1. Experiment Title: Elimination of white spaces & comments.
Aim/Objective:
The aim of this lab session is to introduce students to programming for some small program in a HLL, as
C, that enables them to understand the working of small subset of the Lexical analyser. This familiarity
will enable them to build confidence, and understanding of the overall module of the lexical analysis
(scanning).
Description:
 In this lab session, students will learn about To understand how to eliminate white space and
comments
Pre-Requisites:
Students should have a basic understanding of the C language, and the ability to program using different
constructs of the same.

Pre-Lab Questions:

1a. Draw the state transition diagram for recognizing white Spaces.

1b. Draw the state transition diagram for recognizing comments (of both the types permitted in C).

2. Draw state transition Diagram for the keyword: Begin.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 1 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

3. Write Regular Expressions for the following patterns. Use auxiliary definitions wherever
convenient.
I. The set of Words having a, e, i, o, u appearing in that order, although not
necessarily consecutively.
II. Comments in C

4. Convert the regular expression (((00)*(11)) ∪ 01)* into an NFA.

5. Write regular expressions for the following languages over the alphabet Σ = {0, 1}. Hint: some of
these languages may include ε.
(a) The set of all strings that do not contain the substring 00.

(b) The set of all strings that contain at least three 1s.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 2 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

(c) The set of strings where all characters must appear in consecutive pairs (i.e. 00 or 11). Examples
of strings in the language: ε, 000011, and 11. Examples of strings not in the language: 11100, 00100,
and 11000).

In-Lab Questions:
1. Problem Definition: Removing white spaces.
Write a program to eliminate the white spaces between the words of a text.

Example:
Enter any statement, in input, say:

Welcome to Compiler Design Lab

That should lead to output sentence without any spaces:


WelcometoCompilerDesignLab

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 3 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

2. Problem Definition: Removing comments (of both types permitted in the C-language) from a C
program.

Post Lab Questions:


1.a) Write a program to find whether given string is constant or not.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 4 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

1.b) Write a C program to test whether a given identifier is valid or not.

2) Write a C program to recognize strings having the patterns, given by the following regex: ‘a’, ‘a*b+’,
‘abb’.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 5 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Viva Voce Questions:


Q1. Define a regular expression. Give example. Write its applications.
Ans)

Q2. Differentiate between a compiler and an interpreter.


Ans)

Q3. Differentiate between NFA and DFA.


Ans)

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 6 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Q4. What is the difference between a compiler’s pass and phase, in the stated context?
Ans)

Q5. Differentiate front end and back end. List out the reasons for separating lexical analysis from syntax
analysis, i.e. why the need for separation of the first two phases arose. What would be benefits and
disadvantages, if the given first two modules, were clubbed in one?
Ans)

(For Evaluator’s use only)

Comment of the Evaluator (if Any) Evaluator’s Observation

Marks Secured: out of

Signature of the Evaluator Date of Evaluation

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 7 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Session 2:
Experiment Title: Design a lexical analyzer for a given language (i.e., a set of strings accepted)
using a C language program.
Aim/Objective:
The aim of this lab session is to introduce students to programming a scanner for some small program (set
of input strings) in a HLL, as C. This familiarity will enable them to kickstart into the topic, while
comparing their program with an actual one, such as Lex. That would make them aware of their
shortcomings in terms of the breadth and depth of the subject of lexical analyser, for a real programming
language C.
It is hoped that this will make them improve upon their version, by goading them to follow the industry
standards.
Learning Outcomes:
• To understand how to design a lexical analyser for a given language
Pre-Lab Task:
1. Differentiate Tokens, Patterns, and Lexemes

2. State the role of the lexical analyzer. Identify the lexemes and their corresponding tokens in the
following statement:
printf (“Simple Interest=%f\n”, si);
Ans)

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 8 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

3. Identify the tokens in the following statement:


a) int a = 10;
b) int main()
{
printf("Welcome to GeeksforGeeks!"); return 0;
}

4. For the design of scanner (lexer/ lexical-analyzer), show how the below two factors guide the
choice of tokens:
(a) The language whose expressions in a program need be recognized.
(b) Parser used for the language.
Ans)

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 9 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

5. (a) Select the correct choice for the meaning of the below atomic regular expression, with
reasoning:
ε : 1. {""}, 2. ∅ = { }.
Ans)

b) A Lex program may be ambiguous, if some particular string may match more than one
translation expression.
If the input matches more than one expression, Lex uses the following rules to determine
which action to take:
1. Longest match: The rule that matches the longest possible input stream is preferred.

I.e. given rules as: a+(aUb)*, ab(aUb)*; and input as : abbb, the second rule is preferred as the
pattern in it matches the longest input stream. An alternative way to say the same is that the
rule that reflects, in terms of longest number of exact inputs, the state diagram of the DFA of
the matched pattern(s).

2. Rule priority: If more than one rule matches an input of the same length, the rule that
appears first in the translations section is preferred, and the later rule(s) matching the given
input are ignored.

(i) Elaborate, in below fragment of Lex program, how ambiguity in the design of lexer is
removed by having lookahead, to enable the rule of ‘ matching the longest pattern’, to
recognize the string: ‘aaaaa’:

%%
[[:lower:]] { putchar(*yytext); }
aaa* { printf("abc"); }

Ans)

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 10 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

(ii) Show why the below fragment of Lex program, is ambiguous for recognizing the input string: ‘abbb’:
letter [[:lower:]]
%%
a({letter})* { return('A'); }
ab({letter})* { return('B'); }

Will the below modification of the above program, solve the issue?
If yes, why?

letter [[:lower:]]
%%
ab({letter})* { return('B'); }
a({letter})* { return('A'); }
Ans)

(iii) Show why the below pattern of Lex rule, is needed to express Real numbers.
([0-9]+"."[0-9]*)|([0-9]*"."[0-9]+) {return REAL;}
The below DFA state diagram, for recognizing Reals, can be used to provide explanation.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 11 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

c) Lexical analyzers often have to know what the next input character is going to be without
actually reading past it. They must look ahead by some number of characters. Similarly, they
often need to read past the end of a lexeme in order to recognize it, and then push back the
unnecessary characters onto the input stream.
Traditional lexical analyzers based on minimum deterministic finite automata
for regular expressions cannot handle the look-ahead problem. The scanner writer needs to
explicitly identify the look-ahead states and code the buffering and re-scanning operations by
hand, or use tools that achieve the same effect by having lookahead token and states, as Lex,
Flex, JFlex, etc.The disadvantage is more time, but advantage is lesser state keeping.

If the below DFA were used by Lex to find tokens in a file, then:
a) How many characters past the end of a token might Lex have to examine before matching
token.
b) Assume your answer to the part (a) is: k, show an input file containing at least two tokens,
such that the first call to Lex will examine k characters past the end of the first token, before
returning to the first token.
If answer to the part (a), i.e. k =0, then show an input file containing at least two tokens, and
indicate the endpoint of each token.

Ans)

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 12 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

d) Consider the following scanner for a JavaScript-like language that has both = = and = = =
operators:
%% == { return T_Equal ; }
=== { return T_StrictEqual ; }
For the input string "====".

This can be broken into ’= =’ followed by ’= =’.


But, the largest possible match stretegy will not be able to do the same.

Elaborate why? Also, what sort of error it should produce.


Ans)

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 13 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

In-Lab Experiments:
1. Problem Definition: Identify below stated, different tokens in the given program. Different
tokens, for this program purpose, are:
i. Keywords
ii. Identifiers
iii. Operators
iv. Constants
v. Special Characters
Writing space of the Problem:

Input:
//var.c
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
a=1;
b=2;
c=a+b;
printf("Sum:%d", c);
}

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 14 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Output:

2. Complete the first experiment, with the given input, using regular expressions to specify all given
tokens. There would be need to include the regex.h library file for the same.

Post-Lab Experiments:
1. Design a lexical analyzer for given language, with support for identifiers. It should ignore
redundant spaces, tabs, and new lines. It should also ignore comments. Although the syntax
specification states that identifiers can be arbitrarily long, you may restrict the length to some
reasonable value. Simulate the same in C language, with a program.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 15 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

2. Design a lexical analyzer for a given language, that has variables, and constants –
recognized under the category of : identifier.
It should ignore redundant spaces, tabs, and new lines. It should also ignore comments.
Although the syntax specification states that identifiers can be arbitrarily long, you ‘may’
restrict the length to some reasonable value.
Simulate the same in C language, with a program.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 16 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Viva voce questions:


Q1. Explain bootstrapping a compiler with suitable diagrams.
Ans)

Q2. State different functions of lexical analyzer?


Ans)

Q3. Define cross-compilers.


Ans)

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 17 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Q4. Classify approach that you would use to recover errors in lexical analysis phase?
Ans)

Q5. Point out why is buffering used in the lexical analysis? Differentiate between buffering and
Lookahead.
Ans)

(For Evaluator’s use only)

Comment of the Evaluator (if Any) Evaluator’s Observation

Marks Secured: out of

Signature of the Evaluator Date of Evaluation

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 18 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Session 3:
Experiment Title: Lexical analysis using the Lex tool.
Aim/Objective:
The aim of this lab session is to make the students conversant with the details of the Lex tool, which is an
off-the-shelf tool for building scanners.
Description:
In this lab session, students will learn how to use Lex to build for a given smallish subset of C, or any
other language, using the given off-the-shelf tool.
Pre-Requisites:
Students should have a basic understanding of lexical analysis process, C language.

Pre-Lab Experiments:
1. Write installation procedure for Lex tool. The easiest way, is to install Cygwin, over Windows.
That obviates the unnecessary need to have a separate Linux partition, or system.
1. Convert the following NFA to a DFA using Jflap:

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 19 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

2.Explain how to run Lex programs on Windows Platform, as Windows is the preferred environment, for
the user community, at large.

3. Design an NFA that accepts strings over the alphabet {a, b} where "ab" appears as a substring but not
"ba”. Convert the NFA to DFA using Jflap.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 20 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

In-Lab Experiments:
Q.1. To write a program for implementing a Lexical analyzer using Lex tool in Linux / Cygwin platform,
with output as a sequence of tokens, for a subset of the C-language, as given by the input.

State the output of the below input program, on first running by lex(flex), and then compiling the output
of the lex(flex), with the C compiler. Then run the executable file.

INPUT:
//var.c #include<stdio.h> #include<conio.h> void main()
{
int a,b,c; a=1;b=2;
c=a+b; printf("Sum:%d",c);
}
OUTPUT:

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 21 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

2. Write a Lex program to recognize the tokens of a language different from C, here: the language given
by the below Java program:

import java.lang.*;
import java.io.*;
public class Unit{
int emp_id;
int salary;

public Unit()
{
emp_id = 8141;
salary = 123456;
}
public static void main(String[] args)
{
Unit u = new Unit();
System.out.println(“Employee Id no:”+ t.emp_id+ “ salary:”+u.salary);
}
}

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 22 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Post-Lab Experiments:
1a) Write a Lex program to recognize a float number.

1b) Write a Lex program to convert a floating-point number to an integer.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 23 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

2a) Write a Lex program to find whether the given string representing an integer, is Armstrong or not.

2b) Write a Lex program to print given string in reverse.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 24 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

2c) Write Lex program to implement a simple calculator.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 25 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

viva voce questions:


Q1. Write cousins of compiler.
Ans)

Q2. What is the output of Lexical analyzer? State the syntax of Lex Program.
Ans)

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 26 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Q.3. For Java language, the lexer be built in another language, as say: C, i.e. say using Lex/Flex.
Please state how it is possible. But, then why JFlex is used as pre-built tool for generating the lexical
analyzer, for Java? Elaborate using some small program in Java, for comparing lexical analysis in JFlex,
vs Lex.
Ans)

Q.4. Why the regular expressions are not enough for syntax analysis, i.e. why the need arose for parser
(CFG, recursive descent, predictive) having productions: rules that produce rhs from a lhs, (for top-down
parsing), or lhs from rhs (for bottom-up parsing) of a substitution rule)? Explain in the context of the
below :
(a) Patterns having a recursive structure: HTML/XML open/close tags, open/close braces ({, }) in
programming languages, open/close parenthesis, i.e. ‘(‘, ‘)’, in arithmetical expressions.
(b) Arithmetical expressions, expressible only as a tree; say: (2 + (3 * (7-4))) expressible only as:
+
/ \
2 *
/ \
3 -
/ \
7 4

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 27 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

( c) Conditional statements, as: if-then else clause, while statement.

(d) Iteration statements, as: for (int i = 0; i<= 14; i++)

Q.5. Kindly elaborate the below paragraph, in the context of the below C- language expression:
sum = 3 + 2;

The token categories are: identifier, assignment operator, integer literal, addition operator, eos; that are
assigned the integer values of: 0, 1, 2, 3, 4, respectively; apart from the: illegal input, being assigned
integer value of 999.

Lex generates a function yylex(), that generates an integer for categorization for each lexeme (recursively
till the end of file returned by yyin()), using finite-state-machines. It also returns global information (used
by the later modules) with information.

Lex uses an integer, yychar, holds the category for the lexeme. This lexeme is stored as a string in yytext,
to hold the pointer to the start of the array of characters in the matched string.

Finally, Lex uses yyleng, an integer variable, to tell how many characters were matched, or the length of
the lexeme string stored in yytext.

(For Evaluator’s use only)

Comment of the Evaluator (if Any) Evaluator’s Observation

Marks Secured: out of

Signature of the Evaluator Date of Evaluation

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 28 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Session 4:
Title: Elimination of Left Recursion from the given CFG

Aim/Objective:
The aim of this lab session is to introduce students to solution of the left recursive grammars, and the issue
of infinite loop, associated with its top-down parser.
Description:
In this lab session, students will learn about CFG, and remove the problem of left recursion.
Pre-Requisites:
Students should have a basic understanding of top-down parsing, and how it is performed.

Pre-Lab Questions:
1. Given a grammar:
S→ (L)|a, L→ L, S | S
(i) Is the grammar ambiguous? If yes, then justify by generating at least 2 distinct parse trees, using
this grammar, for some (any, i.e. at least one) input (string). If not, modify the grammar to make
it ambiguous. What is the definition of an ambiguous grammar?
(ii) Give the parse tree for the input string (a, ((a, a), (a, a))), for the above grammar.
Ans)

2. Explain left recursive grammar by example. What are the steps in removing left recursion?

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 29 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

3. (a) Differentiate between direct and indirect left recursion.


(b) Is the below grammar an example of direct or indirect left recursion?
A → Bb | …
B → Aa | …
Ans)

4. Why removal of left recursion is important? Illustrate with the below grammar, the error it can produce.
exp → exp + term | exp – term | term
Ans)

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 30 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

5. (a) Will left recursion removal affect the language (i.e., the set of strings) being recognized?
(b) Will left recursion removal affect the grammar (the set of production rules used), and hence also
the parse trees?
(c) Given the below grammar, having left recursion, and the associated problem of infinite looping in
top-down parsers; show modified grammar and the two grammars’ based parse tree, for the input string:
id + id + id.
E → E + T, T→ F, F → id.
Ans)

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 31 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

In-Lab Experiments:
1. Write a Program to eliminate Direct Left recursion from the given CFG.
As the top-down parser is sensitive to Left Recursion, we must eliminate it before applying the
top-down parsing algorithm.
The following algorithm can be used to eliminate the Direct left recursion in the given CFG.
Assign an ordering A1,…..,An to the nonterminal of the grammar.
for i:=1 to n do begin
for j:=1 to i−1 do begin
for each production of the form Ai→Ajα do begin
remove Ai→Ajα from the grammar
for each production of the form Aj→β do begin
Add production rule Ai→βα , to the grammar
End of for loop
End of for loop
End of for loop

Input :

Output:

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 32 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

2. Write a Program to eliminate Indirect Left recursion from the given CF.
The algorithm to remove indirect left recursion, needs more systematic approach than removal of direct
left recursion.

There are two-step:


a) forward substitution to convert indirect left recursion, to direct left recursion,
b) rewriting direct left recursion as right recursion.

It assumes that the original grammar has no cycles, no ∈ - production.


The algorithm imposes an order on the nonterminals: A1, A2, ..., An.
for i ← 1 to n do;
for j ← 1 to i - 1 do;
if ∃ a production Ai→Aj γ
then replace Ai→Aj γ with one or more productions that expand Aj
end;
rewrite the productions to eliminate any direct left recursion on Ai
end;

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 33 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Post-Lab Experiments:
1) Write program to eliminate direct left recursion in the following grammar:
A → ABd | Aa | a
B → Be | b

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 34 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

viva voce questions:


Q1. Define ambiguous grammar? Explain it with an example.
Ans)

Q2. Check whether the grammar S→aAB, A→bC|cd, C→cd, B→c|d, is ambiguous?
Ans)

Q3. Discuss about Left Factoring, with example of the below two grammars (the bold & italic font, is
for a terminal symbol):
(a) if-stmt → if (exp) statement
| if (exp) statement else statement
statement → if-stmt | other
Ans)

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 35 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

(b) exp → exp addop term | exp mulop term | term


term → 0|1|2|…|9
addop → + | -
mulop → *| /
Ans)

Q. 4. Eliminate left recursion for given grammar S → Sa | ∈


Ans)

Q.5. Below is classic expression grammar:


0. Goal → Expr
1. Expr → Expr + Term
2. | Expr - Term
1. | Term
2. Term → Term * Factor
3. | Term / Factor
4. | Factor
5. Factor → (Expr)
6. | num
7. | name
From which right recursive variant is derived after removal of left recursion as:
0. Goal → Expr
1. Expr → Term Expr’
2. Expr’ → + Term Expr’
3. | - Term Expr’
4. | e
5. Term → Factor Term’

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 36 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

6. Term’ → * Factor Term’


7. | / Factor Term’
8. | e
9. Factor → (Expr)
10. | num
11. | name

Show that this grammar does not suffer from left factoring.
Show the significance of removal of left factoring, for a non-backtracking recursive parser; by
comparing the execution for the two above grammar shown above; on the given input strings:
a) num + num +num*num
a) num +num*num

For Evaluator’s use only)

Comment of the Evaluator (if Any) Evaluator’s Observation

Marks Secured: out of

Signature of the Evaluator Date of Evaluation

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 37 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Session 5 :
Experiment Title: Compute the FIRST set.

Aim/Objective:
To make the student conversant with the predictive parser (top-down) need to know the FIRST set, in
order to parse without backtracking.
Description:
The experiment involves the following steps:
i. Understand the working of a top-down parser.
ii. How is there the need to backtrack in the normal top-down search.
iii. Understand why the given parsing, needs for correct prediction the FIRST set.
iv. How to compute the FIRST set for the different grammar symbols.
Pre-Requisites:
Students should have a basic understanding of top-down search process, and the left-to-right derivation
that can be derived from that in parsing, with left side symbol of a production replaced by the rhs string.
Pre-Lab Questions:
1. Construct FIRST set for the Grammar:
E→ E+T/T,
T→ T*F/F,
F→ (E)/id.

2. Compute FIRST for the grammar: S → SS+ | SS* | a

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 38 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

3a) Prove that the given grammar is ambiguous, and eliminate ambiguity in it. G: S →
iEtSeS | iEtS | a, E→ b | c | d

3b) If for a given input string, have more than one derivations, for a given parse tree; then is the grammar
ambiguous?
The below grammar generates two parse trees (left-handed, & right-handed) for the input: bb.
S → SS,
S→ b
Is the grammar ambiguous?

Can we be sure that the above grammar is unambiguous? Why? Explain your answer considering the
input string: aaa.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 39 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

4a) Why FIRST and FOLLOW sets are needed by a predictive parser? Given an input string α = X 1X2 …
X n (the lhs being a non-terminal (NT), and the rhs being a string of terminals & non-terminals, ), what
do the First(α), Follow(α) functions denote? Given grammar with lhs as: A, and rhs as: abc|def|ghi;
compute First(α).

4b) Given the production rule as: X → Y1Y2 Y3, elaborate the logic of the below two recursive rules for
computing First(X):
1. If Y1 is not nullable, i.e. Y1 →∈, is not a production rule; then First(X) = First(Y 1).
1. If Y1 is nullable; i.e. is by some production rule, reducible to ∈, then First(X) = the set given by the
union of the non-null symbols, that can occur in the FIRST set of Y 1 ; and the FIRST set of the rest
of the rule on the rhs.
Need continue the computation of the FIRST set on the rhs, till get the first non-nullable symbol on
the left, or (if , all symbols on the right are nullable) no further symbol on the rhs is left.
FIRST (X) = (FIRST (Y1) - ∈) U FIRST (Y2 Y3),

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 40 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

5. Show that the grammar : S→ A | b, A→ cA + b, B → cB + a | b, is not LL(1), since


FIRST (A) ∩ FIRST (B) = {c}.

In-Lab Experiments:
1. The construction of a predictive parser is aided by two functions associated with a grammar
G. The functions, FIRST and FOLLOW, allow us to fill in the entries of a predictive parsing table
for G, whenever possible.
FIRST set of a grammar (terminal/non-terminal) symbol: try to drive all the terminal symbols,
the string can begin, from the given non-terminal.
FOLLOW set of a non-terminal symbol: which non-null terminals can immediately follow the
given non-terminal symbol.
The rules for finding FIRST set of a given grammar are:
1. If X is terminal or ε, FIRST (X) = {X}.
2. If X1 is a non-nullable non-terminal, then FIRST (X) = contains FIRST (X1).
3. If X is a nullable non-terminal, then need to explore recursively till the first non-nullable
symbol on the rhs, from left to right:
For each production choice X → X1X2 … Xn , FIRST (X) contains FIRST (X1) –{ ε }.
If for some i < n, all the sets FIRST (X1), FIRST (Xi) contain ε, then FIRST (X) also contains
FIRST (Xi+1) – { ε }.
If all the sets FIRST (X1), …, FIRST (Xn) contain ε, then FIRST (X) also contains ε.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 41 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Input :

Output:

Post Lab Experiments:


1. Write a program to compute FIRST function of the grammar symbols: E, T, F; for the following
grammar along with solving the associated issues of the Left recursion & left factoring; for the given left
recursive grammar.
E→ E+T|T
T→T*F|F
F→ (E)|i

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 42 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

2. Dangling Else problem is shown for the below grammar, by having two parse trees; for the input string:
if (0) if (1) other else other
statement → if-stmt | other
if- stmt → if(exp) statement | if (exp) statement else statement
exp → 0|1
Bold and italic font in the grammar is for the terminal symbols.
Write a program to compute the FIRST set of the root node for the ‘Dangling else’ problem?

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 43 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 44 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Viva-voce questions:
Q.1. Define CFG, i.e. context-free Grammar.

Q2. What is the input format to compute FIRST set? List out the rules to compute FIRST set?

Q3. For a grammar without left factoring, can recursive descent parser work without backtracking? Elaborate
with an example grammar.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 45 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Q4.a) What is meant by Predictive parser?


b) Why one symbol of lookahead is normally enough?
c) Show when more than one symbol of lookahead is needed , by giving example of a LL(2) grammar.
d) Give an example of an LL(0) grammar.

Q5. Does top-down parser always need backtracking? Explain a backtracking top-down parser, with the
example of the below grammar, for the input string: rmnd.
S → rPd, P → m | mn

(For Evaluator’s use only)

Comment of the Evaluator (if Any) Evaluator’s Observation

Marks Secured: out of

Signature of the Evaluator Date of Evaluation

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 46 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Session 6 :
Experiment Title: Implementation using a program to compute FOLLOW S
et
Aim/Objective:
To make the student conversant with the predictive parser (top-down) need to know the FOLLOW set, in
order to parse without backtracking.
Description:
The experiment involves the following steps:
i. Understand the situations where there is need to compute the FOLLOW set, for a top-down parser.
Why top-down search might need to backtrack, in absence of the FOLLOW set.
ii. How to compute the FOLLOW set.
Pre-Requisites:
Students should have a basic understanding of top-down search process, and the left-to-right derivation
that can be derived from that in parsing, with left side symbol of a production replaced by the rhs string.
Pre-Lab Experiments:
1.a) Given the definition of FOLLOW set of a non-terminal X, as Follow(X):
All terminals that can immediately follow X (not, strictly leftmost), i.e., t ∈ Follow(X) if there is a
derivation containing Xt.
The Follow set of the start symbol, always contains the end-of-input marker (#).
 For a predictive LL parser, having done the computation of the FIRST set, elaborate the need to
compute the FOLLOW set, based on the below:
For a nullable non-terminal Yi, there is need to compute what follows Yi

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 47 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

1.b) State why FOLLOW set (of a non-terminal symbol) can never contain empty set.

1.c) Why for a given set of productions, for a grammar G, the FOLLOW set of the start symbol contains
the end-of-input marker (#)?

1.d) List out various rules to compute FOLLOW function for every Non-Terminal for CFG grammar.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 48 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

2) Explain the process of handling “Dangling-ELSE” ambiguity, with the help of grammar:
statement → if-stmt | other
if- stmt → if(exp) statement | if (exp) statement else statement
exp → 0|1

Also, show how the C code below will exhibit ambiguity, as unlike Python, there is no spacing based
identation.
if (x !=0)
if (y==1/x) ok = TRUE;
else z = 1/x;
And, how to remove the ambiguity in the given C code.

In-Lab Experiments:
1. FOLLOW is used only if the current non-terminal can derive ε; then we're interested in what
could have followed it in a sentential form. (NB: A string can derive ε if and only if ε is in its
FIRST set). FOLLOW can be applied to a non-terminal only and returns a set of terminals.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 49 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Input :

Output:

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 50 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Post-Lab Experiments:
1. Write a program to compute FOLLOW of NON-Terminal for the following grammar?

E→ E+T | T
T→ T*F | F
F→ (E) | i

2. Write a program to compute FOLLOW set of non-terminal for the ‘Dangling else’ problem?

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 51 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

viva-voce questions:
Q1. Calculate FOLLOW(S) and FOLLOW(A) for the given CFG :
S→ Aa|Ac, A→ b
Ans)

Q2. What is Recursive Descent Parser? Why it is called a top-down parser, rather than a bottom-up one,
though the base case(s) occur at the leaf node level only?
Ans)

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 52 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Q.3 a) Consider the evaluation of unparenthesized arithmetic expressions, with two groups (having equal
precedence, inside a group) operators: *, /, and +, -, with the first group having higher precedence than
the second one; using either suffix (reverse polish notation/form), or prefix (polish notation/form).
Which form is amenable to construction by bottom-up parsing, and why?
Which form is amenable to top-down parsing?
Which data structure is used for each of the two types of parsing?

Q.3 b) Given the below arithmetic expressions, construct the equivalent suffix, & prefix forms; without
parenthesis. What is the use of precedence of operators, in conversion?
(i) (w + x) * (y +z)
(ii) (x+y)/z
(iii) x/y/z
Show how to extend the precedence functions to handle :
1. relop (relational operators),
2. conditional statements,
3. Unconditional transfers (goto),
4. subscripted variables,
with code below to show the implementation of arithmetic expressions:

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 53 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Q.3 c) The below CFG grammar handles the precedence (mulop above addop) , but not the left-
associativity of mulop (*, /), & addop (+,-).

exp → exp addop exp | term


addop → + | -
term → term mulop term | factor
mulop → * | /
factor → ( exp) | number

In order to enable left-associativity, need the modified grammar below:

exp → exp addop term | term


addop → + | -
term → term mulop factor | factor
mulop → * | /
factor → ( exp) | number

But, this grammar is not suitable for top-down parsing, due to left recursion.
C uses bottom-up parsing, as left recursion is not an issue there. YACC tool also uses bottom-up parsing.
State any possible modifications, so that the above modified grammar is fit for top-down parsing, as well
as enables left-associativity.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 54 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Q. 4. Why top-down parsing is stated to be based on left-to-right, search for applicable derivations,
while bottom-up parser is stated to be based on right-to-left search for applicable reductions? Explain
with example for the below set of productions, for the CFG below:
0. Goal → Expr
1. Expr → Term Expr’
2. Expr’ → + Term Expr’
3. | - Term Expr’
4. | e
5. Term → Factor Term’
6. Term’ → * Factor Term’
7. | / Factor Term’
8. | e
9. Factor → (Expr)
10. | num
11. | name

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 55 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Q.5. List out various Error detection and Error recovery strategies, for predictive LL parsers.

Ans)

(For Evaluator’s use only)

Comment of the Evaluator (if Any) Evaluator’s Observation

Marks Secured: out of

Signature of the Evaluator Date of Evaluation

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 56 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Session 7:
Implementation for constructing LL (1) parsing using a program.
Date of the Session: / / Time of the Session: to

Learning Outcomes:

 To understand how to construct a LL (1) parsing using a program.


Pre-Lab Task:
1. Construct the LL(1)
parser forG: S-
>(L)|a,
L-> L,S|S,

and check the acceptance of input string (a,(a,a))

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 57 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

2. Test whether the grammar is LL (1) or not, and construct a predictive parsing
table forfollowing grammar: S -> iEtSS1/ a, S1->eS / ε, E -> b

In-Lab Task:
The Non-Recursive predictive parsing table should be designed for each non- terminal
under terminal symbol one production is available or not should be verified. If only
one production is available for parsing then the productions are in LL(1).

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 58 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Post Lab Task:

1) Write a C program to check the given input is acceptable or not for given
productions by using the designed LL (1) table.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 59 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Viva-Voce:

Q1. Discuss in brief about LL (1) Grammars

Ans)

Q2. “Top-down parser is also considered as Left Most Derivation” Justify this with an
example.
Ans)

Q3. What is the basic step if the top of the STACK is not equal to the input symbol
andnot equal to the end of the input string.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 60 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Ans)

Q4. What is top-down parsing? What are the problems in top-down parsing?
Explaineachwith a suitable example.
Ans)

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 61 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Q5. What is an LL (1) grammar? When the grammar is said to be LL(1) grammar?

Ans)

(For Evaluator’s use only)

Comment of the Evaluator (if Any) Evaluator’s Observation

Marks Secured: out of

Signature of the Evaluator Date of Evaluation

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 62 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Session 8:

Implementation of shift-reduce parser using a program.


Date of the Session: / / Time of the Session: to

Learning Outcomes:

 To understand the implementation of shift-reduce parser using a program.

Pre-Lab Task:
1. Construct an SLR parser for the given grammar and check the
acceptance of theinput string using your own parser for the
productions:
R->R+|+R|RR|R*|a|b.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 63 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

2. Consider below CFG construct stack implementation using shift-reduce parsing

E→E+E
E→E*E
E→(E)
E→id
And the input string : id +id*id

In-Lab Task:
Shift Reduce parser attempts for the construction of parse in a similar manner as done in
bottomup parsing i.e., the parse tree is constructed from leaves(bottom) to the root(up).
A more generalform of shift reduce parser is LR parser. This parser requires some data
structures i.e.

 An input buffer for storing the input string.


 A stack for storing and accessing the production rules.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 64 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Input:

Output:

Post Lab Task:

The student should be capable of understanding the concept of shift reduce


parsing andshould have the ability to design an effective program for operator
precedence parsing.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 65 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Viva-Voce:

Q1. What is Shift reduce parser?

Ans)

Q2. What are the operations of Parser?

Ans)

Q3. What is the use of parsing table?

Ans)

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 66 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Q4. What is bottom-up parsing?

Ans)

Q5. Distinguish the top-down parsing and bottom-up parsing?

Ans)

(For Evaluator’s use only)

Comment of the Evaluator (if Any) Evaluator’s Observation

Marks Secured: out of

Signature of the Evaluator Date of Evaluation

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 67 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Session 9: Implementation of LR parser using a program.

Date of the Session: / / Time of the Session: to


Learning Outcomes:

 To implement LR Parser.
 Students should have knowledge about bottom-up parsers i.e..
Terminology,Reasoning, Properties of LR(k), How LR(0)
parsing works.
Pre-Lab Task:
1. Compute closure(I) and goto(I) for the
grammar
S -> Aa| bAc| Bc| bBa
A -> d

B -> d

2. Construct SLR parsing table fo following


grammar:
S -> L = R, S -> R, L -> *R, L -> id, R -> L

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 68 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

In-Lab Task:
An LR parser reads input text from left to right without backing up (this is true for most parsers)
and produces a rightmost derivation in reverse: it does a bottom-up parse - not a top-down LL
parse or ad-hoc parse. The name LR is often followed by a numeric qualifier, as in LR (1) or
sometimes LR(k). To avoid backtracking or guessing, the LR parser is allowed to peek ahead
at k look-ahead input symbols before deciding how to parse earlier symbols. Typically, k is 1
and is not mentioned. The name LR is often preceded by other qualifiers, as in SLR and LALR.
The LR(k) condition for a grammar was suggested by Knuth to stand for "translatable from left
to right with bound k.

Input:

Output:

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 69 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Viva-Voce:

Q1. Define parser

Ans)

Q2. What is the role of Parsers in languages?

Ans)

Q3. How many types of Parsers exist?

Ans)

Q4. Compare and contrast LR and LL Parsers.

Ans)

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 70 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Q5. What are the different types of LR Parsers?

Ans)

(For Evaluator’s use only)

Comment of the Evaluator (if Any) Evaluator’s Observation

Marks Secured: out of

Signature of the Evaluator Date of Evaluation

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 71 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Session 10: Implementation to represent a given high-level programmingstatement in


Quadruple notation using a program.

Date of the Session: / / Time of the Session: to

Learning Outcomes:

 To understand and implement a given high-level programming statement


in Quadruple notation using a program
Pre-Lab Task:
1. What is an intermediate code? Explain different types of intermediate codes forms and
represent the following statement in different forms: W = (A + B) – (C + D) + (A + B + C).

2. Write the quadruple, triple, and indirect triple for the expression (a*b) + (c+d)-(a+b+c+d)

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 72 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

In-Lab Task:
Using quadruple representation, the three-address statement x = y op z is represented by placing
op in the operator field, y in the operand1 field, z in the operand 2 field, and x in the result
field. The statement x = op y, where op is a unary operator, is represented by placing op in the
operator field, y in the operand1 field, and x in the result field; the operand2 field is not used.
A statement like param t 1 is represented by placing param in the operator field and t 1 in the
operand1 field; neither operand2 nor the result fields are used. Unconditional and conditional
jump statements are represented by placing the target labels in the result field.

Input:

Output:

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 73 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Post Lab Task:


1. Write a program in C to implement semantic rules to calculate the expression that takes an
expression with digits, + and * and computes the value.

Viva-Voce:

Q1. What is DAG?

Ans)

Q2. What is Difference between Triple and Indirect triple.


Ans)

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 74 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Q3. What is syntax tree and list out rules for construction of syntax tree?
Ans)

Q4. What is difference between syntax tree and Parse tree?


Ans)

Q5. What is the use of quadruples in compilers?


Ans

(For Evaluator’s use only)

Comment of the Evaluator (if Any) Evaluator’s Observation

Signature of the Evaluator Date of Evaluation

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 75 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

11. Implementation of Code Optimization Technique using a program.

Date of the Session: / / Time of the Session: to

Learning Outcomes:
a. To understand and implement Code Optimization Technique using a program.
Pre-Lab Task:
Program for implementation of Code Optimization Technique in for and do while loop using C
Algorithm:

1. Generate the program for factorial program using for and do-while loop to specify
optimization technique.
2. In for loop variable initialization is activated first and the condition is checked next. If
the condition is true, the corresponding statements are executed, and specified
increment / decrement operation is performed.
3. For loop operation is activated till the condition failure.
4. In do-while loop the variable is initialized and the statements are executed then the
condition checking and increment / decrement operation is performed.
5. When comparing both for and do-while loop for optimization do-while is best because
first the statement execution is done then only the condition is checked. So, during the
statement execution itself we can find the inconvenience of the result and no need to
wait for the specified condition result.
6. Finally, when considering Code Optimization in loop do-while is best with respect to
performance.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 76 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

In-Lab Task:
1. Implement a program on code optimization techniques (constant folding)

Input:

Output:

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 77 of 86
Experiment <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Post Lab Task:


1. Explain loop optimization with example.

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 22CS2235 Page 78 of 86
Experiment # <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Viva-Voce:

Q1. What are the different types of code optimization techniques?

Ans)

Q2. What is code motion and give example?

Ans)

Q3. What is loop fusion and give example on it?

Ans)

Q4. What is frequency reduction?

Ans)

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 21CS3204A Page 86 of 86
Experiment # <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Q5. What is induction variable and why the usage of a greater number of induction
variables becomes a problem about code optimization technique.
Ans)

(For Evaluator’s use only)

Comment of the Evaluator (if Any) Evaluator’s Observation

Marks Secured: out of

Signature of the Evaluator Date of Evaluation

Course Title COMPILER DESIGN ACADEMIC YEAR: 2023-24


Course Code(s) 21CS3204A Page 86 of 86

You might also like