Docsity

5 Problems on Programming Languages - Assignment 1 | ECS 140A, Assignments of Programming Languages

Toggle sidebar
bg1
Assignment 1
Due: Thursday, 9 April 2009 (in class)
This assignment asks you to prepare written answers to questions on BNF, EBNF, and Syntax Diagrams.
Each of the questions has a short answer—no essays, please! You may discuss this assignment with other
students and work on the problems together. However, your write-up should be your own individual work.
Remember that written assignments are to be turned in in class on the due date.
1. (15pts) Consider the following BNF grammar:
A ::= [B, A] | B
B ::= C | (A; C)
C ::= {C} | D
D::=a|b|c
Note that in this grammar, [, ], ’,’, (, ), ;, , , a, b, and c are terminals, whereas A, B, C, and D are
non-terminals. For each of the strings listed below, indicate all the non-terminals that can generate it:
(a) (5pts) [c, (b; a)]]
(b) (5pts) [[[a, b], c], a]
(c) (5pts) [(a; [b, c]), [(b; [a, c])]]
2. (20pts) Given the following BNF:
<integer> ::= <unsigned> | <sign> <unsigned>
<unsigned>::= <digits> | <unsigned><digits>
<digits> ::= <digits><digit> | <digit>
<digit> ::= 0 | 1 | ... | 9
<sign> ::= + | -
(a) (10pts) Convert the grammar into EBNF.
(b) (10pts) Show the syntax diagram of the above EBNF.
3. (25pts) What language is generated by each of the BNF grammars below (assuming that <s> is the start
symbol):
(a) (5pts)
<s> ::= 0 0 0 <s> 1 | empty
(b) (10pts)
<s> ::= <x> | <y> | empty
<x> ::= 0 <x> 1 | empty
<y> ::= 0 0 <y> 1 | empty
1
pf2

Partial preview of the text

Download 5 Problems on Programming Languages - Assignment 1 | ECS 140A and more Assignments Programming Languages in PDF only on Docsity!

Assignment 1

Due: Thursday, 9 April 2009 (in class)

This assignment asks you to prepare written answers to questions on BNF, EBNF, and Syntax Diagrams. Each of the questions has a short answer—no essays, please! You may discuss this assignment with other students and work on the problems together. However, your write-up should be your own individual work. Remember that written assignments are to be turned in in class on the due date.

  1. (15pts) Consider the following BNF grammar:

A ::= [B, A] | B

B ::= C | (A; C)

C ::= {C} | D

D ::= a | b | c

Note that in this grammar, [, ], ’,’, (, ), ;, , , a, b, and c are terminals, whereas A, B, C, and D are non-terminals. For each of the strings listed below, indicate all the non-terminals that can generate it:

(a) (5pts) [c, (b; a)]] (b) (5pts) [[[a, b], c], a] (c) (5pts) [(a; [b, c]), [(b; [a, c])]]

  1. (20pts) Given the following BNF:

::= | ::= | ::= | ::= 0 | 1 | ... | 9 ::= + | -

(a) (10pts) Convert the grammar into EBNF. (b) (10pts) Show the syntax diagram of the above EBNF.

  1. (25pts) What language is generated by each of the BNF grammars below (assuming that is the start symbol):

(a) (5pts)

::= 0 0 0 1 | empty

(b) (10pts)

::= | | empty ::= 0 1 | empty ::= 0 0 1 | empty

(c) (10pts)

::= | ::= 0 1 | ::= 0 | 0 ::= 0 1 1 | ::= 1 | 1

  1. (20pts) Given the following grammar:

::= if then | if then else | other ::= true | false

where other is a terminal that stands for any other kinds of statements.

(a) (10pts) This grammar is ambiguous. Give a string having two different parse trees and draw the parse trees. (b) (10pts) If we adopt the disambiguating rule (used in most languages) “match each else with the closest previous unmatched then,” write an equivalent, unambiguous grammar.

  1. (20pts) In a grammar, a grammar symbol X is useless if X can never appear in a derivation of a string.

(a) (15pts) Write an algorithm for eliminating all productions containing useless symbols from a gram- mar. (b) (5pts) Apply your algorithm to the following grammar:

::= 0 | | ::= ::= 1 ::= 2

where is the start symbol.