#Deterministic finite automaton -DFA

1 messages · Page 1 of 1 (latest)

neat radish
#

i'm trying to build a simple program in java to check whether a real number is correctly written. following the schema that i displayed. now, the following code checks whether the state that i find myself into(from the beginning it should be q0 with a symbol) is in my list of transitions with that specific symbol.

Tranzitie findTransition(String state, char symbol){
        for (int i=0; i<this.list.size(); i++){
            Tranzitie tmp = this.list.get(i);
            if (tmp.getSt_first().equals(state) && tmp.getSimbol()==simbol)
                return tmp;
        }
        return null;
    }

the following boolean method returns whether the string parameter "sir" is accepted by my dfa. (the string is supposed to be a real number)

boolean analyzeWord(String sir){
        Tranzitie tmp = null;
        for (int i = 0; i < sir.length(); i++) {
            if (i == 0) {
                tmp = lista.findTransition(st_initial, sir.charAt(i));
                System.out.println(tmp.getSt_first() + "\t" + tmp.getSimbol() + "\t" +tmp.getSt_end());
                if (tmp == null)
                    return false;
            } else {
                tmp = lista.findTransition(tmp.getSt_end(), sir.charAt(i));
                System.out.println(tmp.getSt_first() + "\t" + tmp.getSimbol() + "\t" +tmp.getSt_end());
                if (tmp == null)
                    return false;
            }
        }
        for (String st_final : st_finale)
            if (tmp.getSt_end().equals(st_final))
                return true;
        return false;
    }

i'm reading the transitions from the file such as
q0' 0 q5
q0' 0 q2

civic tundraBOT
#

<@&987246883653156906> please have a look, thanks.

civic tundraBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

Unfortunately, your question got cut off. Could you please provide the complete code that you have so far?

neat radish
#

the problem with this order of transitions is that if i'm reading 0.5 for example it would jump from q0' to q5 and it would ignore the other q2 that has a way of 0 aswell and the program would end with just 0. should i put the final states at the end of the program or is it a way of avoiding these scenarios using code, any advice would help me.

rustic basalt
#

hi

#

uhh

#

need help?

#

woah woah woah

#

thats complex af

#

you can shorten and simplify the code

neat radish
#

it's the code made by my teacher

#

i just need to complete the analyzeWord, but as i said i'm quite unsure of some things

rustic basalt
#

oh

#

i cant help with that then

#

like

#

if u can shorten the code

#

and make it simple

neat radish
#

that's pretty much it

tawdry hawk
#

Was this code originally in another language? Some functions make me think they are the same, but not translated...
Is findTransition the same as gasesteTranzitie? Is getSt_first the same as getSt_sf?
What do the getSt_* functions do?

neat radish
#

getSt_first is the first state such as q0 and getSt_sf is the next transition such as q0 "a" q1

#

these 2 functions are getters, they return the first state/current state and getSt_sf returns next state

#

i edited the second code so it's more understandable

tawdry hawk
#

Ok, thanks. And just so I understand, you already have the implementation of a DFA, and the problem is that you need to properly instantiate it to accept real numbers in a proper format?

neat radish
#

i could give you an example to understand my problem

tawdry hawk
#

Oh, wait, you need to implement the analyzeWord, right?

neat radish
#

yes

#

i don't think i implemented it right

#

firstly, i want to ask you whether the transitions are written correctly in the text file

#

so i wrote some transitions like this
q0' 0 q5
q0' + q0
q0' - q0
this is what i thought of

tawdry hawk
#

Well, there probably shouldn't be an arrow from q0 pointing up to q5 with 0, there is another arrow from q0 with 0, which makes the FA not Deterministic, altough thats probably an error

#

Other than that, the diagram looks fine

neat radish
#

but if the input would be just 0, q5 represents the final state and the program ends

tawdry hawk
#

Why is that a problem?

neat radish
#

if it goes down the path to q2, it would expect to have a . and decimals after the .

tawdry hawk
#

Ok, but why not make q2 a final state?

neat radish
#

and simply remove q5?

tawdry hawk
#

well, if you want a DFA, you will not be able to construct it with transition both to q2 and q5 anyway, if I'm not mistaken

#

so yes

neat radish
#

but if it would reach q2, isn't q2 a final state and it should stop there?

#

how would q2 be capable of going to q3 and continue to write the decimals after 0? or am i making a mistake here and it's possible to pass a final state and go into another state?

tawdry hawk
#

Oh, it is absolutely possible to continue accepting from a final state, the difference between non-final states only matters when the word input is over (i.e. the whole word has been read)

neat radish
#

got it

tawdry hawk
#

You actually already seem to have q1 as a final state, so I thought you knew this

neat radish
#

yeah i understand now

#

also, in the case of first letter to be 0, it could go 2 paths

#

isn't it that way? it should be nondeterministic

tawdry hawk
#

yep, exactly. deterministic means no choice

neat radish
#

i'm pretty sure in class the teacher was reffering to get used to dfa firstly, then learn to convert a dfn into dfa

#

when i created the analyzeWord method, it worked for a very simple dfa

tawdry hawk
#

Well, the method is probably not shown whole. There must be a return true somewhere in there right?

neat radish
#

yeah, check now, i'm sorry

tawdry hawk
#

I see one error. Did it not work on something?

#

(I suspect it does not work on empty words)

neat radish
#

it should work now

#

so this was the method that i wrote, and as input in the file i had:
q0 "a" q1
q0 "b" -
q1 "a" q2
q1 "b" q1
q2 "a" -
q2 "b" -

#

it would work for strings such as a[(b)^n]a

tawdry hawk
#

That seems correct. So what is the problem?

neat radish
#

but this type of strings, and the schema that i have is deterministic

#

but i literally got the schema that is above and tried to create a code for that specific one

#

this nfa that i provided you with above, should firstly be converted into a dfa, right?

#

in order for my code to work

tawdry hawk
#

Umm, yes, right. Although that may sometimes lead to a very complicated DFA, i'm afraid.

neat radish
#

yeah, that's true

#

i thought of it being nondeterminisitic, but idk what led me into trying to make a code...

tawdry hawk
#

although, wait

#

if you removed the transition above to q5, it is almost deterministic, it may be worth it to think just about q3 and q4

#

and complications with conversion to DFA using some algorithm could be avoided

neat radish
#

the schema was not made by me, the teacher came up with it

#

on the left you got accepted words and on the right the words that are not accepted

#

that was the goal, so i guess he went for a nondeterministic

tawdry hawk
#

Well, if you can convert it to DFA in some way, then it is fine

neat radish
#

we just learnt the conversion at the course, just an introduction so i guess his main goal was to create this algorithm for a deterministic one and i understood it all wrong ..

#

so, the only way to code a nfa is by converting it into a dfa, right?

#

there is no other shortcut

tawdry hawk
#

If the only thing you can simulate is a DFA then yes... but the thing about q3 and q4 I was telling you is that you can semantically split the automaton into a deterministic portion, and then a nondeterministic kind of 'sub-automaton' consisting of only q3 and q4, and you only need to convert this sub-automaton into a DFA

neat radish
#

i think i understand what you re saying

#

i think i got all my answers

#

thank you so much for explaining in detail