6.1. top level parser

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