0% found this document useful (0 votes)
47 views18 pages

CS310 Dec 2021 - KCC

1) The document outlines an end of semester examination for a Compiler Design and Construction course. It provides information on the course code, title, lecturer, date, duration, and topics to be covered. 2) The exam consists of 4 questions to be answered in 3 hours. Question 1 has 3 parts related to compiler construction tools, code generation attributes, and code optimization rules. Question 2 also has 3 parts on finite automata applications, language operations, and LL(1) grammars. 3) Questions 3 and 4 each have 3 parts involving regular expressions, bottom-up parsing, grammars; and BNF notations, top-down parsing with backtracking, and derivation trees respectively. Scores
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)
47 views18 pages

CS310 Dec 2021 - KCC

1) The document outlines an end of semester examination for a Compiler Design and Construction course. It provides information on the course code, title, lecturer, date, duration, and topics to be covered. 2) The exam consists of 4 questions to be answered in 3 hours. Question 1 has 3 parts related to compiler construction tools, code generation attributes, and code optimization rules. Question 2 also has 3 parts on finite automata applications, language operations, and LL(1) grammars. 3) Questions 3 and 4 each have 3 parts involving regular expressions, bottom-up parsing, grammars; and BNF notations, top-down parsing with backtracking, and derivation trees respectively. Scores
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/ 18

END OF SEMESTER EXAMINATIONS

1ST SEMESTER 2021/2022 ACADEMIC YEAR

DATE: DECEMBER 2021

COURSE CODE: CS310

COURSE TITLE: COMPILER DESIGN AND CONSTRUCTION

LECTURER’S NAME: GEORGE LARYEA

DURATION :3-HOURS
COURSE OUTLINE (MAIN TOPICS)
MajorTopic-1 INTRODUCTION TO COMPILER DESIGN AND
CONSTRUCTION
MajorTopic-2 LEXICAL ANALYSIS

MajorTopic-3 SYNTATIC ANALYSIS

MajorTopic-4 SEMANTIC ANALYSIS

MajorTopic-5 INTERMEDIATE AND TARGET CODE GENERATION

MajorTopic-6 CODE OPTIMIZATION

1
PART A
FOUR QUESTIONS ANSWER THREE

Question 1
a) The compiler writer, like any software developer, can profitably use modern
software development environments containing tools such as language editors,
debuggers, version managers, profilers, test harnesses, and so on. In addition to
these general software-development tools, other more specialized tools have been
created to help implement various phases of a compiler. These tools use
specialized languages for specifying and implementing specific components, and
many use quite sophisticated algorithms. The most successful tools are those that
hide the details of the generation algorithm and produce components that can be
easily integrated into the remainder of the compiler. Predict six compiler-
construction tools to implement compilation phases and discuss their purpose.

Major Topic Blooms Score


Designation
INTRODUCTION TO COMPILER DESIGN AN 7
AND CONSTRUCTION: COMPILER
CONSTRUCTION TOOLS

b) A code generator is expected to have an understanding of the target machine’s


runtime environment and its instruction set. Discuss the attributes the code
generator has to take into consideration to generate the code.
Major Topic Blooms Designation Score
CR
INTERMEDIATE AND 7
TARGET CODE
GENERATION

c) Code optimization is a program transformation technique, which tries to improve


the code by making it consume less resources (i.e. CPU, Memory) and deliver
high speed. In code optimization, high-level general programming constructs are
replaced by very efficient low-level programming codes. Discuss rules that code

2
optimizing process must follow and efforts for an optimized code to be made at
various levels of compiling the process.
Major Topic Blooms Designation Score
AP
CODE OPTIMIZATION 6

TOTAL SCORE: 20 MARKS

Question 2

a)
For any language described by a regular expression, there is a state transition
diagram that can parse strings in this language. These are called finite state
automata. Finite automata are recognizers; they simply say “yes” or “no” about
each possible input string. Discuss the applications of finite automata.
Major Topic Blooms Score
Designation
LEXICAL ANALYSIS: FINITE STATE AP 7

AUTOMATA

b)
A language is a finite set of strings over some finite set of alphabets. A language
L can be considered as a subset of the free monoid on an alphabet. It is a set of
strings or sentences over some finite alphabet.
Evaluate the following language operations:

