cd ~/cs46/ cp -r examples/homework/wk5 homework/ cp -r examples/labs/wk5 labs/ git status git add homework/wk5 labs/wk5 git commit -m "week5 start" git pushYou may work with a partner and submit a common solution on this homework.
Our algorithm for parsing strings computes the sets $V_{i,j}$ for $1 \leq i \leq n, j \geq i$. The algorithm proceeds in a series of rounds, where in each round, it considers substrings of length $s, 1 \leq s \leq n$. In the first round, the algorithm sets $V_{i,i}$ to $\{ A \in V, A \rightarrow w_i$ for some rule $A$ in the grammar $\}$.
For substrings longer than $1$, the algorithm checks if the substring $x=x_i \cdots x_{i+s-1}$ can be broken into two smaller pieces at some index $k$, $y=x_i \cdots x_k$ and $z=x_{k+1} \cdots x_{i+s-1}$ such that there is a rule $A \rightarrow BC$ in the grammar where $B \in V_{i,k}$ and $C \in V_{k+1,i+s-1}$. If such a rule $A$ exists, we add $A$ to $V_{i,i+s-1}$.
After finishing all rounds, we just need to check if $S \in V_{i,n}$. A pseudocode summary is below
for i in 1 to n: Add A to V[i,i] if A yields w[i] is a rule for s in 1 to n-1: for i in 1 to n-s: for k in i to i+s-1: if A yields BC is a rule where B is in V[i,k] and C is in V[k+1,i+s] add A to V[i,i+s] return True if S is in V[1,n]Some example output for a CNF grammar for $a^nb^n$.
$ cat grammar1.txt a b S: E S: A1 B S: A B A1: A S A: a B: b $ python cnf.py grammar1.txt string1.txt aabb: True abab: False a: False aaaabbbb: True ab: TrueFor the balanced parens
$ cat grammar2.txt ( ) S: E S: A1 B S: A B S: S S A1: A S A: ( B: ) $ python cnf.py grammar2.txt string2.txt (): True E: True ()(): True (()()): True )(: False (((()): False
The grammar format starts with a line containing the symbols of the alphabet separated by spaces, a blank line, and then the grammar rules. The first rule lists the start symbol on the left hand side. The rule format is V: A B to indicate the rule $V \rightarrow AB$. Note A1 is a single variable not two variables. Variables on the right hand side are separated by spaces.
Remember to run git add, git commit, and git push to submit your assignments. If you are working with a partner, both you and your partner need to hand in the lab, but I will grade the most recent push. Include both names at the top of your homework.