Forum Moderators: phranque

Message Too Old, No Replies

Anyone know anything about Finite State Machines & enjoy a challenge?

         

macca031

9:36 pm on Dec 9, 2003 (gmt 0)

10+ Year Member



i am currently doing a course at university and the following quiestion has been set

Illustrate a simple finite state machine that ensures that each start tag in a data format is matched by a corresponding end tag, assuming that tags of the same kind cannot be nested and that there is only a finite set of possible tags.

does anyone have any idea?

im really stuck

macca

(PS i realise its not a webmaster-related question but im in a real panic!)

mcavic

11:39 pm on Dec 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've done something like this several times in real code. (And I've applied it to parsing HTML code, so it is Webmaster related). :)

Define an array to contain a flag for each type of tag. The flag will tell whether you've encountered a start tag of that type, but not yet found a corresponding end tag.

Then, read though the data. When you see a start tag, set its flag to 1. When you see an end tag, set it to 0.

When you run out of data, all flags must be 0. And if a flag is already 1 when you're trying to set it to 1, that's an error.

I don't know if this is technically a finite state machine, but it's close, and I think it's the most logical way to deal with multiple types of tags.

victor

11:57 pm on Dec 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A FSM is only going to be of any use if you have tags that also nest "correctly".

You could use an FSM for nesting like:

<a> <b> </b> <c> </c> </a>

But I don't think it would work with:

<a> <c> <b> </c> </a> </b>

Either way, a stack or a pushdown list would be more natural.

With an FSM, I reckon you'd have to know what the tags were in advance. With a stack, you could handle arbitrary tags.

mcavic

12:30 am on Dec 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good point Victor, but I think it's the other way around.

The FSM will deal with any kind of nesting. But it won't guarantee correct nesting. You could also change the flags to counters, to allow tags of the same kind to be nested within each other. And if you use an associative array (hash), you could deal with unknown tags.

A stack will guarantee correct nesting, but it won't work if incorrect nesting is acceptable.

victor

10:35 am on Dec 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Macvic:

Quite right -- thanks for the correction. I shouldn't write these things without enough coffee.