• Home
  • Textbooks
  • Crafting A Compiler
  • Bottom-Up Parsing

Crafting A Compiler

Charles N. Fischer, Ron K. Cytron, Richard J. LeBlanc

Chapter 6

Bottom-Up Parsing - all with Video Answers

Educators


Chapter Questions

Problem 1

Build the CFSM and the parse table for the grammar shown in Figure 6.2.

Check back soon!
02:25

Problem 2

Using the knitting analogy of Section 6.2.2, show the sequence of LR shift and reduce actions for the grammar of Figure 6.2 on the following strings:
(a) plus plus num num num $$$
(b) $plus num plus num num $$$

Alice .
Alice .
Numerade Educator
09:53

Problem 3

Figures 6.6 and 6.7 trace a bottom-up parse of an input string using the table shown in Figure 6.5. Trace the parse of the following strings.
(a) $q $$
(b) $c $$
(c) a d c $$$

Susan Hallstrom
Susan Hallstrom
Numerade Educator
06:18

Problem 4

Build the CFSM for the following grammar:
1 Prog ? Block $
2 Block ? begin StmtList end
3 StmtList ? StmtList semi Stmt
4 | Stmt
5 Stmt ? Block
6 | Var assign Expr
7 Var ? id
8 | id lb Expr rb
9 Expr ? Expr plus T
10 | T
11 T ? Var
12 | lp Expr rp

Chris Trentman
Chris Trentman
Numerade Educator
00:42

Problem 5

Show the LR parse table for the CFSM constructed in Exercise 4.

James Chok
James Chok
Numerade Educator
View

Problem 6

Which of following grammars are LR(0)? Explain why.
$$
\text { (a) } \begin{array}{rlrl}
1 & \mathrm{~S} & \rightarrow \text { StmtList } \$ \\
2 & \text { StmtList } & \rightarrow \text { StmtList semi Stmt } \\
3 & & \mid \mathrm{Stmt} \\
4 & \text { Stmt } \quad & \rightarrow \mathrm{s}
\end{array}
$$
$$
\text { (b) } \begin{array}{rlr}
1 & \mathrm{~S} & \rightarrow \text { StmtList } \$ \\
2 & \text { StmtList } & \rightarrow \text { Stmt semi StmtList } \\
3 & \mid \mathrm{Stmt} \\
4 & \text { Stmt } \quad \rightarrow \mathrm{s}
\end{array}
$$
$$
\text { (c) } \begin{array}{rlr}
1 & \mathrm{~S} & \rightarrow \text { StmtList } \$ \\
2 & \text { StmtList } & \rightarrow \text { StmtList semi StmtList } \\
3 & 1 \mathrm{Stmt} \\
4 & \text { Stmt } \quad \rightarrow \mathrm{s}
\end{array}
$$
$$
\text { (d) } \begin{array}{lll}
1 & \mathrm{~S} & \rightarrow \text { StmtList } \$ \\
2 & \text { StmtList } & \rightarrow \mathrm{s} \text { StTail } \\
3 & \text { StTail } & \rightarrow \text { semi StTail } \\
4 & \| \lambda
\end{array}
$$

James Kiss
James Kiss
Numerade Educator
04:39

Problem 7

Show that the CFSM corresponding to a LL(1) grammar has the following property. Each state has exactly one kernel item if the grammar is $\lambda$-free.

Chris Trentman
Chris Trentman
Numerade Educator

Problem 8

Prove or disprove that all $\lambda$-free $\mathrm{LL}(1)$ grammars are $\mathrm{LR}(0)$.

Check back soon!
00:35

Problem 9

Explain why the following grammar is unambiguous:
Start $\rightarrow$ Single a
| Double b
Single $\rightarrow 0$ Single 1
101
Double $\rightarrow 0$ Double 11
1011

Amy Jiang
Amy Jiang
Numerade Educator

Problem 10

Show the LR(0) construction for the following grammars:
Data can't copy.

Check back soon!
01:46

Problem 11

