text: struct { int foo ; int bar ; } ------ - --- --- - --- --- - - IDENT "{" IDENT IDENT ";" IDENT IDENT ";" "}" --- --- --- --- type var type var --------- --------- decl decl ------------------------- decl_list --------------------------------------------- struct --------------------------------------------- parse #### parse : foo1 foo2 | bar1 bar2 #### use strict; use warnings; use Parse::RecDescent qw( ); my $grammar = <<'__EOI__'; { use strict; use warnings; } parse : foo1 foo2 /\Z/ { [ @item[0,1,2] ] } | bar1 bar2 /\Z/ { [ @item[0,1,2] ] } foo1 : "X" { print("$item[0]\n"); [ @item[0,1] ] } foo2 : "Y" { print("$item[0]\n"); [ @item[0,1] ] } bar1 : "X" { print("$item[0]\n"); [ @item[0,1] ] } bar2 : "Z" { print("$item[0]\n"); [ @item[0,1] ] } __EOI__ Parse::RecDescent->Precompile($grammar, 'Grammar') or die("Bad grammar\n"); #### use strict; use warnings; use Data::Dumper qw( Dumper ); use Grammar qw( ); my $parser = Grammar->new(); my $matches = $parser->parse('XZ') or die("Bad input\n"); print("\n"); print(Dumper($matches)); #### foo1 bar1 bar2 $VAR1 = [ 'parse', [ 'bar1', 'X' ], [ 'bar2', 'Z' ] ];