Implement the McNaughton-Yamada-Thompson algorithm, which converts a regular expression into a nondeterministic finite automaton (NFA), by an L-attributed SDD on a top-down parsable grammar. Assume that there is a token char representing any character, and that char.lexval is the character it represents. You may also assume the existence of a function new() that returns a new state, that is, a state never before returned by this function. Use any convenient notation to specify the transitions of the NFA.
Notes:
- I use the characters '{' and '}' to delimit rules, and the rules end up taking the form "{ state-x -> symbol -> state-y } ... { ... }".
- The symbol "||" means concatenation.
- Concatenation ( T -> F Tsub1 ) represented a bit of confusion for me, since it "deletes" state Tsub1.Start from the automaton (the exercise doesn't allow for deletion). McNaughton-Yamada-Thompson write, "The accepting state of N(s) and the start state of N(t) are merged into a single state, with all the transitions in or out of either state."
My initial attempt, setting F.EndTransitions = F.EndTransitions || Tsub1.StartTransitions seems reasonable. Since state Tsub1.Start continues to exist, references to it in Tsub1.StartTransitions, Tsub1.InnerTransitions, or Tsub1.EndTransitions do not require modification.
Everything works because from F.End the automaton may remain in F, or transition to Tsub1. Once the automaton transitions to Tsub1, however, it never returns--no references in Tsub1 point to states in F. In practical terms, this means the automaton produced contains one extra state Tsub1.Start for every concatenation in the original regular expression. - Adding epsilon transitions to Start or End states requires retaining the existing StartTransitions or EndTransitions, respectively.
- A PDF of this solution produced via LaTeX can be found here.
S -> E
- S.Start = E.Start
- S.End = E.End
- S.StartTransitions = E.StartTransitions
- S.EndTransitions = E.EndTransitions
- S.InnerTransitions = E.InnerTransitions
- E.Start = T.Start
- E.End = T.End
- E.StartTransitions = T.StartTransitions
- E.EndTransitions = T.EndTransitions
- E.InnerTransitions = T.InnerTransitions
- E.Start = new()
- E.End = new()
- E.StartTransitions = "{ " || E.Start || " -> epsilon -> " || T.Start || " } { " || E.Start || " -> epsilon -> " || Esub1.Start || " }"
- E.EndTransitions = Nil
- T.EndTransitions = T.EndTransitions || "{ " || T.End || " -> epsilon -> " || E.End || " }
- Esub1.EndTransitions = Esub1.EndTransitions || "{ " || Esub1.End || " -> epsilon -> " || E.End || " }
- E.InnerTransitions = T.StartTransitions || T.InnerTransitions || T.EndTransitions || Esub1.StartTransitions || Esub1.InnerTransitions || Esub1.EndTransitions
- T.Start = F.Start
- T.End = F.End
- T.StartTransitions = F.StartTransitions
- T.EndTransitions = F.EndTransitions
- T.InnerTransitions = F.InnerTransitions
- T.Start = F.Start
- T.End = Tsub1.End
- T.StartTransitions = F.StartTransitions
- T.EndTransitions = Tsub1.EndTransitions
- F.EndTransitions = F.EndTransitions || Tsub1.StartTransitions
- T.InnerTransitions = F.InnerTransitions || F.EndTransitions || Tsub1.InnerTransitions
- F.Start = G.Start
- F.End = G.End
- F.StartTransitions = G.StartTransitions
- F.EndTransitions = G.EndTransitions
- F.InnerTransitions = .InnerTransitions
- F.Start = new()
- F.End = Fsub1.End
- F.StartTransitions = "{ " || F.Start || " -> epsilon -> " || G.Start || " } { " || F.Start || " -> epsilon -> " || Fsub1.Start || " }"
- F.EndTransitions = Fsub1.EndTransitions
- G.EndTransitions = G.EndTransitions || "{ " || G.End || " -> epsilon -> " || G.Start || " } { " || G.End || " -> epsilon -> " || Fsub1.Start || " }"
- F.InnerTransitions = G.StartTransitions || G.InnerTransitions || G.EndTransitions || Fsub1.StartTransitions || Fsub1.InnerTransitions
- G.Start = E.Start
- G.End = E.End
- G.StartTransitions = E.StartTransitions
- G.EndTransitions = E.EndTransitions
- G.InnerTransitions = E.InnerTransitions
- G.Start = Nil
- G.End = Nil
- G.StartTransitions = Nil
- G.EndTransitions = Nil
- G.InnerTransitions = Nil
- G.Start = new()
- G.End = new()
- G.StartTransitions = "{ " || G.Start || " -> " || char.lexval || " -> " || G.End || " }"
- G.EndTransitions = Nil
- G.InnerTransitions = Nil
Do you have solutions to other parts/exercises of Dragon book. I am badly in need of solutions. Plz help. Mail me at srcyberjockey31@gmail.com
ReplyDeleteSadly, no.
ReplyDeleteAlthough there are a few other solutions to problems on my blog--hardly a complete list, however! I stopped working my way through the book after a few months as other priorities surfaced.
ReplyDelete