Explain why the language defined by the grammar in Exercise 9 is inherently nondeterministic-there is no LALR $(k)$ grammar for such languages.

Celine Ibrahim
Celine Ibrahim
Numerade Educator
00:47

Problem 12

Given the claim of Exercise 11, explain why the following statement is true or false:
There is no LR $(k)$ grammar for the language
$$
\left\{0^n 1^n a\right\} \cup\left\{0^n 1^{2 n} b\right\}
$$

Audrey Fong
Audrey Fong
Numerade Educator
01:11

Problem 13

Discuss why is it not possible during $\operatorname{LR}(0)$ construction to obtain a shift/reduce conflict on a nonterminal.

Manisha Sarker
Manisha Sarker
Numerade Educator

Problem 14

Discuss why there cannot be an unambiguous CFGs for the language
$$
\left\{a^i b^j c^k \mid i=j \text { or } j=k ; i, j, k \geq 1\right\}
$$

Check back soon!

Problem 15

Complete the $\mathrm{LR}(0)$ construction for the grammar in Figure 6.19.

Check back soon!
01:20

Problem 16

16. Show that $\mathrm{LL}(1)$ construction fails for an unambiguous grammar that is $\operatorname{not} \operatorname{LR}(1)$.

Adriano Chikande
Adriano Chikande
Numerade Educator
03:27

Problem 17

Show that the grammar in Figure 6.20 is $\mathrm{LR}(0)$.

Raushan Kumar
Raushan Kumar
Numerade Educator

Problem 18

Complete the $\operatorname{LR}(0)$ construction for the grammar shown in Figure 6.25. Your state numbers should agree with those shown in the partial LR(0) construction.

Check back soon!
01:34

Problem 19

Which of the grammars in Exercise 10 are LR(0)? Justify your answers.

James Kiss
James Kiss
Numerade Educator
01:13

Problem 20

Complete the SLR(1) construction for the grammar shown in Figure 6.26. Show the resulting parse table.

James Kiss
James Kiss
Numerade Educator

Problem 21

Extend the grammar given in Figure 6.21 to accommodate standard expressions involving addition, subtraction, multiplication, and division. Model the syntax and semantics for these operators according to Java or $\mathrm{C}$

Check back soon!
13:01

Problem 22

Extend the grammar as directed in Exercise 21, but introduce an exponentiation operator that is right-associating. Let the operator (denoted by " $\star$ ") have the highest priority, so that the value of $3+4 \times 5 \star 2$ is 103 .

Susan Hallstrom
Susan Hallstrom
Numerade Educator
00:49

Problem 23

Repeat Exercise 22, but add the ability to enclose expressions with parentheses to control how expressions are grouped together. Thus, the value of $((3+4) \times 5) \star 2$ is 1225 .

Ernest Castorena
Ernest Castorena
Numerade Educator
01:34

Problem 24

Which of the grammars in Exercise 10 are SLR(1)? Justify your answers.

James Kiss
James Kiss
Numerade Educator
01:25

Problem 25

Generalize the algorithm given in Section 6.5.1 from SLR(1) to SLR $(k)$.

Nick Johnson
Nick Johnson
Numerade Educator
01:10

Problem 26

Show that the following holds for any LALR(1) construction:
(a) For any state $s$ containing the item $\mathrm{A} \rightarrow \alpha \bullet \beta$,
$$
\text { ItemFollow }((s, \mathrm{~A} \rightarrow \alpha \bullet \beta)) \subseteq \text { Follow }(\mathrm{A})
$$
(b)

Adriano Chikande
Adriano Chikande
Numerade Educator

Problem 27

Perform the LALR(1) construction for the following grammar:
1 Start ? S $
2 S ? x C1 y1 C2 y2 C3 y3
3 | A1
4 A1 ? b1 C1
5 | a1
6 A2 ? b2 C2
7 | a2
8 A3 ? b3 C3
9 | a3
10 C1 ? A2
11 C2 ? A1
12 | A3
13 C3 ? A2

Check back soon!

Problem 28