(i) 𝐿1 𝐿2

(ii) 𝐿1 ⋃ 𝐿2

3
(iii) 𝐿31

(iv) 𝐿∗1

(v) 𝐿+1

(vi) 𝐿01

Major Topic Blooms Designation Score


EV
LEXICAL ANALYSIS: 6

LANGUAGES

c)
Consider grammar 𝐺1:

𝑆 → 𝐴𝑥 | 𝐴

𝐴 → 𝜀

Most grammars that are not left-factored are also not 𝐿𝐿(1), because if there are
two productions 𝑋 → 𝑌𝛼 and 𝑋 → 𝑌𝛽 then it is impossible to predict which
production to use when the lookahead (i.e. the next input) is any terminal 𝑡 ∈
𝐹𝐼𝑅𝑆𝑇 (𝑌). Surprisingly, however, this does not rule out one situation in which a
grammar is not left-factored and yet the grammar is still 𝐿𝐿(1). The grammar 𝐺1
is not left-factored and is 𝐿𝐿(1). Show that the grammar 𝐺1 is in fact 𝐿𝐿(1).
Major Topic Blooms Designation Score
AP
SYNTATIC ANALYSIS: 6

𝐿𝐿(1)

TOTAL SCORE: 20 MARKS

4
Question 3

a)
A finite sequence of symbols from a given alphabet will be called a string over
the alphabet. A string that consists of a sequence 𝑎1 , 𝑎2 , 𝑎3 , 𝑎4 , … … … … , 𝑎𝑛 of
symbols will be denoted by the juxtaposition 𝑎1 𝑎2 𝑎3 𝑎4 … … … … 𝑎𝑛 .
Given the strings 𝑢 = 𝑎2 𝑏𝑎𝑏 2 and 𝑣 = 𝑏𝑎𝑏 2 evaluate the following
strings:
(i) 𝑢𝑣
(ii) 𝑣𝑢
(iii) 𝑣 2
(iv) 𝜀𝑢

Major Topic Blooms Score


Designation
LEXICAL ANALYSIS: REGULAR EV 7

EXPRESSION

b)
Consider the grammar 𝐺2:
𝑆 →𝐸
𝐸 →𝐸+𝑇
𝐸 →𝐸∗𝑇
𝐸 →𝑇
𝑇 → 𝑖𝑑
Generate the input string 𝑖𝑑 + 𝑖𝑑 ∗ 𝑖𝑑 using bottom – up parsing approach and
construct the derivation tree for the input string. Give the stack implementation
5
of the configurations of the shift – reduce parser for the input string 𝑖𝑑 + 𝑖𝑑 ∗
𝑖𝑑 according to the expression of the given grammar 𝐺2.
Major Topic Blooms Score
Designation
SYNTATIC ANALYSIS / SEMANTIC AP 7

ANALYSIS:BOTTOM – UP PARSING

c)
Consider the grammar 𝐺3:
𝑆 → 𝑎𝑆𝑏|𝜀
A formal grammar is a set of rules of a specific kind, for forming strings in a
formal language. The rules describe how to form strings from the language's
alphabet that are valid according to the language's syntax. A grammar describes
only the form of the strings and not the meaning or what can be done with them
in any context. How would you show that the language of grammar 𝐺3 𝐿(𝐺3) is
given by 𝐿(𝐺3) = {𝑎𝑛 𝑏 𝑛 |𝑛 ≥ 0}.

Major Topic Blooms Score


Designation
SYNTATIC ANALYSIS: CR 6

GRAMMAR/LANGUAGES

TOTAL SCORE:20 MARKS

Question 4

a)
Consider the grammar 𝐺4:

6
𝐺4 = (𝑁 = {𝑆, 𝐴, 𝐵}, 𝑇 = {𝑎, +, (, )}, 𝑆, 𝑃) where
𝑆 →𝑎+𝐴
𝐴→𝑎+𝐵
𝐵 →𝑎+𝐵
𝐵→𝑎
Backus – Naur Form (BNF) is an alternative method of displaying productions of
context free grammar. In a context free grammar, left hand side of all productions
is single non-terminal symbols. Create the BNF notations for the productions of
the grammar 𝐺4.

Major Topic Blooms Score


Designation
SYNTATIC ANALYSIS: CONTEXT CR 7

b) FREE GRAMMAR

Consider the grammar 𝐺5:


𝑆 →𝑐𝐴𝑑
𝐴 → 𝑎𝑏 |𝑎
Backtracking means, if one derivation of a production fails, the syntax analyzer
restarts the process using different rules of same production. This technique may
process the input string more than once to determine the right production.
How would you show all the steps in parsing the following string 𝑤 = 𝑐𝑎𝑑 with
the grammar 𝐺5 in a top – down parsing by considering backtracking.
Major Topic Blooms Designation Score
SYNTATIC ANALYSIS / SEMANTIC AP 7

ANALYSIS:

TOP – DOWN PARSING

7
c)
Consider the grammar 𝐺6:
𝑆 → 𝑎𝐵|𝑏𝐴, 𝐴 → 𝑎𝑆|𝑏𝐴𝐴|𝑎, 𝐵 → 𝑏𝑆|𝑎𝐵𝐵|𝑏
A derivation tree is a graphical representation for derivation that filters out the
choice regarding replacement. A derivation tree has the important purpose of
making explicit the hierarchical syntactic structure of sentences that is implied by
the grammar.
A derivation tree or parse tree is an ordered tree in which each vertex (i.e. the root)
is labelled with the left sides of a production and in which the children of a vertex
represent its corresponding right sides of a production. The start symbol of the
derivation becomes the root of the derivation tree or parse tree. At the root of the
tree, is the non-terminal symbol which begin the derivation. Interior vertex of the
tree corresponds to a non-terminal symbol that arises in the derivation. The leaves
of the tree represent the terminal symbols that arise.
Obtain the string 𝑎𝑎𝑎𝑏𝑏𝑎𝑏𝑏𝑏𝑎 using leftmost derivation from the grammar
𝐺6 and construct the derivation tree for the grammar 𝐺6.

Major Topic Blooms Score


Designation
SYNTATIC ANALYSIS: DERIVATION CR 6

TREE

TOTAL SCORE: 20 MARKS

8
PART B

TWO QUESTIONS ANSWER ALL

Question 1
Consider the grammar 𝐺7:
𝑆 → 𝑎𝐵𝑎
𝐵 → 𝑏𝐵 | 𝜀
a)
During top - down parsing, FIRST allows us to choose which production to
apply, based on the next input symbol. Define F𝐼𝑅𝑆𝑇 (𝛼), where 𝛼 is any
string of grammar symbols, to be the set of terminals that begin strings
derived from 𝛼 with the following possibilities:
i. If 𝛼 ⟹ 𝜀, then 𝜀 is also in 𝐹𝐼𝑅𝑆𝑇 (𝛼). Hence, 𝐹𝐼𝑅𝑆𝑇 (𝛼) = {𝜀}
ii. If 𝐴 ⟹ 𝑐𝛾, then 𝑐 is in 𝐹𝐼𝑅𝑆𝑇 (𝐴). Hence, 𝐹𝐼𝑅𝑆𝑇 (𝐴) = {𝑐}
For each production 𝐴 → 𝛼 of grammar 𝐺7, evaluate 𝐹𝐼𝑅𝑆𝑇 (𝛼).
Major Topic Blooms Score
Designation
SYNTATIC ANALYSIS / SEMANTIC ANALYSIS: EV 7

FIRST SETS

b)
During top - down parsing, 𝐹𝑂𝐿𝐿𝑂𝑊 allows us to choose which production to
apply, based on the next input symbol. During panic-mode error recovery, sets
of tokens produced by 𝐹𝑂𝐿𝐿𝑂𝑊 can be used as synchronizing tokens.
For each nonterminal 𝐴 of grammar 𝐺7, compute 𝐹𝑂𝐿𝐿𝑂𝑊 (𝐴).

9
Major Topic Blooms Score
Designation
SYNTATIC ANALYSIS / SEMANTIC ANALYSIS: CR 7

FOLLOW SETS

