my $TG = Marpa::R2::Grammar->new({ start=>'FILE', actions=>'ToyLang', default_action=>'swallow', unproductive_ok=>[qw( FILE )], rules=>[ # A file contains a PROGRAM and zero or more SUBROUTINES. The # subroutine definitions may precede and/or follow the program. [ FILE=>[qw( PROGRAM name stmt_list )], 'swallow' ], #PROGRAM FILE2)], ], [ FILE=>[qw( COMMENT FILE )], ], #stmt_list PROGRAM FILE2)], ], # [ FILE=>[qw( PROGRAM name stmt_list PROGRAM FILE2)], ], # [ FILE=>[qw( SUB name stmt_list sub FILE)], ], # [ FILE2=>[qw( SUB name stmt_list sub FILE2)], ], # A statement list consists of zero or more statements followed # by END. We don't care whether or not there's an end of # statement marker before END. [ stmt_list=>[qw( END )], 'discard' ], [ stmt_list=>[qw( stmt stmt_list_2 )], 'first_on' ], [ stmt_list_2=>[qw( END )], 'discard' ], [ stmt_list_2=>[qw( OP_EOS END )] ], [ stmt_list_2=>[qw( OP_EOS stmt stmt_list_2 )], 'second_on' ], # [ stmt=>[qw( IF expr if_body )], ], [ stmt=>[qw( PRINT expr print_body )], ], [ stmt=>[qw( WHILE expr DO do_body )], ], # [ stmt=>[qw( DO do_body WHILE expr )], ], [ stmt=>[qw( name assop expr )], 'binary_op' ], [ do_body=>[qw( LOOP )], ], [ do_body=>[qw( stmt do_body_2 )], 'first_on' ], [ do_body_2=>[qw( LOOP )], ], [ do_body_2=>[qw( OP_EOS LOOP )], 'second_arg' ], [ do_body_2=>[qw( OP_EOS stmt do_body_2 )], 'second_arg' ], [ print_body=>[qw( OP_EOS )], ], [ print_body=>[qw( OP_COMMA expr print_body )], 'second_on' ], [ expr=>[qw( term )], 'first_arg' ], [ expr=>[qw( expr logop expr )], 'binary_op' ], [ term=>[qw( term addop term )], 'binary_op' ], [ term=>[qw( factor )], 'first_arg'], [ factor=>[qw( factor mulop factor )], 'binary_op'], [ factor=>[qw( name )], 'first_arg'], [ factor=>[qw( number )], 'first_arg'], [ factor=>[qw( string )], 'first_arg'], [ addop=>[qw( OP_ADD )], 'first_arg'], [ addop=>[qw( OP_SUB )], 'first_arg'], [ assop=>[qw( OP_EQ )], 'first_arg'], [ assop=>[qw( OP_EQADD )], 'first_arg'], [ logop=>[qw( OP_NEQ )], 'first_arg'], [ mulop=>[qw( OP_MUL )], ], [ mulop=>[qw( OP_DIV )], ], ], });