ECS421U Automata and Formal Languages Assignment

Assignment Help Theory of Computation
Reference no: EM132496642

ECS421U Automata and Formal Languages - Queen Mary University

Problem 1: CFGs, PDAs, parsing
(a) Give a context-free grammar G accepting the same language as the regular expression:
1(0 + 1)(0 + 1)
(b) Consider the following context-free grammar G over Σ = {0, 1, 2}, with start variable S:
S → 1Y | 2S | ε
Y → S1 | 0Y10 | 0
(i) Using the rules of G, construct parse trees for the words 2, 210 and 210010.
(ii) Explain why the grammar is not in Chomsky normal form (CNF). One reason is sufficient.
(iii) Using any of the techniques we have seen in the lectures, construct a CNF grammar accepting the same language as G.
Make sure you include in your answer all individual steps you followed.
(iv) Using the rules of your CNF grammar and the CYK algorithm, verify whether the following words are in the language of G: 121, 122.
(c) Consider the following context-free grammar G over Σ = {0, 1, 2, 3}, with start variable S:
S → 1S1 | 3S3 | Y Y → 20 | 20Y
(i) Give three example words in the language of the grammar G, and three words (from Σ∗) not in it. Make sure that your accepted words combined use all of the alphabet letters.
(ii) For each word of yours from part (i) that is in the language give an accepting derivation.
(iii) Describe in English the language accepted by G, or give a formal definition of it.
(iv) Give a PDA A recognising the same language as G.
(v) For two of the accepted words from part (i), give accepting runs of your PDA.

In the rest of this coursework you will work with a Java implementation of Context-Free Grammars. The implementation of CFGs will use the classes CFG and Rule, similar to the classes FSA and Transition that you worked with in the previous coursework:
public class CFG {
public String alphabet[]; public String vars[]; public Rule rules[];

public String startVar;

public CFG(String[] a, String[] v, Rule[] r, String sv) { alphabet=a; vars=v; rules=r; startVar=sv;
}

// ... plus public and private methods
}
So, objects of the class CFG can be seen as tuples containing four elements. If G is an instance of the class CFG, representing the CFG G = (Σ, V, R, S), we have:
• G.alphabet is an array of strings representing the elements of the alphabet Σ. For example, if Σ = {a, b} then G.alphabet should be the array {"a", "b"}.
Note that the array {"a", "b"} is created by the command: new String[]{"a", "b"}.
• G.vars is an array of strings representing the variables of G, i.e. the elements of V . E.g. if
V = {S, Y } then G.vars should be the array {"S", "V"}.
• G.startVar is a string corresponding to the starting variable. In our example, the starting variable is S, so G.startVar should be "S".
• G.rules is the set of grammar rules, represented as an array. Grammar rules have their own
Rule class:
public class Rule { public String var; public String rhs[];

public Rule(String v, String[] r) { var=v; rhs=r;
}
}
So, a Rule instance r simply contains two fields:
- r.var is the variable on the LHS of the rule
- r.rhs is the RHS of the rule, which is an array of strings (i.e variables and letters).
E.g. to create a new rule S → 1S1 we use: new Rule("S",new String[]{"1","S","1"}). Returning to G.rules, we can construct e.g. R = {S → ε, S → 0XX, X → 10} using:
new Rule[]{
new Rule{"S", new String[]{}},
new Rule{"S", new String[]{"0","X","X"}},
new Rule{"X", new String[]{"1","0"}} };
Note that when the rule RHS is just ε, it is represented as an empty array (but not null!).

String[] alphabet = new String[]{ "0", "1", "2", "3" }; String[] vars = new String[]{ "S", "Y" };
Rule[] R = new Rule[] {
new Rule("S", new String[]{"1","S","1"}),
new Rule("S", new String[]{"3","S","3"}),
new Rule("S", new String[]{"Y"}),
new Rule("Y", new String[]{"2","0"})
new Rule("Y", new String[]{"2","0","Y"})
};
new CFG(alphabet,vars,R,"S");

Figure 1: An implementation of the CFG of Problem 1(c).

Problem 2: Java Implementation

Use the provided files Question2.java, CFG.java and Rule.java for the following exercises.
• You should write your code in Question2.java, by altering only the TODO parts of the file. In particular, keep the body of main intact.
• You should not change CFG.java and Rule.java.
In Question2.java there is some DEMO code to help you with what you need to do in this Question!
(a) Write down code implementing each of the CFGs of parts (i), (ii) and (iii) below, in the manner we explained above (i.e. as was done in Figure 1). Include the code in the methods:
public static CFG generateCFG1() public static CFG generateCFG2() public static CFG generateCFG3()
of Question2.java respectively. In your report, include the code you wrote for each of these methods.
(i) The initial CFG of Problem 1(b).
(ii) Taking Σ = { ( , ) } (i.e. the alphabet contains left brackets and right brackets):
S → ε | (S) | SS
(iii) Taking Σ = {0, 1, 2, 3}, and start variable S:
S → 3X | 1X
X → 3S | 1Y | ε
Y → 0X | 3X | ε
(b) Implement in Question2.java the method:
public static void runThem()