c)
Predictive parsers are recursive descent parsers that need no backtracking and
can be constructed for a class of grammars called 𝐿𝐿 (1).
The first "𝐿" in 𝐿𝐿 (1) indicates that, scanning the input from left to right.
The second "𝐿" in 𝐿𝐿 (1) signifies that, producing a leftmost derivation.
The "1" in 𝐿𝐿 (1) indicates that, using one input symbol of look ahead at each
step to make parsing action decisions.
Like recursive descent, the 𝐿𝐿 (1) technique is top-down, goal oriented, and
predictive.
The class of 𝐿𝐿 (1) grammars is rich enough to cover most programming
constructs, although care is needed in writing a suitable grammar for the source
language.
𝐿𝐿 (1) parsing table is a two dimensional table denotes by the symbol 𝑀[𝐴, 𝑎]
with each row occupied by nonterminal symbols and each column occupied by
terminal symbols or the special symbol $.
Using the results in (a) and (b), construct the 𝐿𝐿 (1) parsing table for the
grammar 𝐺7 and pass the string 𝑎𝑏𝑎𝑎 using the 𝐿𝐿 (1) parsing table for the
grammar 𝐺7 by stack implementation. Indicate the results.
Major Topic Blooms Score
Designation
AP 6

10
SYNTATIC ANALYSIS: 𝐿𝐿 (1)

PARSING TABLE

TOTAL SCORE: 20 MARKS

Question 2

a)
Consider the grammar 𝐺8:
𝑆 → 𝑎𝑆𝑏𝑆| 𝑏𝑆𝑎𝑆| 𝜀 for the string 𝑎𝑎𝑏𝑏𝑎𝑏.
A grammar 𝐺 that produces more than one different parse tree for the same
sentence is said to be ambiguous. A grammar 𝐺 that produces more than one
different leftmost derivation or more than one different rightmost derivation for
the same sentence is said to be ambiguous.
A grammar 𝐺 is ambiguous if and only if there exists at least one sentence for
which two or more different derivation trees exist by applying either the left most
derivation or right most derivation. After applying leftmost derivation or right
most derivation even though the derivations look different and if the structure of
derivation trees obtained is the same, we cannot conclude that the grammar is
ambiguous. It is not the multiplicity of the derivations that cause ambiguity. But,

Major Topic Blooms Score


SYNTATIC ANALYSIS: AMBIGUOUS Designation 7
GRAMMAR AP
it is the existence of two or more derivation trees for the same sentence. How
would you demonstrate that grammar 𝐺8 is ambiguous?

11
b)
Consider the grammar 𝐺9:
𝐴 → 𝑎𝑑 |𝑎 |𝑎𝑏 |𝑎𝑏𝑐 |𝑏
Left factoring is a grammar transformation that is useful for producing a grammar
suitable for predictive parsing or top-down parsing.
When the choice between two alternative 𝐴 − productions is not clear, we may be
able to rewrite the productions to defer the decision until enough of the input has
been seen that we can make the right choice.
In general, if
𝐴 → 𝛼𝛽1 |𝛼𝛽2
are two A – productions and the input begins with a nonempty string derived from
𝛼.
When processing 𝛼 , we do not know whether to expand 𝐴 to 𝛼𝛽1 or 𝐴 to 𝛼𝛽2 .
However, we may defer the decision by expanding 𝐴 to 𝛼𝐴′. Then, after seeing
the input derived from 𝛼 , we expand 𝐴′ to 𝛽1 or 𝐴′ to 𝛽2 . That is, left-
factored, the original 𝐴 − productions:
𝐴 → 𝛼𝛽1 |𝛼𝛽2
become:
𝐴 → 𝛼𝐴′
𝐴′ → 𝛽1 |𝛽2
Now the parser has only one production per prefix which makes it easier to take
decisions.
How would you left factor this grammar 𝐺9 ?

Major Topic Blooms Score


Designation
CR 7

12
SYNTATIC ANALYSIS: LEFT

FACTORING

