How to resolve the algorithm Execute a Markov algorithm step by step in the REXX programming language
How to resolve the algorithm Execute a Markov algorithm step by step in the REXX programming language
Table of Contents
Problem Statement
Create an interpreter for a Markov Algorithm.
Rules have the syntax:
There is one rule per line.
If there is a . (period) present before the
Rulesets Use the following tests on entries:
Sample text of: Should generate the output:
A test of the terminating rule Sample text of: Should generate:
This tests for correct substitution order and may trap simple regexp based replacement routines if special regexp characters are not escaped. Sample text of: Should generate:
This tests for correct order of scanning of rules, and may trap replacement routines that scan in the wrong order. It implements a general unary multiplication engine. (Note that the input expression must be placed within underscores in this implementation.) Sample text of: should generate the output:
A simple Turing machine, implementing a three-state busy beaver. The tape consists of 0s and 1s, the states are A, B, C and H (for Halt), and the head position is indicated by writing the state letter before the character where the head is. All parts of the initial tape the machine operates on have to be given in the input. Besides demonstrating that the Markov algorithm is Turing-complete, it also made me catch a bug in the C++ implementation which wasn't caught by the first four rulesets. This ruleset should turn into
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Execute a Markov algorithm step by step in the REXX programming language
Source code in the rexx programming language
/*REXX program executes a Markov algorithm(s) against specified entries. */
parse arg low high . /*allows which ruleset to process. */
if low=='' | low=="," then low=1 /*Not specified? Then use the default.*/
if high=='' | high=="," then high=6 /* " " " " " " */
tellE= low<0; tellR= high<0 /*flags: used to display file contents.*/
call readEntry
do j=abs(low) to abs(high) /*process each of these rulesets. */
call readRules j /*read a particular ruleset. */
call execRules j /*execute " " " */
say 'result for ruleset' j "───►" !.j
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
execRules: parse arg q .; if tellE | tellR then say /*show a blank line?*/
do f=1
do k=1 while @.k\==''; if left(@.k, 1)=='#' | @.k='' then iterate
parse var @.k a ' ->' b /*obtain the A & B parts from rule.*/
a=strip(a); b=strip(b) /*strip leading and/or trailing blanks.*/
fullstop= left(b, 1)==. /*is this a "fullstop" rule? 1≡yes */
if fullstop then b=substr(b, 2) /*purify the B part of the rule. */
old=!.q /*remember the value before the change.*/
!.q=changestr(a, !.q, b) /*implement the ruleset change. */
if fullstop then if old\==!.q then return /*should we stop? */
if old\==!.q then iterate f /*Has Entry changed? Then start over.*/
end /*k*/
return
end /*f*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
readEntry: eFID= 'MARKOV.ENT'; if tellE then say /*show a blank line?*/
!.= /*placeholder for all the test entries.*/
do e=1 while lines(eFID)\==0 /*read the input file until End-Of-File*/
!.e=linein(eFID); if tellE then say 'test entry' e "───►" !.e
end /*e*/ /* [↑] read and maybe echo the entry. */
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
readRules: parse arg ? .; rFID= 'MARKOV_R.'?; if tellR then say /*show a blank line?*/
@.= /*placeholder for all the Markov rules.*/
do r=1 while lines(rFID)\==0 /*read the input file until End-Of-File*/
@.r=linein(rFID); if tellR then say 'ruleSet' ?"."left(r,4) '───►' @.r
end /*r*/ /* [↑] read and maybe echo the rule. */
return
You may also check:How to resolve the algorithm Ordered words step by step in the Groovy programming language
You may also check:How to resolve the algorithm Magic 8-ball step by step in the Locomotive Basic programming language
You may also check:How to resolve the algorithm Pinstripe/Display step by step in the Ring programming language
You may also check:How to resolve the algorithm Sleeping Beauty problem step by step in the Julia programming language
You may also check:How to resolve the algorithm Quine step by step in the Applesoft BASIC programming language