There are many formalisms, so while you may find other sources useful I hope to specify this clearly enough that they're not necessary.
A RM consists of a finite state machine and a finite number of named registers, each of which holds a non-negative integer. For ease of textual input this task requires that the states also be named.
There are three types of state: increment and decrement, which both reference a specific register; and terminate. An increment state increments its register and passes control to its one successor. A decrement state has two successors: if its register is non-zero then it decrements it and passes control to the first successor; otherwise (i.e. register is zero) it simply passes control to the second successor.
For "niceness" as a programming language, the terminate states take a hard-coded string to print (so you can indicate exceptional termination).
Input is from stdin. The input format consists of one line per state, followed by the initial register contents. The first line is the initial state. BNF for the state lines is:
line ::= inc_line | dec_line inc_line ::= label ' : ' reg_name ' + ' state_name dec_line ::= label ' : ' reg_name ' - ' state_name ' ' state_name state_name ::= label | '"' message '"' label ::= identifier reg_name ::= identifier
There is some flexibility in the definition of identifier and message. Your program must accept a non-empty alphanumeric string as an identifier, but it may accept more general strings if you prefer (e.g. if your language supports identifiers with underscores and that's easier for you to work with). Similarly, for message you must accept a non-empty string of alphanumerics and spaces, but you may accept more complex strings which allow escaped newlines and double-quote characters if you want.
The final line of input, which gives the initial register values, is a space-separated list of identifier=int assignments, which must be non-empty. It is not required that it initialise all registers named in the program: any which aren't initialised are assumed to be 0.
Your program should read the input and simulate the RM. When it reaches a terminate state it should emit the message, a newline, and then the values of all the registers (in any convenient, human-readable, format, and any order).
Note: formally the registers should hold unbounded integers. However, you may if you wish assume that no register's value will ever exceed 2^30.
Some simple examplesa+=b, a=0
s0 : a - s1 "Ok" s1 : b + s0 a=3 b=4
Ok a=0 b=7
init : t - init d0 d0 : a - d1 a0 d1 : b + d2 d2 : t + d0 a0 : t - a1 "Ok" a1 : a + a0 a=3 b=4
Test cases for trickier-to-parse machines
Ok a=3 b=7 t=0
s0 : t - s0 s1 s1 : t + "t is 1" t=17
t is 1 t=1
s0 : t - "t is nonzero" "t is zero" t=1
t is nonzero t=0
A more complicated example
Taken from the DailyWTF's Josephus problem code challenge. Input is n (number of soldiers) and k (advance) and output in r is the (zero-indexed) position of the person who survives.
init0 : k - init1 init3 init1 : r + init2 init2 : t + init0 init3 : t - init4 init5 init4 : k + init3 init5 : r - init6 "ERROR k is 0" init6 : i + init7 init7 : n - loop0 "ERROR n is 0" loop0 : n - loop1 "Ok" loop1 : i + loop2 loop2 : k - loop3 loop5 loop3 : r + loop4 loop4 : t + loop2 loop5 : t - loop6 loop7 loop6 : k + loop5 loop7 : i - loop8 loopa loop8 : r - loop9 loopc loop9 : t + loop7 loopa : t - loopb loop7 loopb : i + loopa loopc : t - loopd loopf loopd : i + loope loope : r + loopc loopf : i + loop0 n=40 k=3
Ok i=40 k=3 n=0 r=27 t=0
That program as a picture, for those who think visually and would find it helpful to grasp the syntax:
If you enjoyed this golf, have a look at the sequel.