patterncsharpMinor
Sample table-driven finite-state machine
Viewed 0 times
samplefinitetablestatemachinedriven
Problem
We have interviewees write sample code, but now we want them to modify some simple working code. I came up with the following proposal, which is a syntax-directed interpreter implemented with a table-driven finite state machine. The interpreter simply accepts whitespace-delimited text and outputs "Hello" when it sees the case-insensitive command "HELLO". Any other input is an error.
This is a highly-simplified version of some C code converted to C# for the interview:
The proposed exercise is to extend this interpreter to accept a "HELP" command that will print out:
"usage 'HelloScript <scriptfile'
This can be done in about a half-dozen changes.
A co-worker I asked this about was able to complete it in under 15 minutes, but wanted to argue about terminology, implementation, et al.
The intent is for it to be a simple exercise only to see if interviewees can understand and maintain existing code. How understandable and maintainable is it?
This is a highly-simplified version of some C code converted to C# for the interview:
enum Lexeme
{
Error,
EOF,
Whitespace,
H, E, L, O
}
static Lexeme LexemeOfIntChar(int cIn)
{
if (cIn = 0) && (currentState != 7))
{
Lexeme t = LexemeOfIntChar(Console.Read());
currentState = transition[currentState, (int)t];
switch (currentState)
{
case -1: Console.WriteLine("Invalid character!"); break;
case 6: Console.WriteLine("Hello"); break; // 6 = saw 'HELLO' with terminal symbol
}
}
}The proposed exercise is to extend this interpreter to accept a "HELP" command that will print out:
"usage 'HelloScript <scriptfile'
This can be done in about a half-dozen changes.
A co-worker I asked this about was able to complete it in under 15 minutes, but wanted to argue about terminology, implementation, et al.
The intent is for it to be a simple exercise only to see if interviewees can understand and maintain existing code. How understandable and maintainable is it?
Solution
If I were a candidate, I'd judge what I think the quality of your code looks like based on this simple, and I'd have some concerns:
-
You've two extra sets of parens here:
- Your transition table depends on the values of the
Lexemeenumnot changing (say by inserting additional values). If this is the case I'd like there to be a comment onenumsaying order is significant or having the values explicitly given orders. Otherwise I'd expect the actual numbers to not matter.
- Your code contains a transition table. That's hard to read and hard to modify. Why would you do that?
-
You've two extra sets of parens here:
while ((currentState >= 0) && (currentState != 7))- Your states and done via magic numbers and not an
enum.
- The check for -1 feels like it's in the wrong place. It is handling an error condition. But the other element of the switch isn't an error condition.
Code Snippets
while ((currentState >= 0) && (currentState != 7))Context
StackExchange Code Review Q#5044, answer score: 4
Revisions (0)
No revisions yet.