6.1. top level parser

Start felix section to tut/examples/tut_beg310.flx[1 /1 ]
     1: #line 3296 "./lpsrc/flx_tutorial.pak"
     2: #import <flx.flxh>
     3: 
     4: // the input string
     5: data := "1+2+3$";
     6: 
     7: // a type for tokens
     8: union token_t =
     9:   | TOK_EOF
    10:   | TOK_PLUS
    11:   | TOK_INT of int
    12: ;
    13: 
    14: // a token stream generator
    15: var i = 0;
    16: fun get_token():token_t =
    17: {
    18:   ch := data.[i to i+1];
    19:   ++i;
    20:   tok :=
    21:     match ch with
    22:     | "$" => TOK_EOF
    23:     | "+" => TOK_PLUS
    24:     | "1" => TOK_INT 1
    25:     | "2" => TOK_INT 2
    26:     | "3" => TOK_INT 3
    27:     endmatch
    28:   ;
    29:   return tok;
    30: }
    31: 
    32: // a type for expression terms
    33: union expr_t =
    34:   | Integer of int
    35: ;
    36: 
    37: // a grammar for expressions
    38: nonterm eexpr : expr_t =
    39: | xx:eexpr TOK_PLUS y:TOK_INT =>
    40:   match xx with
    41:   | Integer ?i => Integer (i+y)
    42:   endmatch
    43: 
    44: | y:TOK_INT => Integer y
    45: ;
    46: 
    47: // a parser for our example
    48: var z : 1 + int =
    49:   parse (the get_token) with
    50:   | e: eexpr => match e with | Integer ?i => i endmatch
    51:   endmatch
    52: ;
    53: 
    54: // print the result
    55: match z with
    56: | case 0 => { print "Error"; }
    57: | case 1 (?i) => { print i; }
    58: endmatch;
    59: endl;
    60: 
End felix section to tut/examples/tut_beg310.flx[1]