Problem Set
A=Answer
6. 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.
A=
S → AbB bAc A → Ab aBB B → Ac cBb c a.
A=
S → AbB bAc A → Ab aBB B → Ac cBb c a.
a. aAcccbbc
S -> AbB -> aBBbB -> aAcBbB -> aAccBbbB -> aAcccbbc
S -> AbB -> aBBbB -> aAcBbB -> aAccBbbB -> aAcccbbc
b. AbcaBccb
S -> AbB -> AbcBb -> AbcAcb -> AbcaBBcb -> AbcaBccb
S -> AbB -> AbcBb -> AbcAcb -> AbcaBBcb -> AbcaBccb
c. baBcBbbc
S -> bAc -> baBBc -> baBcBbc -> baBcBbbc
S -> bAc -> baBBc -> baBcBbc -> baBcBbbc
7. Show a complete parse,
including the parse stack contents, input string, and action for the string id
* (id + id), using the grammar and parse table in Section 4.5.3.
A=
A=
8. Show a complete parse,
including the parse stack contents, input string, and action for the string (id
+ id) * id, using the grammar and parse table in Section 4.5.3.
A=
A=
9. Write an EBNF rule that
describes the while statement of Java or C++. Write the recursive-descent
subprogram in Java or C++ for this rule.
A=
A=
<while_stmt> ->
WHILE ‘(‘ (<arith_expr> | <logic_expr>) ‘)’ <block>
<block> -> <stmt> | ‘{‘ <stmt> {<stmt>} ‘}’
10. Write an EBNF rule that
describes the for statement of Java or C++. Write the recursive-descent subprogram
in Java or C++ for this rule.
A=
A=
Assume the following
non-terminals are given: <type>, <id>, <literal>,
<assign>, <expr>, and <stmt_list>.
<for> -> for ‘(‘
[[<type>] <id> = <expr> {, [<type>] <id> =
<expr>}] ; [<expr>] ; [<expr> {, <expr>}] ‘)’ ‘{‘
<stmt_list> ‘}’
No comments:
Post a Comment