How to determine if my NFA is correct? - finite-automata

The obvious choice is to exhaust all possible inputs. I think I did. But I am not very sure whether it's valid and I didn't break any rules of non-deterministic finite automata.
My NFA is given by: (ab u aab u aba)* and below is my diagram.
Have I missed something?

You aren't missing anything, but the NFA could be simplified considerably by collapsing paths and eliminating λ-rules. In order to determine whether your NFA decides the language denoted by the regular expression, you can argue informally by chasing states in the transition graph. In order to talk about the NFA, I'll use the following definition of the δ function with final states {q0,q3,q4} and initial state q0
δ(q0,a) = {q1,q2}
δ(q1,a) = {q2}
δ(q2,b) = {q3}
δ(q3,a) = {q4}
δ(q3,λ) = {q0}
δ(q4,λ) = {q-}
The goal is to show that the NFA accepts precisely the language (ab U aab U aba)*. To this end, we can consider the strings accepted by beginning with λ at q0 and exhausting all possible transitions through the graph, recording the strings built by concatenating the symbols transitioned on, and noting whether such a string is accepted or not. Paths in the graph indicate concatenation; final states indicate acceptance or disjunction; loops indicate Kleene star.
From q0 and λ we can transition on a to q1 or q2. On q1 and a we can transition to q2. Hence, on q2 we have either a or aa and nothing else. From q2 and a or aa we can transition to q3 on b. Hence, at q3 we have either ab or aab and nothing else. From q3 and ab or aab we can transition to q0 on λ or q4 on a. Hence, on q3 and ab or aab we have either ab or aab or aba and nothing else. Finally, on q4 and aba we can transition to q0 on λ. Since we have ab or aab or aba and transitioned to the start state, meaning our derivation can repeat itself zero or more times, and we have exhausted the possible transitions, we conclude that the NFA decides the Kleene closure of ab or aab or aba.
There are more formal methods of showing that a given NFA decides a language. But the easiest way is to exhaust all possible paths through the NFA, introducing Kleene closures on cycles. An example of a formal method would be to convert the NFA to a regular expression, then derive the equality of the obtained expression and the target expression axiomatically. This is largely unnecessary.
You probably have done precisely what I just wrote, however, making this entire post unnecessary. If not, I hope it shows the kind of informal reasoning you can use to convince yourself that an NFA decides a language.

Related

Automata theory : Conversion of a Context free grammar to a DFA

How to convert a Context Free Grammar to a DFA? This is easy if we have transitions like
A->a B. But when we have the transitions as A->a B c. Then how should we represent it as a DFA
There is no general procedure to convert an arbitrary CFG into a DFA. For example, consider this CFG:
S → aSb | ε
This grammar is for the language { anbn | n ≥ 0 }, which is a canonical nonregular language. Since we can only build DFAs for regular languages, there’s no way to build a DFA with the same language as this CFG
First, you should convert your language to CNF (Chomskey Normal Form).
Then steps for conversion are as such:
Convert it to left/right grammar is called a regular grammar.
Convert the Regular Grammar into Finite Automata
The transitions for automata are obtained as follows
For every production A -> aB make δ(A, a) = B that is make an are labeled ‘a’ from A to B.
For every production A -> a make δ(A, a) = final state.
For every production A -> ϵ, make δ(A, ϵ) = A and A will be final state.
No. For these Grammar no DFA can form.
why?
because it requires memory. Memory of occurence of a.
Yes . it is CFL (context free Language).
We can design a PDA (Push down automata). Here , memory ( STACK is
use ). for PUSH a and POP b

Why can't a LL grammar be left-recursive?