Recall the EvalItemPropGraph algorithm given in Figure 6.28. Using the grammars in Figure 6.31 and Exercise 27 as a guide, show how to generate a LALR(1) grammar that requires $n$ iterations for ItemFollow sets to converge in EvalitemPropGraph.

Check back soon!

Problem 29

For the grammar shown in Figure 6.35, complete the LALR(1) construction from Figure 6.37.

Check back soon!
01:34

Problem 30

Which of the grammars in Exercise 10 are LALR(1)? Justify your answers.

James Kiss
James Kiss
Numerade Educator

Problem 31

Show the LR(1) construction for the grammar in Exercise 4.

Check back soon!
03:26

Problem 32

Define the quasi-identical states of an $L R(1)$ parsing machine to be those states whose kernel productions are identical. Such states are distinguished only by the lookahead symbols associated with their productions. Given the LR(1) machine built for Exercise 31, complete the following:
(a) List the quasi-identical states of the LR(1) machine.
(b) Merge each set of quasi-identical states to obtain an LALR(1) machine.

Chris Trentman
Chris Trentman
Numerade Educator
00:57

Problem 33

Starting with the CFSM built in Exercise 4, compute the LALR(1) lookahead information. Compare the resulting LALR(1) machine with the machine obtained in Exercise 32 .

Kian Manafi
Kian Manafi
Numerade Educator
01:18

Problem 34

34. Which of the grammars in Exercise 10 are LR(1)? Justify your answers.

Adriano Chikande
Adriano Chikande
Numerade Educator
12:36

Problem 35

Consider a grammar $G$ and its LALR(1) construction. Suppose that a shift/reduce conflict occurs in G's LALR(1) construction. Prove that G's LR(1) construction also contains a shift/reduce conflict.

Chris Trentman
Chris Trentman
Numerade Educator
03:53

Problem 36

Describe an algorithm that computes LALR(1) and then splits states as needed in an attempt to address conflicts. Take note of the issue raised in Exercise 35 .

Chris Trentman
Chris Trentman
Numerade Educator

Problem 37

Using a grammar for the C programming language, try to extend the syntax to allow nested function definitions. For example, you might allow function definitions to occur inside any compound statement.
Report on any difficulties you encounter and discuss possible solutions. Justify the particular solution you adopt.

Check back soon!

Problem 38

Using a grammar for the $C$ programming language, try to extend the syntax so that a compound statement can appear to compute a value. In other words, allow a compound statement to appear wherever a simple constant or identifier could appear. Semantically, the value of a compound statement could be the value associated with its last statement.
Report on any difficulties you encounter and discuss possible solutions. Justify the particular solution you adopt.

Check back soon!

Problem 39

In Figure 6.3, Marker(2) pushes a state on the parse stack. In the bottomup parse shown in Figures 6.6 and 6.7, stack cells show both the state and the input symbol causing the state's shift onto the stack. Explain why the input symbol's presence in the stack cell is superfluous.

Check back soon!

Problem 40

Recall the dangling else problem introduced in Chapter 5. A grammar for a simplified language that allows conditional statements follows:
Explain why the grammar is or is not LALR(1).

Check back soon!

Problem 41

Consider the following grammar:
$\begin{array}{lll}1 & \text { Start } & \rightarrow \text { Stmt } \$ \\ 2 & \text { Stmt } & \rightarrow \text { Matched } \\ 3 & \quad \text { I Unmatched } \\ 4 & \text { Matched } \quad & \text { if e then Matched else Matched } \\ 5 & \quad \text { o other } \\ 6 & \text { Unmatched } \rightarrow \text { if e then Matched else Unmatched } \\ 7 & \quad \text { if e then Unmatched }\end{array}$
(a) Explain why the grammar is or is not LALR(1).
(b) Is the language of this grammar the same as the language of the grammar in Exercise 40 ? Why or why not?

Check back soon!
00:41

Problem 42

Repeat Exercise 41, adding the production Unmatched $\rightarrow$ other to the grammar.

Natalie Anderson
Natalie Anderson
Numerade Educator

Problem 43

