3.2. User defined statements

Felix allows the programmer to invent new statements. The statements must start with a user keyword: they may not start with an existing non-user keyword.

The Felix LR parser recognises these keywords and switches to Recursive Descent parsing mode. Therefore, grammar productions must not be left recursive or the parser will crash.

The leading identifier is automatically turned into a keyword.

These examples are lifted from the system library.

The following identifiers have a special, obvious, meaning:

  ident
  expr
  statement
  statements
  string_literal
  integer_literal

Felix considers productions in LIFO order, like a stack. Therefore, general cases must be given after specialised ones.

Start felix section to tut/examples/mac301.flx[1 /1 ]
     1: #line 750 "./lpsrc/flx_tut_macro.pak"
     2: #include <flx.flxh>
     3: 
     4: #statement#
     5:   whilst expr do statements done ; =>#
     6:   macro {
     7:     macro lab1 is new;
     8:     macro lab2 is new;
     9:     lab1:>
    10:       if not _1 goto lab2;
    11:       _3;
    12:       goto lab1;
    13:     lab2:>
    14:   };
    15: #
    16: 
    17: {
    18:   var x = 10;
    19:   whilst x > 0 do print x; print " "; --x; done;
    20:   endl;
    21: };
    22: 
    23: #statement#
    24:   until expr do statements done; =>#
    25:   macro {
    26:     macro lab1 is new;
    27:     macro lab2 is new;
    28:     lab1:>
    29:       if _1 goto lab2;
    30:       _3;
    31:       goto lab1;
    32:     lab2:>
    33:   };
    34: #
    35: 
    36: {
    37:   var x = 10;
    38:   until x == 0 do print x; print " "; --x; done;
    39:   endl;
    40: };
    41: 
    42: #statement#
    43:   forall ident in expr do statements done ; =>#
    44:     macro for val _1 in _3 do _5; done;
    45: #
    46: 
    47: forall x in (1," plus ",2," is ",1+2) do print x; done; endl;
    48: 
    49: #keyword upto
    50: #statement#
    51:   forall ident in expr upto expr do statements done ; =>#
    52:   _1 = _3;
    53:   whilst _1 <= _5 do _7; ++i; done;
    54: #
    55: 
    56: {
    57:   var i = 0;
    58:   forall i in 1 upto 10 do print i; print " "; done;
    59:   endl;
    60: };
    61: 
    62: #keyword downto
    63: #statement#
    64:   forall ident in expr downto expr do statements done ; =>#
    65:   _1 = _3;
    66:   whilst _1 >= _5 do _7; --i; done;
    67: #
    68: 
    69: {
    70:   var i = 0;
    71:   forall i in 10 downto 1 do print i; print " "; done;
    72:   endl;
    73: };
    74: 
End felix section to tut/examples/mac301.flx[1]