How to resolve the algorithm Execute a Markov algorithm step by step in the CLU programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Execute a Markov algorithm step by step in the CLU 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   ,   then this is a terminating rule in which case the interpreter must halt execution. A ruleset consists of a sequence of rules, with optional comments.

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 CLU programming language

Source code in the clu programming language

markov = cluster is make, run
    rule = struct[from, to: string, term: bool]
    rep = array[rule]
    
    % Remove leading and trailing whitespace from a string
    trim = proc (s: string) returns (string)
        ac = array[char]
        sc = sequence[char]
        own ws: string := "\n\t "
        a: ac := string$s2ac(s) 
        while ~ac$empty(a) cand string$indexc(ac$bottom(a), ws) ~= 0 do 
            ac$reml(a)
        end
        while ~ac$empty(a) cand string$indexc(ac$top(a), ws) ~= 0 do
            ac$remh(a)
        end
        return(string$sc2s(sc$a2s(a)))
    end trim
    
    % Parse a single Markov rule
    parse = proc (s: string) returns (rule) signals (comment, invalid(string))
        if string$empty(s) cor s[1]='#' then signal comment end
        arrow: int := string$indexs(" -> ", s)
        if arrow=0 then signal invalid(s) end
        left: string := trim(string$substr(s, 1, arrow-1))
        right: string := trim(string$rest(s, arrow+4))
        
        if ~string$empty(right) cand right[1] = '.' then
            right := string$rest(right, 2)
            return(rule${from: left, to: right, term: true})
        else
            return(rule${from: left, to: right, term: false})
        end
    end parse
    
    % Add a rule to the list
    add_rule = proc (m: cvt, s: string) signals (invalid(string))
        rep$addh(m, parse(s)) resignal invalid
        except when comment: end
    end add_rule
    
    % Read rules in sequence from a stream
    add_rules = proc (m: cvt, s: stream) signals (invalid(string))
        while true do
            add_rule(up(m), stream$getl(s)) resignal invalid
            except when end_of_file: break end
        end
    end add_rules
    
    make = proc (s: stream) returns (cvt) signals (invalid(string))
        a: rep := rep$new()
        add_rules(up(a), s)
        return(a)
    end make
    
    % Apply a rule to a string
    apply_rule = proc (r: rule, s: string) returns (string) signals (no_match)
        match: int := string$indexs(r.from, s)
        if match = 0 then signal no_match end
        new: string := string$substr(s, 1, match-1)
                    || r.to 
                    || string$rest(s, match+string$size(r.from))
        return(new)
    end apply_rule
    
    % Apply all rules to a string repeatedly
    run = proc (c: cvt, s: string) returns (string)
        i: int := 1 
        while i <= rep$high(c) do
            r: rule := c[i]
            begin
                s := apply_rule(r, s)
                i := 1
                if r.term then break end
            end except when no_match: 
                i := i+1 
            end
        end
        return(s)
    end run
end markov

start_up = proc ()
    po: stream := stream$primary_output()
    eo: stream := stream$error_output()
    
    begin
        args: sequence[string] := get_argv()
        file: string := args[1]
        input: string := args[2]
        fs: stream := stream$open(file_name$parse(file), "read")
        mkv: markov := markov$make(fs)
        stream$close(fs)
        stream$putl(po, markov$run(mkv, input))
    end except 
        when bounds: stream$putl(eo, "Arguments: markov [filename] [string]")
        when not_possible(s: string): stream$putl(eo, "File error: " || s)
        when invalid(s: string): stream$putl(eo, "Parse error: " || s)
    end
end start_up

  

You may also check:How to resolve the algorithm Color of a screen pixel step by step in the 8086 Assembly programming language
You may also check:How to resolve the algorithm File input/output step by step in the Pike programming language
You may also check:How to resolve the algorithm XML/Input step by step in the OpenEdge/Progress programming language
You may also check:How to resolve the algorithm Barnsley fern step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Selectively replace multiple instances of a character within a string step by step in the sed programming language