rewrite the following bnf grammar using the form of ebnf.make the resulting gram
ID: 3756536 • Letter: R
Question
rewrite the following bnf grammar using the form of ebnf.make the resulting grammar as short as possible (fewest rules). note: it is possible to replace all eight bnf rules with a single ebnf rule.
<do-statement> --> do<code-block>
|do<code-block> <catch-clauses>
<catch-clauses> --> <catch-clauses>
| <catch-clauses > <catch-clauses>
<catch-clauses> --> catch<code-block>
| catch <pattern > <code-block>
| catch <where-clause > <code-block>
| catch <pattern> <where-clauses><code-block>
Explanation / Answer
Converting bnf to ebnf.
Handling Common prefix:
<do-statement>--> do<code-block> | do<code-block><catch-clauses>, here "do<code-block>" is common in both the two rules in bnf above, so by changing into ebnf it becomes as below, where "<catch-clauses>" is an optional field hence enclosed within "[]"
<do-statement> ---> do<code-block>[<catch-clauses>] ----Rule 1 in ebnf
Handling left recursion: In Rules 3 and 4 in bnf
<catch-clauses> --> <catch-clauses> | <catch-clauses><catch-clauses>
In ebnf it becomes as
<catch-clauses> --> <catch-clauses>{<catch-clauses>} ----- Rule 2 in ebnf, which means 1 or more <catch-clauses>
By replacing the <catch-clauses> field in Rule 1 in ebnf by Rule 2 in ebnf, Rule 1 in ebnf becomes as:
<do-statement> ---> do<code-block>[<catch-clauses>{<catch-clauses>} ] ---- New Rule 1 in ebnf
The rules 5,6,7 and 8 in bnf
<catch-clauses> --> catch<code-block>
|catch<pattern><code-block>
|catch<where-clause><code-block>
|catch<pattern><where-clauses><code-block>
By handling common factors the above 4 rules become as:
<catch-clauses> --> catch[<pattern>][<where-clauses>]<code-block> ---Rule 3 in ebnf
By replacing <catch-clauses> in new Rule 1 in ebnf by using Rule 3 in ebnf, the new Rule 1 in ebnf is:
<do-statement> ---> do<code-block>[catch[<pattern>][<where-clauses>]<code-block>{catch[<pattern>][<where-clauses>]<code-block>} ]
The above rule is the ebnf form of given 8 rules in bnf
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.