0

Hi I have programmed my finite state automata string matching algorithm. However I am struggling to limit the alphabet to only two characters. My implementation looks similar to http://www.sanfoundry.com/cpp-program-perform-finite-state-automaton-based-search/.

The NO_OF_CHAR variable is stating the alphabet of the program. I am trying to limit this to only two characters {0,1} eg: 0101001. If anyone has knowledge of finite state automata, input would be appreciated.

jonn
  • 103
  • 2
  • 2
  • 11
  • 1
    OK, so what have you done during your struggle to limit the alphabet length? What results have you get? – CiaPan Mar 18 '15 at 14:14
  • When I change the variable NO_OF_CHARS to 2, the program has an error. I think this is due to char being 256? – jonn Mar 18 '15 at 14:19
  • What do you mean *my implementation looks similar to*? Do you have your own code, or is it just a copy of what's at that link? And what do you mean *the program has an error*? What was the error? Can you narrow down the area in your code where you have the error and show that portion? – lurker Mar 18 '15 at 14:23
  • What kind of error? Does it refuse to compile? It compiles but doesn't start? It runs but crashes? It runs successfuly but produces wrong output?...... What error does it 'have'? – CiaPan Mar 18 '15 at 14:25
  • The program crashes. Get error message: abc.exe file has stopped working. The program does run but it crashes after. – jonn Mar 18 '15 at 14:26
  • Superb. Now kindly tell us what data are you supplying to your automaton. – CiaPan Mar 18 '15 at 14:29
  • The data type I am using is char. – jonn Mar 18 '15 at 14:32
  • I know what *type* of data it uses. But I'm curious if you realise what *values* you give it to process. – CiaPan Mar 18 '15 at 14:33
  • char text[]="0101001010101"; char pattern[]="1001"; I think is what your asking. My code is similar to the link given above – jonn Mar 18 '15 at 14:37
  • Gets the same error as I do when i change the variable. Because the data type is char it doesn't handle any value other than 256 – jonn Mar 18 '15 at 14:45
  • I'll ask again: what do you mean by, *My code is similar to the link given above*? How similar? What's different? If you're going to use someone else's code to solve your problem, you need to learn at least a little bit about how that code works. – lurker Mar 18 '15 at 14:47
  • Well, so input is `char text[]="0101001010101"; char pattern[]="1001";` Ok, see my answer. – CiaPan Mar 18 '15 at 14:48

2 Answers2

1

From OP answer to my questions for a program's input:

char text[]="0101001010101"; char pattern[]="1001";

So you give it a normal string with characters encoded in ASCII. The FSM uses those characters to index a state-and-transition table (line 60.) Character '0' in your input string is an int value of 48 while '1' is 49. When you declare the array 2-items long those values cause the expression to reach far beyond the array limits and read some random data. That causes the program to wander in unexpected direction and eventually crash. It is a special case of Undefined Behavior.

Solution: Set NO_OF_CHAR at least 49 49+1. (Thank you, @wildplasser!)

CiaPan
  • 9,381
  • 2
  • 21
  • 35
0

An answer was already accepted but I post this based on the OP insistence in an earlier question on this topic, that there must be only 2 possibilities.

int TF[][NO_OF_CHARS] is an array originally sized by #define NO_OF_CHARS 256. So in the example all possible unsigned char values can index it. When you try to reduce the number of chars to 2 you can only index this array by 0 or 1, but if your '0' and '1' in the cellular string are ASCII values, they will break the array.

Based on that this line (and possibly others) are foxing the array

state = TF[state][txt[i]];

Notice that with characters '0' and '1' the array will be indexed by 48 and 49. What you need to do here, and possibly elsewhere, is

state = TF[state][txt[i] & 1];

Also watch out whether there are any places where this index of 0 or 1 is turned back into a char. If so you will need to add '0' to the array index.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56