Write a program to determine whether an instruction obeys the following rules, which are loosely based on real car trialling instructions. BOLD-TEXT indicates text as it appears in the instruction (case sensitive), separates options of which exactly one must be chosen, and .. expands, so A..D is equivalent to A B C D .
instruction = navigational time-keeping navigational AND time-keeping
navigational = directional navigational AND THEN directional
directional = how direction how direction where
how = GO GO when KEEP
direction = RIGHT LEFT
when = FIRST SECOND THIRD
where = AT sign
sign = "signwords"
signwords = s-word signwords s-word
s-word = letter s-word letter
letter = A..Z .
time-keeping = record change
record = RECORD TIME
change = cas TO nnn KMH
cas = CHANGE AVERAGE SPEED CAS
nnn = digit nnn digit
digit = 0..9 Note that s-word and nnn are sequences of letters and digits respectively, with no intervening spaces. There will be one or more spaces between items except before a period (.), after the opening speech marks or before the closing speech marks.
KEEP LEFT AND THEN GO RIGHT CAS TO 20 KMH GO FIRST RIGHT AT "SMITH ST." AND CAS TO 20 KMH GO 2nd RIGHT GO LEFT AT "SMITH STREET AND RECORD TIME KEEP RIGHT AND THEN RECORD TIME #
1. KEEP LEFT AND THEN GO RIGHT 2. CAS TO 20 KMH 3. GO FIRST RIGHT AT "SMITH ST." AND CAS TO 20 KMH 4. Trap! 5. Trap! 6. Trap!