15) Augment the CFG given in the notes to add simple switch state- ments, descri
ID: 3749196 • Letter: 1
Question
15) Augment the CFG given in the notes to add simple switch state-
ments, described informally below. Also provide a short description of
any ambiguities or shortcomings of the informal description, and how you
chose to address them in your grammar (i.e., you are allowed to have your
grammar generate etter" switch statements than what are described).
You may assume the appropriate nonterminals and terminals have been
added, and that the production [stmt]->[switch] has been added to
the set of productions. So you need only give the productions relevant
to switch statements themselves; you do not need to copy out the whole
grammar.
A switch statement begins with the keyword switch, followed by a variable name, and then a list of cases delimited (surrounded)
by the keywords begin and end
.
A case begins the keyword case , followed by an integer constant,
followed by a colon and then a statement. A default case may be speci ed after all the other cases by using
the keyword default followed by a colon and then a statement.
You may use EBNF for this question
Explanation / Answer
Context Free Grammar for Switch statement
---------------------------------------------------
<start> ---à switch ( <expr> ) <stmt>
<stmt> ---à case <expr> : <stmt>
<stmt> = Statement
<expr> = Expression
Non-terminal symbols are <stmt> and <expr>
Terminal symbols are switch and case
The symbol <stmt> at the end of the first line is the production
<start> is the Start symbol
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.