CD Work Book-Even Sem 2-0-2-0
CD Work Book-Even Sem 2-0-2-0
22CS2235
9. Implementation of LR parser
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).
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
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.
(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:
2. Problem Definition: Removing comments (of both types permitted in the C-language) from a C
program.
2) Write a C program to recognize strings having the patterns, given by the following regex: ‘a’, ‘a*b+’,
‘abb’.
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)
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)
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)
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)
(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.
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)
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 "====".
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);
}
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.
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.
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)
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:
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.
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:
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);
}
}
Post-Lab Experiments:
1a) Write a Lex program to recognize a float number.
2a) Write a Lex program to find whether the given string representing an integer, is Armstrong or not.
Q2. What is the output of Lexical analyzer? State the syntax of Lex Program.
Ans)
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
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.
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?
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)
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)
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:
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.
Post-Lab Experiments:
1) Write program to eliminate direct left recursion in the following grammar:
A → ABd | Aa | a
B → Be | b
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)
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
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.
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.
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),
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 ε.
Input :
Output:
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?
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.
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
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
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.
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.
Input :
Output:
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?
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)
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:
Q.3 c) The below CFG grammar handles the precedence (mulop above addop) , but not the left-
associativity of mulop (*, /), & addop (+,-).
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.
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
Q.5. List out various Error detection and Error recovery strategies, for predictive LL parsers.
Ans)
Session 7:
Implementation for constructing LL (1) parsing using a program.
Date of the Session: / / Time of the Session: to
Learning Outcomes:
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).
1) Write a C program to check the given input is acceptable or not for given
productions by using the designed LL (1) table.
Viva-Voce:
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.
Ans)
Q4. What is top-down parsing? What are the problems in top-down parsing?
Explaineachwith a suitable example.
Ans)
Q5. What is an LL (1) grammar? When the grammar is said to be LL(1) grammar?
Ans)
Session 8:
Learning Outcomes:
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.
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.
Input:
Output:
Viva-Voce:
Ans)
Ans)
Ans)
Ans)
Ans)
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
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:
Viva-Voce:
Ans)
Ans)
Ans)
Ans)
Ans)
Learning Outcomes:
2. Write the quadruple, triple, and indirect triple for the expression (a*b) + (c+d)-(a+b+c+d)
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:
Viva-Voce:
Ans)
Q3. What is syntax tree and list out rules for construction of syntax tree?
Ans)
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.
In-Lab Task:
1. Implement a program on code optimization techniques (constant folding)
Input:
Output:
Viva-Voce:
Ans)
Ans)
Ans)
Ans)
Q5. What is induction variable and why the usage of a greater number of induction
variables becomes a problem about code optimization technique.
Ans)