You are on page 1of 7

REVIEW QUESTIONS

11. Describe the parsing problem for a bottom-up parser

– Bottom-up parser can only identify and process the low level details of the structure of the text prior to
the secondary level and leave the overall structure of the highest level.

12. Explain why compilers use parsing algorithms that work on only a subset of all grammars

– Because parsing algorithms that works for any unambiguous grammar are complex and inefficient. In
fact, the complexity of the algorithm is O (n3). Algorithm is O (n3) is not usually useful for practical
processes, such as analysis of the syntax for the compiler, because they are too slow. In this situation,
computer scientists often look for a faster algorithm, although it is less common. In the case of parsing,
faster algorithm has listed who work only a subset of the set of all possible grammar

13. Why are named constants used, rather than numbers, for token codes?

– Because named constants can used to simplify the reading of lexical and syntactic analysis

14. Describe how a recursive-descent parsing subprogram is written for a rule with a single RHS?

– Recursive-descent parsing subprogram is written for a rule with a single RHS is relatively simple. For
each terminal symbol in the RHS, compared with nextToken terminal symbol. If they do not match, it is a
syntax error. If they match, the lexical analyzer is called to get the next input token. For each
nonterminal, parsing subprogram to be called nonterminal.

15. Explain the two grammar characteristics that prohibit them from being used as the basis for a top-
down parser.
– Two characteristics of grammar that prohibits top-down parsing: Left Recursion directly or indirectly.

PROBLEM SETS

1. Perform the pairwise disjointness test for the following grammar rules.

a. A → aB | b | cBB

b. B → aB | bA | aBb

c. C→ aaA | b | caB

a.

A -> aB | b | CBB

first(aB) = a

first(b) = b

first(CBB) = aaA = a

b.

B -> aB | ba | aBb

first(aB) = a

first(ba) = b

first(aBb) = a

They intersect and rules frustrate the test.

c.

C -> aaA | b | caB


first(aaA) = a

first(b) = b

first(caB) = c

There is not any intersect so that the rules are passed.

2. Perform the pairwise disjointness test for the following grammar rules.

a. S→ aSb bAA

b. A → b{aB} a

c. B → aB a

a.

S→ aSb | bAA

first(aSb) = a

first(bAA) = b

There is not any intersect so that the rules are passed.

b.

A → b{aB} | a

first(b{aB}) = b

first(a) = a

There is not any intersect so that the rules are passed.

c.

B → aB | a
first(aB) = a

first(a) = a

They intersect and rules frustrate the test.

3. Show a trace of the recursive descent parser given in Section 4.4.1 for the string a + b * c.

The next token is: 10 The next lexeme is a

Enter <expr>

Enter <term>

Enter <factor>

The next token is: 21 The next lexeme is +

Exit <factor>

Exit <term>

The next token is: 10 The next lexeme is b

Enter <term>

Enter <factor>

The next token is: 10 The next lexeme is *

Exit <factor>

The next token is: 26 The next lexeme is c

Enter<factor>
The next token is: -1 The next lexeme is EOF

Exit <term>

Exit <expr>

4. Show a trace of the recursive descent parser given in Section 4.4.1 for the string a * (b + c).

The next token is: 10 The next lexeme is a

Enter <expr>

Enter <term>

Enter <factor>

The next token is: 21 The next lexeme is *

Exit <factor>

The next token is: 25 The next lexeme is (

Enter <factor>

The next token is: 10 The next lexeme is b

Enter <expr>

Enter <term>

Enter <factor>

The next token is: 21 The next lexeme is +

Exit <factor>
The next token is: 26 The next lexeme is c

Enter <factor>

The next token is: 26 The next lexeme is )

Exit <term>

Exit <expr>

The next token is: -1 The next lexeme is EOF

Exit <factor>

Exit <term>

Exit <expr>

5. Given the following grammar and the right sentential form, draw a parse tree and show the phrases
and simple phrases, as well as the handle.

S → aAb bBA A → ab aAB B → aB b

a. aaAbb

b. bBab

c. aaAbBb

a. aaAbb

Phrases: aaAbb, aAb, b

Simple phrases: b

Handle: b
b.bBab

Phrases: bBab, ab

Simple phrases: ab

Handle: ab

c. aaAbBb (no answer)

You might also like