Consider the following grammar:
$\begin{array}{lll}1 & \text { Start } & \rightarrow \text { Stmt } \$ \\ 2 & \text { Stmt } & \rightarrow \text { Matched } \\ 3 & \quad \text { Unmatched } \\ 4 & \text { Matched } & \rightarrow \text { if e then Matched else Matched } \\ 5 & & \text { I other } \\ 6 & \text { Unmatched } & \text { if e then Matched else Unmatched } \\ 7 & \quad \text { if e then Stmt }\end{array}$
(a) Explain why the grammar is or is not LALR(1).
(b) Is the language of this grammar the same as the language of the grammar in Exercise 40 ? Why or why not?

Check back soon!

Problem 44

Based on the material in Exercises 40,41 , and 43 , construct an LALR(1) grammar for the language defined by the following grammar:
$$\begin{array}{ll}1 & \text { Start } \rightarrow \text { Stmt } \$ \\ 2 & \text { Stmt } \rightarrow \text { if e then Stmt else Stmt } \\ 3 & \text { if e then Stmt } \\ 4 & \text { while e Stmt } \\ 5 & \text { repeat Stmt until e } \\ 6 & \text { other }\end{array}$$

Check back soon!
01:23

Problem 45

Show that there exist non-LL(1) grammars that are
(a) $\operatorname{LR}(0)$
(b) SLR(1)
(c) LALR(1)

James Kiss
James Kiss
Numerade Educator

Problem 46

Normally, an LR parser traces a rightmost derivation (in reverse).
(a) How could an LR parser be modified to produce a leftmost parse as LL(1) parsers do? Describe your answer in terms of the algorithm in Figure 6.3.
(b) Would it help if we knew that the LR table was constructed for an LL grammar? Explain your reasoning.

Check back soon!
01:08

Problem 47

For each of the following, construct an appropriate grammar:
(a) The grammar is $\operatorname{SLR}(3)$ but not $\operatorname{SLR}(2)$.
(b) The grammar is $\operatorname{LALR}(2)$ but not LALR(1).
(c) The grammar is $\operatorname{LR}(2)$ but not $\operatorname{LR}(1)$.
(d) The grammar is LALR(1) and SLR(2) but not SLR(1).

James Kiss
James Kiss
Numerade Educator
02:23

Problem 48

Construct a single grammar that has all of the following properties:
$\bullet$ It is SLR(3) but not $\operatorname{SLR}(2)$.
$\bullet$ It is LALR(2) but not LALR(1).
$\bullet$ It is LR(1).

James Kiss
James Kiss
Numerade Educator

Problem 49

For every $k>1$ show that there exist grammars that are $\operatorname{SLR}(k+1)$, $\operatorname{LALR}(k+1)$, and $\operatorname{LR}(k+1)$ but not $\operatorname{SLR}(k), \operatorname{LALR}(k)$, or $\operatorname{LR}(k)$.

Check back soon!
02:23

Problem 50

Consider the grammar generated by $1 \leq i, j \leq n, i \neq j$ using the following template:
$$
\begin{aligned}
& \mathrm{S} \rightarrow \mathrm{X}_i \mathrm{z}_i \\
& \mathrm{X}_i \rightarrow \mathrm{y}_j \mathrm{X}_i \\
& \mid \mathrm{y}_j
\end{aligned}
$$
The resulting grammar has $O\left(n^2\right)$ productions.
(a) Show that the CFSM for this grammar has $O\left(2^n\right)$ states.
(b) Explain why the grammar is, or is not, SLR(1).

James Kiss
James Kiss
Numerade Educator
06:22

Problem 51

The bottom-up parsing techniques given in this chapter are more powerful than top-down techniques given in Chapter 5 .
Using the alphabet $\{a, b\}$, devise a language that is not $\operatorname{LL}(k)$ for any $k$ but is $\operatorname{LR}(k)$ for some $k$. What property of $\operatorname{LR}(k)$ parsing allows such a grammar to be constructed?

Trang Hoang
Trang Hoang
Numerade Educator