c)
Consider the grammar 𝐺10:
𝑆 → 𝐴𝑎|𝑏
𝐴 → 𝐴𝑐|𝑆𝑑|𝜀
A grammar is left recursive if the grammar is of the form:
𝐴 → 𝐴𝛼|𝛽
for some string 𝛼 where 𝛽 does not start with nonterminal 𝐴.
That is, a grammar is left recursive if the grammar has any nonterminal 𝐴 whose
derivation contains 𝐴 itself as the leftmost symbol as in the production.
Left recursion is considered to be a problematic situation for top-down parsers.
1. Top-down parsers start parsing from the start symbol, which in itself is non-
terminal. So, when the parser encounters the same non-terminal in its derivation,
it becomes hard for it to judge when to stop parsing the left non-terminal and it
goes into an infinite loop.
2. Left recursive grammar have to be converted into an equivalent grammar which
is not left
How would you show that the grammar 𝐺10 is left recursive and convert this
grammar 𝐺10 into non – left recursive by considering both order of nonterminals
as in 𝑆, 𝐴 and 𝐴, 𝑆.

Major Topic Blooms Score


Designation
SYNTATIC ANALYSIS: LEFT RECURSIVE 7
AP
TOTAL SCORE: 20 MARKS

13
ALGORITHM SHEET

A. Algorithm for Left Factoring a Grammar


For each nonterminal 𝐴, find the longest prefix 𝛼 common to two or more alternative
productions.
If 𝛼 ≠ 𝜀, there is a nontrivial common prefix 𝛼.
Replace all the 𝐴 − productions:
𝐴 → 𝛼𝛽1 |𝛼𝛽2 |𝛼𝛽3 |… … … … … |𝛼𝛽𝑛 |𝛾
where 𝛾 represents all alternative productions that do not begin with 𝛼
By
𝐴 → 𝛼𝐴′ |𝛾
𝐴′ → 𝛽1 |𝛽2 |… … … … |𝛽𝑛
where 𝐴′ is a new nonterminal.

B. Algorithm for Removing an Indirect Left Recursion


1. START
2. Initialize: Arrange non-terminals in some order like
𝐴1 , 𝐴2 , 𝐴3 , … … … … … . . , 𝐴𝑛
3. for (𝑖 ∶= 1 𝑡𝑜 𝑛)
4. {
5. for ( 𝑗 ∶= 1 𝑡𝑜 𝑖 − 1)
6. {
7 replace each production of form 𝐴𝑖 → 𝐴𝑗 𝛾
with 𝐴𝑖 → 𝛿1 𝛾 |𝛿2 𝛾 |𝛿3 𝛾 |𝛿4 𝛾 |… … … . |𝛿𝑘 𝛾
where 𝐴𝑗 → 𝛿1 |𝛿2 |𝛿3 |𝛿4 |… … … . |𝛿𝑘 are current 𝐴𝑗 productions
8. }

14
9. eliminate immediate left recursion among 𝐴𝑖 productions
(i.e. 𝐴 → 𝐴𝛼|𝛽 reduces to 𝐴 → 𝛽𝐴′ and 𝐴′ → 𝛼𝐴′|𝜀)
without changing the set of strings derivable from 𝐴
10. }
11. END
C. Algorithm for Backus – Naur Form (BNF)
1. Every non-terminal symbol is enclosed in angle brackets < >.
2. The terminal symbols are written without any special making.
3. The symbol :: = is used instead of → and should be read as “is defined as”.
4. All the productions with the same non-terminal left hand side are combined into
one statement with all the right hand sides listed on the right of :: = , separated
by vertical bars.
5. For instance, the production 𝐴 → 𝐵 is written in BNF as < 𝐴 > ∶: = < 𝐵 >
.
6. Production of the form 𝐴 → 𝐴𝑎 is written in BNF as < 𝐴 > ∶: = < 𝐴 > 𝑎
7. Production of the form 𝐴 → 𝑎𝐴 is written in BNF as < 𝐴 > ∶: = 𝑎 < 𝐴 >
8. Production of the form 𝐴 → 𝑎 is written in BNF as < 𝐴 > ∶: = 𝑎
9. Production of the form 𝐴 → 𝐴𝐵 is written in BNF as < 𝐴 > ∶: = < 𝐴 ><
𝐵>
10. Production of the form 𝐴 → 𝑎 + 𝐴 is written in BNF as < 𝐴 > ∶: = 𝑎 + <
𝐴>

D. Algorithm for Computation of 𝐹𝐼𝑅𝑆𝑇(𝑋) for all Grammar Symbols 𝑋


To compute 𝐹𝐼𝑅𝑆𝑇(𝑋) for all grammar symbols 𝑋, apply the following rules until
no more terminals or 𝜀 can be added to any FIRST set.
1. If 𝑋 is a terminal, then 𝐹𝐼𝑅𝑆𝑇(𝑋) = {𝑋}