In the dragon book, LL grammar is defined as follows:
A grammar is LL if and only if for any production A -> a|b, the following two conditions apply.
FIRST(a) and FIRST(b) are disjoint. This implies that they cannot both derive EMPTY
If b can derive EMPTY, then a cannot derive any string that begins with FOLLOW(A), that is FIRST(a) and FOLLOW(A) must be disjoint.
And I know that LL grammar can't be left recursive, but what is the formal reason? I guess left-recursive grammar will contradict rule 2, right? e.g., I've written following grammar:
S->SA|empty
A->a
Because FIRST(SA) = {a, empty} and FOLLOW(S) ={$, a}, then FIRST(SA) and FOLLOW(S) are not disjoint, so this grammar is not LL. But I don't know if it is the left-recursion make FIRST(SA) and FOLLOW(S) not disjoint, or there is some other reason? Put it in another way, is it true that every left-recursive grammar will have a production that will violate condition 2 of LL grammar?
OK, I figure it out, if a grammar contains left-recursive production, like:
S->SA
Then somehow it must contain another production to "finish" the recursion,say:
S->B
And since FIRST(B) is a subset of FIRST(SA), so they are joint, this violates condition 1, there must be conflict when filling parse table entries corresponding to terminals both in FIRST(B) and FIRST(SA). To summarize, left-recursion grammar could cause FIRST set of two or more productions to have common terminals, thus violating condition 1.
Consider your grammar:
S->SA|empty
A->a
This is a shorthand for the three rules:
S -> SA
S -> empty
A -> a
Now consider the string aaa. How was it produced? You can only read one character at a time if you have no lookahead, so you start off like this (you have S as start symbol):
S -> SA
S -> empty
A -> a
Fine, you have produced the first a. But now you cannot apply any more rules because there is no more non-terminals. You are stuck!
What you should have done was this:
S -> SA
S -> SA
S -> SA
S -> empty
A -> a
A -> a
A -> a
But you don't know this without reading the entire string. You would need an infinite amount of lookahead.
In a general sense, yes, every left-recursive grammar can have ambiguous strings without infinite lookahead. Look at the example again: There are two different rules for S. Which one should we use?
An LL(k) grammar is one that allows the construction of a deterministic, descent parser with only k symbols of lookahead. The problem with left recursion is that it makes it impossible to determine which rule to apply until the complete input string is examined, which makes the required k potentially infinite.
Using your example, choose a k, and give the parser an input sequence of length n >= k:
aaaaaaa...
A parser cannot decide if it should apply S->SA or S->empty by looking at the k symbols ahead because the decision would depend on how many times S->SA has been chosen before, and that is information the parser does not have.
The parser would have to choose S->SA exactly n times and S->empty once, and it's impossible to decide which is right by looking at the first k symbols in the input stream.
To know, a parser would have to both examine the complete input sequence, and keep count of how many times S->SA has been chosen, but such a parser would fall outside of the definition of LL(k).
Note that unlimited lookahead is not a solution because a parser runs on limited resources, so there will always be a finite input sequence of a length large enough to make the parser crash before producing any output.
In the book "The Theory of Parsing", Volume 2, by Aho and Ullman, page 681 you can find Lemma 8.3 that states: "No LL(k) grammar is left-recursive".
The proof says:
Suppose that G = (N, T, P, S) has a left-recursive nonterminal A. Then there is a derivation A -> Aw. If w -> e then it is easy to show that G is ambiguous and hence cannot be LL. Thus, assume that w -> v for some v in T+ (a non empty string of terminals). We can further assume that A -> u, being u some string of terminals and that there exists a derivation
Hence, there is another derivation:

How to solve δ(A,01) for this DFA?

