my $grammar = <<'END_OF_GRAMMAR';
employee_info: 'employee' name id
name: m{ \S+ }xms
id: m{ \d+ }xms
END_OF_GRAMMAR
####
my $text = "employee Joe 10";
my $parser = Parse::RecDescent->new($grammar)
or die "Bad grammar!\n";
defined $parser->employee_info($text)
or die "Text doesn't match";
##
##
my $grammar = <<'END_OF_GRAMMAR';
employee_info: 'employee' name id
{ print "$_\n" for @item; } #Action
name: m{ \S+ }xms
id: m{ \d+ }xms
END_OF_GRAMMAR
##
##
use strict;
use warnings;
use 5.012;
use Parse::RecDescent;
$::RD_ERRORS = 1; #Parser dies when it encounters an error
$::RD_WARN = 1; #Enable warnings - warn on unused rules &c.
$::RD_HINT = 1; #Give out hints to help fix problems.
#$::RD_TRACE = 1; #Trace parsers' behaviour
my $grammar = <<'END_OF_GRAMMAR';
employee_info: 'employee' name id
{ print "$_\n" for @item; } #Action
name: m{ \S+ }xms
id: m{ \d+ }xms
END_OF_GRAMMAR
my $text = "employee Joe 10";
my $parser = Parse::RecDescent->new($grammar)
or die "Bad grammar!\n";
defined $parser->employee_info($text)
or die "Text doesn't match";
--output:--
employee_info
employee
Joe
10
##
##
myrule: brace_clause
brace_clause: '{' word '}'
word: m{ [a-z]+ }xms
##
##
myrule: brace_clause
brace_clause: '{' word '}'
{ print "$_\n" for @item; }
word: m{ [a-z]+ }xms
##
##
brace_clause #the rule name
{ #the match for '{'
hello #the match for word
} #the match for '}'
##
##
myrule: brace_clause
{ print "$_\n" for @item; }
brace_clause: '{' word '}'
word: m{ [a-z]+ }xms
##
##
myrule
{ hello }
##
##
myrule
}
##
##
myrule: brace_clause
{ print "$_\n" for @item; }
brace_clause: '{' word '}'
{ join ' ', @item[1..3] }
word: m{ [a-z]+ }xms
--output:--
myrule
{ hello }
##
##
my $grammar = <<'END_OF_GRAMMAR';
#Start up action(executed in parser namespace):
{
use 5.012; #enable say()
use Data::Dumper;
}
…
…
END_OF_GRAMMAR
##
##
my $grammar = <<'END_OF_GRAMMAR';
#Start up action(executed in parser namespace):
{
use 5.012; #enable say()
use Data::Dumper;
}
employee_info: 'employee' name id
{ say Dumper(\@item); } #Action
name: m{ \S+ }xms
id: m{ \d+ }xms
END_OF_GRAMMAR
my $text = "employee Joe 10";
my $parser = Parse::RecDescent->new($grammar)
or die "Bad grammar!\n";
defined $parser->employee_info($text)
or die "Text doesn't match";
--output:--
$VAR1 = [
'employee_info',
'employee',
'Joe',
'10'
];
##
##
use strict;
use warnings;
use 5.012;
use Parse::RecDescent;
$::RD_ERRORS = 1; #Parser dies when it encounters an error
$::RD_WARN = 1; #Enable warnings - warn on unused rules &c.
$::RD_HINT = 1; #Give out hints to help fix problems.
#$::RD_TRACE = 1; #Trace parsers' behaviour
my $grammar = <<'END_OF_GRAMMAR';
{
use Data::Dumper;
use 5.012; #enable say()
}
another_rule: 'new' employee_info
{ say Dumper(\@item); }
employee_info: 'employee' name id
{
say Dumper(\@item);
say '-' x 20;
'hello world';
}
name: m{ \S+ }xms
id: m{ \d+ }xms
END_OF_GRAMMAR
my $text = "new employee Joe 10";
my $parser = Parse::RecDescent->new($grammar)
or die "Bad grammar!\n";
defined $parser->another_rule($text)
or die "Text doesn't match";
--output:--
$VAR1 = [
'employee_info',
'employee',
'Joe',
'10'
];
--------------------
$VAR1 = [
'another_rule',
'new',
'hello world'
];
##
##
employee_info: 'employee'
name
{ say $item[1]; }
id
name: m{ \S+ }xms
id: m{ \d+ }xms
##
##
employee_info: 'employee'
name
{ say $item[1]; }
id
{ say $item[3] } #print match for id
##
##
my $grammar = <<'END_OF_GRAMMAR';
#Start up action(executed in parser namespace):
{
use 5.012; #So I can use say()
}
...
##
##
my $grammar = <<'END_OF_GRAMMAR';
#Start up action(executed in parser namespace):
{ use 5.012; #So I can use say() }
...
##
##
Unknown starting rule (Parse::RecDescent::namespace000001::startrule)
+called
at 3.pl line 76.
##
##
defined $parser->another_rule($text)
or die "Text doesn't match";
##
##
word_list: word(s /,/)
word : m{ [^,]+ }xms
##
##
word_list: word(s /,/)
{
say Dumper(\@item);
}
word : m{ [^,]+ }xms
##
##
$VAR1 = [
'word_list',
[
'hello',
'world',
'goodbye',
'mars'
]
];
##
##
my $grammar = <<'END_OF_GRAMMAR';
{
use Data::Dumper;
use 5.012; #enable say()
}
delimited_or_list: word(s /or/)
{ say Dumper(\@item); }
word: m{ \S+ }xms
END_OF_GRAMMAR
my $text = "apple or strawberry or cherry";
my $parser = Parse::RecDescent->new($grammar)
or die "Bad grammar!\n";
defined $parser->delimited_or_list($text)
or die "Text doesn't match";
--output:--
$VAR1 = [
'delimited_or_list',
[
'apple',
'strawberry',
'cherry'
]
];
##
##
some_rule_name: 'hello' word(s /,/)
word : m{ [^,]+ }xms
##
##
$item{'word(s /,/)'}
##
##
$item[-1]
##
##
$VAR1 = [
'command_choices',
'commands->',
[
[
'',
'\'',
'go to',
'\'',
'',
'',
'',
''
],
[
'',
'\'',
'stop',
'\'',
'',
'',
'',
''
]
]
];
##
##
cmd_choices: 'commands->' quoted_string(s)
{
my @results = map { $_->[2] } @{$item[-1]};
say for @results;
}
quoted_string :
--output:--
go to
stop
##
##
use strict;
use warnings;
use 5.012;
use Parse::RecDescent;
$::RD_ERRORS = 1; #Parser dies when it encounters an error
$::RD_WARN = 1; #Enable warnings - warn on unused rules &c.
$::RD_HINT = 1; #Give out hints to help fix problems.
#$::RD_TRACE = 1; #Trace parsers' behaviour
our %RESULTS; #Need to declare the variable, but my variables
#won't be seen in another namespace. So declare
#a global variable with our().
my $grammar = <<'END_OF_GRAMMAR';
#Start up action(executed in parser namespace):
{
use 5.012; #enable say()
use Data::Dumper;
}
some_rule: name id
{
$main::RESULTS{names} = $item{name};
$main::RESULTS{phone_numbers} = $item{id};
}
name: m{ \S+ }xms
id: m{ \d+ }xms
END_OF_GRAMMAR
my $text = "Joe 10";
my $parser = Parse::RecDescent->new($grammar)
or die "Bad grammar!\n";
defined $parser->some_rule($text)
or die "Text doesn't match";
use Data::Dumper;
say Dumper(\%RESULTS);
#Do something with %RESULTS
--output:--
$VAR1 = {
'names' => 'Joe',
'phone_numbers' => '10'
};
##
##
my $text = <<'END_OF_TEXT';
{{ hello }}
END_OF_TEXT
my $grammar = <<'END_OF_GRAMMAR';
{
use 5.012;
use Data::Dumper;
}
#Declare some my() variables for use within the rule:
brace_block:
brace_block: lbrace(1..)
{
$lbraces = join '', @{$item[1]};
$rbraces = '}' x length $lbraces;
}
'hello'
"$rbraces"
{
say "$lbraces $item[3] $rbraces";
}
lbrace: / [{] /xms
END_OF_GRAMMAR
##
##
my $text = <<'END_OF_TEXT';
{{ hello }}
END_OF_TEXT
my $grammar = <<'END_OF_GRAMMAR';
{
use 5.012;
use Data::Dumper;
my $lbrace_count; #**DECLARE VARIABLE**
}
brace_block: lbrace(1..)
{
$lbrace_count = @{$item[1]}; #SET VARIABLE**
}
'hello'
rbraces
{
say "rbraces matched: $item{rbraces}";
}
lbrace: / [{] /xms
rbraces: / [}]{$lbrace_count} /xms #**INTERPOLATE VARIABLE**
END_OF_GRAMMAR