15
2. If 𝑋 → 𝜀 is a production, then add 𝜀 to 𝐹𝐼𝑅𝑆𝑇(𝑋). That is, 𝐹𝐼𝑅𝑆𝑇(𝑋) = {𝜀}.
3. If 𝑋 is a nonterminal and 𝑋 → 𝑎𝛼 is a production, then add 𝑎 to 𝐹𝐼𝑅𝑆𝑇(𝑋). That
is, 𝐹𝐼𝑅𝑆𝑇(𝑋) = {𝑎}.
4. If 𝑋 is a nonterminal and 𝑋 → 𝑌1 𝑌2 … … 𝑌𝑘 is a production for some 𝑘 ≥ 1, then
place 𝑎 in 𝐹𝐼𝑅𝑆𝑇(𝑋) if for some 𝑖, 𝑎 is in 𝐹𝐼𝑅𝑆𝑇 (𝑌𝑖 ) and 𝜀 is in all of
𝐹𝐼𝑅𝑆𝑇(𝑌1 ), … … . . , 𝐹𝐼𝑅𝑆𝑇(𝑌𝐼−1 ), that is 𝑌1 … … … … … 𝑌𝐼−1 ⟹ 𝜀. If 𝜀 is in
𝐹𝐼𝑅𝑆𝑇(𝑌𝑗 ) for all 𝑗 = 1, 2, … … … . . , 𝑘, then add 𝜀 to 𝐹𝐼𝑅𝑆𝑇(𝑋).

E. Algorithm for Computation of 𝐹𝑂𝐿𝐿𝑂𝑊(𝐴) for all Nonterminals 𝐴


To compute 𝐹𝑂𝐿𝐿𝑂𝑊(𝐴) for all nonterminals 𝐴, apply the following rules until
nothing can be added to any 𝐹𝑂𝐿𝐿𝑂𝑊 set.
1. Place $ in 𝐹𝑂𝐿𝐿𝑂𝑊(𝑆), where 𝑆 is the start symbol, and $ is the input right
endmarker.
That is, FOLLOW(𝑆) = {$}
2. If there is a production 𝐴 → 𝛼𝐵𝛽 then everything in 𝐹𝐼𝑅𝑆𝑇(𝛽) except 𝜀 is in
𝐹𝑂𝐿𝐿𝑂𝑊(𝐵).
That is, 𝐹𝑂𝐿𝐿𝑂𝑊(𝐵) = 𝐹𝐼𝑅𝑆𝑇(𝛽), 𝑒𝑥𝑐𝑒𝑝𝑡 𝜀
3. If there is a production 𝐴 → 𝛼𝐵, or a production 𝐴 → 𝐵 or a production 𝐴 →
𝛼𝐵𝛽 where 𝐹𝐼𝑅𝑆𝑇(𝛽) contains 𝜀, then everything in 𝐹𝑂𝐿𝐿𝑂𝑊(𝐴) is in
𝐹𝑂𝐿𝐿𝑂𝑊(𝐵).
That is:
(i) For 𝐴 → 𝛼𝐵 , hence, 𝐹𝑂𝐿𝐿𝑂𝑊(𝐵) = 𝐹𝑂𝐿𝐿𝑂𝑊(𝐴)
(ii) For 𝐴 → 𝐵, hence, 𝐹𝑂𝐿𝐿𝑂𝑊(𝐵) = 𝐹𝑂𝐿𝐿𝑂𝑊(𝐴)
(iii) For 𝐴 → 𝛼𝐵𝛽, where 𝐹𝐼𝑅𝑆𝑇(𝛽) contains 𝜀 , hence, 𝐹𝑂𝐿𝐿𝑂𝑊(𝐵) =
𝐹𝑂𝐿𝐿𝑂𝑊(𝐴)