Consider the DFA :
What will be δ(A,01) equal to ?
options:
A) {D}
B) {C,D}
C) {B,C,D}
D) {A,B,C,D}
The correct answer is option B) but I don't get how. Please some one explain me the steps to solve it and also in general how do we solve sit for any DFA and any transition?
Thanks.
B) Option is not Correct answer! for this transition graph.
In Transition Graph(TG) symbol ε means NULL-move (ε-move). There is two NULL-moves in TG.
One: (A) --- `ε` ---->(B)
Second: (A) --- `ε` ---->(C)
A ε-move means without consume any symbol you can change state. In your diagram from A to B, or A to C.
What will be δ(A,01) equal to ?
Question asked "what is path from state A if input is 01". (as I understand because there is only one final state)
01 can be processed in either of two ways.
(A) -- ε --->(B) -- 0 --> (B) -- 1 --> (D)
(A) -- ε --->(C) -- 0 --> (B) -- 1 --> (D)
Also, there is no other way to process string 01 even if you don't wants to reach final state.
[ANSWER]
So there is misprinting in question (or either you did).
You can learn how to remove NULL-move from transition graph. Also HOW TO WRITE REGULAR EXPRESSION FOR A DFA
If you remove null-moves from TG you will get three ways to accept 01.
EQUIVALENT TRANSITION GRAPH WITHOUT NULL-MOVE
Note there is three start-states in Graph.
Three ways:
{ABD}
{CBD}
{BBD}
In all option state-(B) has to be come.
Also, you have written Consider the DFA : is wrong. The TG is not deterministic because there is there is non-deterministic move δ(A,ε) and next states are either B or C.

NFA to DFA Algorithm

I've read a text file of symbols, states and transitions and put it all in a table. It looks like this:
symbols a, b
states q1, q2, q3, q4
start state q4
final state q2, q3
transition state:
q4, epsilon, q1
q1, a, q2
q3, a, q3
q3, b, q1
I've read an algorithm on how to convert NFA to DFA but I don't really understand the algorithm. How would I create transition methods and what should I have state class?
i have a nifty link right here:
JFLAP
JFLAP is an Java JAR which has some nice visualization included. You can test and transform NFAs/DFAs, do Pummping Lemma Stuff and check various grammars and such...
You might give it a try!
Do you have a programming problem, or an problem with Automata?
Because programming this it seems very simple. Yes, you'd have a state class, with 4 possible values q1-a4. The state class has a single constructor initializing it to q4. It has an Accept(symbol) function that modifies the state object, and possibly an IsEndState() function that returns true for states q2 and q3.
The NFA/DFA part comes into play in the implementation of the Accept() method. Now it coukd be that you are asked for a programmtic solution to convert an NFA-based implementation of Accept() to a DFA-based implementation of Accept().
These two links should help you:
http://www.win.tue.nl/prose/pres/ploeger-19-10-06.pdf
http://www.cs.sun.ac.za/rw711/documentation/hopcroft2.pdf
If you want to do it automatically without having to download something like JFLAP, give the Online NFA to DFA conversion tool a spin.
ProfBrown's "Convert NFA to DFA" video on YouTube examines how to do it in some rigor, though IMO it's impractical and unnecessarily tedious to have to write the tables up by hand. As you get used to doing the process, you'll be able to eyeball it for small graphs. In the case of large graphs, you'd use a tool to automate it, anyway.

State based testing(state charts) & transition sequences

I am really stuck with some state based testing concepts...
I am trying to calculate some checking sequences that will cover all transitions from each state and i have the answers but i dont understand them:
alt text http://www.gam3r.co.uk/1m.jpg
Now the answers i have are :
alt text http://www.gam3r.co.uk/2m.jpg
I dont understand it at all. For example say we want to check transition a/x from s1, wouldnt we do ab only? As we are already in s1, we do a/x to test the transition to s2, then b to check we are in the previous right state(s1)? I cant understand why it is aba or even bb for s1...
Can anyone talk me through it?
Thanks
There are 2 events available in each of 4 states, giving 8 transitions, which the author has decided to test in 8 separate test sequences. Each sequence (except the S1 sequences - apparently the machine's initial state is S1) needs to drive the machine to the target state and then perform either event a or event b.
The sequences he has chosen are sufficient, in that each transition is covered. However, they are not unique, and - as you have observed - not minimal.
A more obvious choice would be:
a b
ab aa
aaa aab
ba bb
I don't understand the author's purpose in adding superfluous transitions at the end of each sequence. The system is a Mealy machine - the behaviour of the machine is uniquely determined by the current state and event. There is no memory of the path leading to the current state; therefore the author's extra transitions give no additional coverage and serve only to confuse.
You are also correct that you could cover the all transitions with a shorter set of paths through the graph. However, I would be disinclined to do that. Clarity is more important than optimization for test code.