"Tag" Finite State Machine

D

Davide T

How would I illustrate a finite state machine that checks that each start
tag in an HTML document has a corresponding end tag (ignoring nested tags of
the same type and assuming that there is only a finite set of tags)?
 
E

Eric Bohlman

How would I illustrate a finite state machine that checks that each
start tag in an HTML document has a corresponding end tag (ignoring
nested tags of the same type and assuming that there is only a finite
set of tags)?

Very awkwardly. Such a machine would need to have one state for every
possible combination of open tags; if N is the number of tags in HTML, then
under your constraints there would be N! states; with only 10 tags (there
are more than that), you'd have over 3 million possible states.
 
D

Davide T

Eric Bohlman said:
Very awkwardly. Such a machine would need to have one state for every
possible combination of open tags; if N is the number of tags in HTML, then
under your constraints there would be N! states; with only 10 tags (there
are more than that), you'd have over 3 million possible states.

Right, well say there was only two tags... <head> and <body> - how would I
do it?
 
A

Andy Dingley

How would I illustrate a finite state machine that checks that each start
tag in an HTML document has a corresponding end tag (ignoring nested tags of
the same type and assuming that there is only a finite set of tags)?

Very easily, if you allow maintenance of some attributional state (ie
the name of the tag that opened the element), but otherwise impossible
in a FSM (trivial proof), unless you permit a truly huge number of
states and a limited length document.

Nesting tags of the same type makes no difference.


This just isn't an appropriate task for a classical FSM, but it's very
easily solved by something with a stack.
 
H

Hywel Jenkins

How would I illustrate a finite state machine that checks that each start
tag in an HTML document has a corresponding end tag (ignoring nested tags of
the same type and assuming that there is only a finite set of tags)?

Recursion.
 
E

Eric Bohlman

Right, well say there was only two tags... <head> and <body> - how
would I do it?

Well, in that case you'd have three states: [empty],[<head>], and
[<head><body>] (assuming you required them to be in the correct order).
[empty] would have a transition to [<head>] on a <head> tag. [<head>]
would have a transition back to [empty] on </head>, and a transition to
[<head><body>] on <body>. And so on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top