Tracking Odd or Even Symbol Count


This automaton tracks if the number of target symbols is odd or even.

  • The input data stream may consist of any number of leading # symbols.
  • These symbols are then followed by any number of A symbols, followed by a trailing # symbol.
  • When this trailing # symbol is seen, the automaton will report if the number of A symbols seen was odd or even.


AP Workbench

Odd or even symbol count

C Code

#include <micron/ap/ap_defs.h>
#include <micron/ap/ap_anml.h>

int main(int argc, char* argv[]) {
    ap_anml_t anml = NULL;
    ap_anml_network_t anml_net;
    struct ap_anml_element element = {0};
    ap_anml_element_ref_t lead, odd, even, odd_count, even_count;

    // Initialize the automata network
    anml = AP_CreateAnml();
    AP_CreateAutomataNetwork(anml, &anml_net, "odd_or_even_symbol_count");

    // Build the network = "lead";
    element.res_type = RT_STE;
    element.start = START_OF_DATA;
    element.symbols = "#";
    AP_AddAnmlElement(anml_net, &lead, &element);

    element.start = NO_START;
    element.match = 1; = "odd";
    AP_AddAnmlElement(anml_net, &odd, &element); = "even";
    AP_AddAnmlElement(anml_net, &even, &element);

    element.symbols = "A";
    element.match = 0; = "odd_count";
    AP_AddAnmlElement(anml_net, &odd_count, &element); = "even_count";
    AP_AddAnmlElement(anml_net, &even_count, &element);

    AP_AddAnmlEdge(anml_net, lead, lead, 0);
    AP_AddAnmlEdge(anml_net, lead, odd_count, 0);
    AP_AddAnmlEdge(anml_net, odd_count, even_count, 0);
    AP_AddAnmlEdge(anml_net, even_count, odd_count, 0);
    AP_AddAnmlEdge(anml_net, odd_count, odd, 0);
    AP_AddAnmlEdge(anml_net, even_count, even, 0);

    // Export the network to an ANML file
    AP_ExportAnml(anml_net, "odd_or_even_symbol_count.anml", "");

    // Clean up 
    return 0;

Python Code

from micronap.sdk import *

def main():
    # Initialize the automata network
    A = Anml()
    AN = A.CreateAutomataNetwork(anmlId='odd_or_even_symbol_count')

    # Build the network
    lead = AN.AddSTE('#', startType=AnmlDefs.START_OF_DATA, anmlId='lead')
    AN.AddAnmlEdge(lead, lead, 0)

    odd_count = AN.AddSTE('A', anmlId='odd_count')
    AN.AddAnmlEdge(lead, odd_count, 0)

    even_count = AN.AddSTE('A', anmlId='even_count')
    AN.AddAnmlEdge(odd_count, even_count, 0)
    AN.AddAnmlEdge(even_count, odd_count, 0)

    odd = AN.AddSTE('#', match=True, anmlId='odd')
    AN.AddAnmlEdge(odd_count, odd, 0)

    even = AN.AddSTE('#', match=True, anmlId='even')
    AN.AddAnmlEdge(even_count, even, 0)

    # Export the network to an ANML file

if __name__ == '__main__':


This simple design illustrates a basic concept that could be developed into more sophisticated designs: automata can be created to perform modular arithmetic.

In this case, the design simply toggles between an even_count and an odd_count state each time an A symbol is seen. The first STE (lead) is active at the beginning of the input data stream, and it consumes any number of leading # symbols through self-activation. It also enables the transition to STE even_count when the first A symbol is seen. After the automaton is toggling between the even_count and the odd_count STEs, the first trailing # symbol transitions the automaton to a reporting state, with the report event being generated by either the even_count STE or the odd_count STE. The STE that generates the report event indicates if the number of A symbols processed was even or odd.

Extending upon this design of performing modular arithmetic, an automaton could report if the number of A symbols was a multiple of 3, 4, 5, or any integer.