# Def

The def is where colm really shines. A def is somewhere between a struct and a regular expression. Again one example is much more clearer.

assign.lm

    lex
            token id / ('a' .. 'z' | 'A' .. 'Z' ) + /
            token number / ( '0' .. '9' )+ /
            literal `= `;
            ignore / [ \t\n]+ /
    end

    def value
            [id] | [number]

    def assignment
            [id `= value `;]

    def assignment_list
            [assignment assignment_list]
    |       [assignment]
    |       []

    parse Simple: assignment_list[ stdin ]

    if ( ! Simple ) {
            print( "[error]\n" )
            exit( 1 )
    }
    else {
            for I:assignment in Simple {
                    print( $I.id, "->", $I.value, "\n" )
            }
    }

After the compilation we can pipe some input to it’s stdin.

    /opt/colm/bin/colm assign.lm
    echo -e 'b=3;a=1;\n c=2;' |./assign

This gives us:

    b->3
    a->1
    c->2

| this also illustrates how to read from stdin.