Nick Vanderbilt
Nick Vanderbilt

Reputation: 1777

Is writing parser the right way to solve this programming challenge?

Take a look at this programming challenge.

https://gist.github.com/1083219

We’ve built a new communication protocol that sends messages with a restricted syntax. We need to write a function which determines whether a given message is syntactically valid or not.

Here are the rules:

  1. There are 15 valid characters in the protocol: the lower-case characters ‘a’ through ‘j’ and the uppercase characters ‘Z’, ‘M’, ‘K’, ‘P’, and ‘Q’.
  2. Every lower-case character in isolation is a valid message, e.g., ‘a’ is a valid message.
  3. If σ is a valid message then so is Zσ.
  4. If σ and τ are valid messages then so are Mστ , Kστ , Pστ , and Qστ .
  5. All other messages are invalid.

Write a function in the language of your choosing to check whether messages are valid. The input consists of a series of potential messages separated by whitespace and containing only the 15 characters above.

The output consists of one line per potential messages, followed by ‘VALID’ if the message is valid and ‘INVALID’ if it is invalid.

Below is some example output.

Input    Output
Qa Zj    Qa INVALID
MZca     Zj VALID
Khfa     MZca VALID
         Khfa INVALID

There are many ways to solve it including regex, parsing character by character. I am wondering what others think of how to go about solving the problem.

I think a simple parser might be the best way to go. First write the grammar and then implement the parser.

Upvotes: 0

Views: 244

Answers (1)

murgatroid99
murgatroid99

Reputation: 20297

I think you should be able to do this with a fairly simple recursive string function f:

  1. Look at the first character of the string
  2. If it is a lower case letter, return the rest of the string
  3. If it is Z, return f(the rest of the string)
  4. If it is M, K, P, or Q, then return f(f(the rest of the string))
  5. Otherwise, return the entire string

Then wrap this in a function g that returns true if f returns the empty string and false otherwise. Do this for each message.

This works because the grammar you described is a simple tree structure: Any node with two children is M, K, P, or Q, any node with one child is Z, and any leaf is a lowercase letter. f always consumes exactly one node on the tree and returns the rest of the tree (including siblings), and returns a nonempty string on a malformed tree.

Upvotes: 1

Related Questions