which generates the CFG of Problem 1(b) (which is already implemented in Question2.java) and three CFGs that you implemented in part (a) and runs them, one by one, on the inputs specified below, using the method isAccepted (the method is explained at the end of this assignment). Your code should test the CFGs on the following inputs and print the results of the runs, as follows:
G0 accepts: 2020 [...], 120201 [...], 2021 [...]
G1 accepts: epsilon [...], 210 [...], 210010 [...] G2 accepts: (()) [...], ()(()) [...], ((())))( [...] G3 accepts: 11011 [...], 11010 [...], 11001 [...]
where the dots should contain yes or no, depending on whether the word is accepted by the corresponding CFG or not. So, for example, for G2 the words to test are (()), ()(()) and ((())))(. Also, the first word to check for G1 is ε (the empty word). In your report, include:
• The code of your implementation of runThem. • A snapshot of the printout of runThem.
(c) We saw that a CFG is in Chomsky-Normal Form (CNF) if its rules satisfy some specified criteria. Implement in Question2.java a method:
public static Boolean isInCNF(CFG G)
which takes a CFG object G as input and returns true if G represents a CFG that is in CNF, and false otherwise. In your report, include:
• The code of your implementation of isInCNF, along with a textual explanation of what it is doing. By explanation, we don't mean a line-by-line readout of the code but, rather, an explanation of the idea of your algorithm and the steps followed to implement it.
• A snapshot of the printout of testIsInCNF.
Hint: One solution is to use a for-loop to go over all rules of the grammar and check if they (all) satisfy the CNF criteria. You can also use the following methods from the class CFG:
public boolean checkVar(String v)
public boolean checkLetter(String s)
which check if the input string is a valid variable or alphabet letter respectively for the given CFG and return true or false accordingly. For example, if G is the CFG instance constructed in Figure 1 then:
• G.checkVar("S") returns true, while G.checkVar("W") and G.checkVar("2") return
false
• G.checkLetter("2") returns true, while G.checkLetter("S") and G.checkLetter("12") return false.

Attachment:- Automata and Formal Languages.rar

Reference no: EM132496642

Questions Cloud

What is true about government should be true about business : To what extent do you agree with Collins' argument? If you agree, can you offer more support to his argument (perhaps from personal experience)?
What shapes external competitiveness from pay mix standpoint : Discuss what shapes external competitiveness from the pay mix standpoint. Your response must be at least 75 words in length.
Know about photosynthesis and cellular respiration : Predict what will happen in the following experiment based on what you know about photosynthesis and cellular respiration.
What internet business model would be appropriate : What Internet business model would be appropriate for the company to follow in creating a Web site and why? In what ways can the business benefit from a Web.
ECS421U Automata and Formal Languages Assignment : ECS421U Automata and Formal Languages Assignment help and solution, Queen Mary University - assessment writing service
What is the most fulfilling thing about the marketing : How do you feel about managing and motivating your marketing team? Is there a specific method? What was the worst experience in your career?
Represent a classical example of codominant alleles : Coat colors of the shorthorn breed of cattle represent a classical example of codominant alleles. Red is governed by the genotype
Where do you think that this evaluation should begin : Where do you think that this evaluation should begin? In your reply, explain your reasoning. Explain the different levels of disciplinary actions that will be.
What is idea generating : What is idea generating. how would you explain idea generation? How does it help us in our new product development process

Reviews

Write a Review

Theory of Computation Questions & Answers

  How many memory cells does a i-gigabyte memory contain

How many memory cells does a 4-Kbyte memory contain? How many memory cells does a I-gigabyte memory contain?

  Write your proof in the format and style adopted

Write your proof in the format and style adopted in the class, with notes/comments to clarify the steps of the proof and the TM's used or created in the proof.

  Succession planning is an important od intervention and

succession planning is an important od intervention and business sector succession planning-sometimes called workforce

  Identify the variables using a truth table

Translate the following sentences into variable form and identify the variables.use a truth table to determine if they are (a) tautological.

  Design grammars for the set of all strings

Design grammars for the set of all strings of 0s and 1s such that every 0 is immediately followed by at least one 1.

  Topicthe enhancement of communication process using a

topicthe enhancement of communication process using a particular computer device or software application by the

  Show that if the statement is true

Show that if the statement P(n) is true for infinitely many positive integers, and the implication P(n+1) ---> P(n) is true for all n>=1, then P(n) is true for all positive integers.

  Prove or disprove the following proposed inference rules

A proof should be made by using the reflexive, augmentation, transitive, decomposition, union, and pseudotransitive rules.

  Characterize the subgame perfect nash equilibria

Characterize the Subgame Perfect Nash Equilibria of this game. Discuss the underlying assumptions made in the analysis.

  Use algorithm np completeness of any of the problems

Use any algorithm we without writing out details of algorithm. In proving problem NP-complete, you may utilize NP completeness of any of the problems.

  How the growth of certain types of plants can be modeled

Describe how the growth of certain types of plants can be modeled using a Lidenmeyer system. Such a system uses a grammar with productions.

  Construct a deterministic finite-state automaton

Construct a deterministic finite-state automaton that is equivalent to the nondeterministic automaton with the state diagram shown here.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd