Practice Problems in Computer Science: Parsing Concepts - Prof. Chao Wen Tseng, Exams of Computer Science

Practice problems related to parsing techniques, including bottom-up parsing, lr parsing, lalr parsing, and various grammar examples. Topics covered include the difference between bottom-up and top-down parsing, handle definition, right-most derivations, lr(k) items, shift/reduce conflicts, and lalr(1) parsing.

Typology: Exams

Pre 2010

Uploaded on 07/30/2009

koofers-user-nvp-2
koofers-user-nvp-2 🇺🇸

9 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 430, Practice Problems 2
1. Bottom up parsing
a. Describe the difference between bottom-up and top-down parsing with respect
to a grammar production A β
b. Define handle
c. Explain why bottom-up parsers result in right-most derivations in reverse.
d. Given the following ACTION/GOTO table, perform a parse of “a”, assuming
0 is the start state. Show the contents of the stack, remaining input, and action
performed at each step of the shift-reduce parse.
2. LR parsing
a. What is the canonical set of LR(k) items for a grammar?
b. What are ACTION/GOTO tables of an LR(k) parser for a grammar?
c. Given an LR(k) item [A αγ,
δ] from a production A β, describe the
role of A, α, γ, δ
d. Given an LR(k) item [A αγ,
δ], what lookahead allows a shift to be
performed (ignoring any other LR(k) items added to the state by closure)?
e. Given an LR(k) item [A αγ,
δ], what LR(k) items must be added to the
closure?
f. If the parser is in a state containing an LR(k) item [A αγ,
δ], what must be
at the top of the stack, and why?
g. Describe what constitutes a shift/reduce conflict
h. Give an example of two LR(1) items causing a reduce/reduce conflict.
i. Explain why there are no shift/shift conflicts
3. LALR parsing.
Consider the following sets of LR(1) items in the states of a LR(1) parser.
pf2

Partial preview of the text

Download Practice Problems in Computer Science: Parsing Concepts - Prof. Chao Wen Tseng and more Exams Computer Science in PDF only on Docsity!

CMSC 430, Practice Problems 2

  1. Bottom up parsing a. Describe the difference between bottom-up and top-down parsing with respect to a grammar production A → β b. Define handle c. Explain why bottom-up parsers result in right-most derivations in reverse. d. Given the following ACTION/GOTO table, perform a parse of “a”, assuming 0 is the start state. Show the contents of the stack, remaining input, and action performed at each step of the shift-reduce parse.
  2. LR parsing a. What is the canonical set of LR(k) items for a grammar? b. What are ACTION/GOTO tables of an LR(k) parser for a grammar? c. Given an LR(k) item [ A → α•γ, δ] from a production A → β, describe the role of A, α, γ, δ d. Given an LR(k) item [ A → α•γ, δ], what lookahead allows a shift to be performed (ignoring any other LR(k) items added to the state by closure)? e. Given an LR(k) item [ A → α•γ, δ], what LR(k) items must be added to the closure? f. If the parser is in a state containing an LR(k) item [ A → α•γ, δ], what must be at the top of the stack, and why? g. Describe what constitutes a shift/reduce conflict h. Give an example of two LR(1) items causing a reduce/reduce conflict. i. Explain why there are no shift/shift conflicts
  3. LALR parsing. Consider the following sets of LR(1) items in the states of a LR(1) parser.

a. Find all shift/reduce and reduce/reduce conflicts. List the LR(1) items and lookaheads causing conflicts. b. What states would be merged in a LALR(1) parser? c. Are any new reduce/reduce conflicts introduced in the LALR(1) parser? d. Explain why LALR(1) parsers will not introduce new shift/reduce conflicts. e. What is the relationship between LALR(1) and SLR(1) parsers?

  1. Consider the following grammar: S → abS | acS | c a. Compute FOLLOW(S) b. Compute the canonical set of LR(0) items for the grammar c. Build the ACTION/GOTO table using LR(0) + FOLLOW d. Explain why the grammar is or is not SLR(1) e. Use the ACTION/GOTO table to parse the string “abc”
  2. Consider the following grammar: S → Sa | Sc | c a. Compute the canonical set of LR(1) items for the grammar b. Build the ACTION/GOTO table for the grammar c. Explain why the grammar is or is not LR(1) d. Use the ACTION/GOTO table to parse the string “caca”
  3. Consider the following grammar: S → S + S | S * S | (S) | num a. Compute the canonical set of LR(1) items for the grammar b. Build the ACTION/GOTO table for the grammar c. Explain why the grammar is or is not LR(1) d. Explain how to use the ACTION/GOTO table so that i. * has higher precedence than + and*, + are both left associative ii. * has higher precedence than +, * is left associative, and + is right associative iii. + has higher precedence than * and *, + are both right associative
  4. Consider the following grammar: S → AS | b A → SA | a a. Compute the canonical set of LR(0) items for the grammar b. Build the ACTION/GOTO table for the grammar c. Compute the canonical set of LR(1) items for the grammar d. Build the ACTION/GOTO table for the grammar
  5. Consider the following grammar: S → S ; a | a a. Is the grammar SLR(1)? Is it LR(1)?
  6. Consider the following grammar: S → aAd | bBd | aBe | bAe A → c B → c a. Is the grammar LR(1)? Is it LALR(1)?
  7. Consider the following grammar: S → Aa | bAc | Bc | bBa A → d B → d a. Is the grammar LR(1)? Is it LALR(1)?