Vous êtes sur la page 1sur 3

ECS140A-W14-03

January 7, 2014

Due:

ASSIGNMENT 1: BNF
January 14, 2014 (in lecture, at start)

Turn in only hardcopy. On each page of what you turn in,


if you worked individually, give (in this order) your
last name, first name.
if you worked in a group, give (in this order) each group members
last name, first name; list group members alphabetically by last name.
Consider the following grammar, which describes what constitutes <room>.
<room>

::= <table> | <chairs> <table> | <table> couch | <table> <stuff>

<table>

::= oak | teak | <set>

<set>

::= <table> <chairs> | dinette

<chairs> ::= <chairs> <chair> | <chair>


<chair>

::= armchair | bench | stool

<stuff>

::= rug | plant

ECS140A-W14-03

January 7, 2014

string

<chairs>

<set>

<table>

<room>

oak
armchair bench oak
dinette plant
bench stool
oak rug
oak teak dinette
teak teak
teak bench oak oak stool
dinette plant
rug plant
couch
armchair bench
teak lamp
bench stool teak bench stool
bench stool teak bench teak stool
teak bench plant

no
no
no
yes

no
no
no
no

yes
no
no
no

yes
yes
yes
no

ECS140A-W14-03

January 7, 2014

(1)

Indicate in the table on the previous page if the string belongs to the class of object at the
head of the column by placing yes or no in the appropriate box. The first few rows of
the table are already filled in to help get you started. Note that the strings contain spaces
to aid in readability, although technically the given grammar does not allow spaces.

(2)

Rewrite the original grammar in EBNF notation (as given in the textbook). Specifically,
simplify the grammar by using the repetitive and optional meta-symbols.

(3)

Rewrite the above grammar using syntax graphs (aka syntax diagrams). (You can base
your answer on the BNF or EBNF forms of the grammar.)

(4)

Consider all strings that can be derived from <table>. Can each of those also be derived
from <room>? Explain.

(5)

Suppose the production <table> <chairs> were added as an alternative in the rule for
<room>. Would this change allow any additional strings to be considered a <room>? If
yes, give one such new string. If no, explain.

(6)

Modify the original grammar so that in strings produced by <chairs>, any stools precede
any armchairs or benches. Show only the changed rule(s) and any new rule(s), not the
entire grammar.
Examples: in the new grammar the following strings

(7)

are <chairs>

are not <chairs>

stool
stool bench
armchair bench
stool bench bench
stool stool armchair bench armchair
bench bench

bench stool
stool bench stool
armchair bench stool
armchair stool bench

The original grammar does not give a unique parse tree for every input string. Give a
string where the grammar is ambiguous and give two different parse trees for that string.
Draw the parse trees in the style given in Louden (page 90, 2nd ediion; page 214, 3rd edition; page 71, 1st edition). What problems would ambiguity cause in a real programming
language?

Vous aimerez peut-être aussi