16
4. If 𝑋 is a nonterminal and has a production 𝑋 → 𝐴𝐵 where 𝐴𝐵 is a rightmost
symbol in some sentential form, then everything in 𝐹𝐼𝑅𝑆𝑇 (𝐵) is in 𝐹𝑂𝐿𝐿𝑂𝑊(𝐴)
except 𝜀.
That is, 𝐹𝑂𝐿𝐿𝑂𝑊(𝐴) = 𝐹𝐼𝑅𝑆𝑇(𝐵), 𝑒𝑥𝑐𝑒𝑝𝑡 𝜀 .
5. If 𝑋 is a nonterminal and has a production 𝑋 → 𝐴𝐵 where 𝐴𝐵 is a rightmost
symbol in some sentential form, where 𝐹𝐼𝑅𝑆𝑇 (𝐵) contains 𝜀 , then everything
in 𝐹𝑂𝐿𝐿𝑂𝑊(𝐴) is in 𝐹𝑂𝐿𝐿𝑂𝑊(𝐵). That is, 𝐹𝑂𝐿𝐿𝑂𝑊(𝐵) = 𝐹𝑂𝐿𝐿𝑂𝑊(𝐴).
6. If 𝑆 ⟹ 𝛼 𝐴 𝑎 is a production or 𝑆 ⟹ 𝛼 𝐴 𝑎 𝛽 is a production, then the
rightmost terminal symbol which is 𝑎 is in 𝐹𝑂𝐿𝐿𝑂𝑊(𝐴). Hence,
𝐹𝑂𝐿𝐿𝑂𝑊(𝐴) = {𝑎}.
For example:
For 𝐹 → (𝐸) |𝑖𝑑 , hence, the rightmost terminal symbol which is ) is in
𝐹𝑂𝐿𝐿𝑂𝑊(𝐸).
Hence, 𝐹𝑂𝐿𝐿𝑂𝑊(𝐸) = { ) }
In general, for 𝑆 ⟹ 𝛼 𝐴1 𝐴2 … …. 𝐴𝑛 𝑎 or 𝑆 ⟹ 𝛼 𝐴1 𝐴2 … …. 𝐴𝑛 𝑎𝛽 ,
then
𝐹𝑂𝐿𝐿𝑂𝑊(𝐴1 ) = {𝑎} , 𝐹𝑂𝐿𝐿𝑂𝑊(𝐴2 ) = {𝑎}, ………., 𝐹𝑂𝐿𝐿𝑂𝑊(𝐴𝑛 ) = {𝑎}
For example:
If 𝑆 → 𝐵𝐴𝑎𝐶, then 𝐹𝑂𝐿𝐿𝑂𝑊(𝐴) = {𝑎}, 𝐹𝑂𝐿𝐿𝑂𝑊(𝐵) = {𝑎}
7. In addition, if 𝐴 can be the rightmost symbol in some sentential form, then $ is in
𝐹𝑂𝐿𝐿𝑂𝑊(𝐴); where $ is a special endmarker symbol that is assumed not to be a
symbol of any grammar.
For example:
If 𝑆 ⟹ 𝐵𝐶𝑎𝐴, then nonterminal 𝐴 is the rightmost nonterminal symbol of the
sentential
𝑆 ⟹ 𝐵𝐶𝑎𝐴 . Hence, 𝐹𝑂𝐿𝐿𝑂𝑊(𝐴) = ($)

17
F. Algorithm for Constructing 𝐿𝐿 (1) Parsing Table
Input: Grammar G.
Output: Parsing table M.
Method: For each production 𝐴 → 𝛼 of the grammar, do the following:
Step 1: For each terminal "𝑎" in 𝐹𝐼𝑅𝑆𝑇 (𝛼), add 𝐴 → 𝛼 to 𝑀[𝐴, 𝑎].
Step 2: If 𝜀 is in 𝐹𝐼𝑅𝑆𝑇 (𝛼), then for each terminal 𝑏 in 𝐹𝑂𝐿𝐿𝑂𝑊(𝐴), add 𝐴 → 𝛼
to 𝑀[𝐴, 𝑏]. If 𝜀 is in 𝐹𝐼𝑅𝑆𝑇 (𝛼) and $ is in 𝐹𝑂𝐿𝐿𝑂𝑊(𝐴), 𝐴 → 𝛼 to 𝑀[𝐴, $]
as well.
Step 3: If, after performing the above, there is no production at all in 𝑀[𝐴, 𝑎], then
set 𝑀[𝐴, 𝑎] to error (which we normally represent by an empty entry in the
table).

18

You might also like