Copy of Project2.pdf

Design and Analysis of Algorithms, Second Programming Assignment, 03/06/06
Deadline 11/06/15
Definitions
A context-free grammar G is a quadruple G = (Vt, Vn, P, S) where:
•
•
•
•
Vt is a finite set of terminals.
Vn is a finite set of non-terminals.
P is a finite set of production rules.
S is an element of Vn, and is distinguished as the starting symbol.
•
Elements of P are of the form Vn → (Vt ∪ Vn)*.
The Language of such a grammar is defined as:
L(G) = {w ∈ Vt* | S ⇒* w}
Where ⇒* is the transitive closure of → for clauses. (For more information on this
consult your favourite text on theory of formal languages.) By a string in this
grammar we mean a member of (Vt ∪ Vn)*.
Project Specification
In this project, your programme will be fed with a grammar G and a string w, and
should be able to answer whether w ∈ G or not.
The input of this project will be a file with the following format:
1.
2.
3.
4.
5.
The first line contains w.
The second line will contain elements of Vt separated by a space.
The third line will contain the same for Vn in the same format above.
The fourth line will contain the starting symbol. And,
the rest of lines, until the end of the file, will each contain one-and-only-one
production rule. The format of each rule (line) will be the left-hand-side of the
rule, a space, followed by the right-hand-side of it, in which all symbols will
be separated by a space. In the set of production rules, the non-terminal
symbols will be surrounded by broken braces (“<” and “>”).
The output file should be a file, first line of which is either “Yes” or “No” depending
on whether the inputted string is a member of the inputted grammar or not. If the
answer is “Yes”, then the next lines should contain one derivation of that string in this
language. The format of this derivation is not important as far as it is humanreadable!
Design and Analysis of Algorithms, Second Programming Assignment, 03/06/06
Deadline 11/06/15
Commentary
First of all, your solution should not be worse than Θ(n3), where n = ||w|| (length of w).
In retrospect, your hand-in package should have a document justifying your solution
and its complexity.
Secondly, your programme could be written in either of C/C++ or Java. In either case,
you should send a zipped file to [email protected] containing the source code of
your programme, as well as the executable of it.
If your code is in C/C++, no non-standard beast should be lurking in your code! This
means that your code should be compiled in any standard-conforming compiler, or
you will lose a mark. The students who programme in Java should patch two makefiles as well; one for compilation, and another for execution. In the case of corruption
of either, you will loose a certain mark. These make-files could be for either of
Windows XP or Linux. In WinXP batch files do, and in Linux bash scripts.
In either case of C/C++ or Java, the executable should be tuned so that it can be run in
command line like this:
CFGMembership in.txt out.txt
Where CFGMembership is the name a sample executable, in.txt and out.txt are
some arbitrary names for input and output files, respectively. Note that your
programme should not be sensitive to any name for the I/O files.
N.B. All the comments on the second homework (available at the assignments
section of the coursepage) apply to here as well.
Sample Input
Suppose your file is being tested for determining whether w = aabbb is in the
language generated by the grammar G({a, b}, {S, A, B}, P, S), where P is
S → AB
A → BB | a
B → AB | b
The input file will be like this:
aabbb
a b
S A B
S
<S> <A> <B>
<A> <B> <B>
<A> a
<B> <A> <B>
<B> b