Subject: | LaTeX example requires space in front of a command |
I tried the example rule set for simple LaTeX parsing:
my $parser =
qr{
<File>
<rule: File> <[Element]>*
<rule: Element> <Command> | <Literal>
<rule: Command> \\ <Literal> <Options>? <Args>?
<rule: Options> \[ <[Option]> ** (,) \]
<rule: Args> \{ <[Element]>* \}
<rule: Option> [^][\$&%#_{}~^\s,]+
<rule: Literal> [^][\$&%#_{}~^\s]+
}xms;
It doesn't work on a string like '\ref{A}-\ref{B}'. According to my
understanding, this should be parsed into a command '\ref{A}', a literal
'-' and another command '\ref{B}'. However, Regexp::Grammars seems not
to be able to split '-\ref{B}' and chokes. It works correctly if I
insert a space: '\ref{A}- \ref{B}', but according to the rules, this
should not be necessary. My test script together with its output are
attached.
Thanks for Regexp::Grammars and best wishes
Lutz
Subject: | regexp_grammars_bug.pl |
#!/usr/bin/perl
use strict;
use warnings;
use Regexp::Grammars;
use Data::Dumper;
$Data::Dumper::Maxdepth = 4;
my $parser =
qr{
<File>
<rule: File> <[Element]>*
<rule: Element> <Command> | <Literal>
<rule: Command> \\ <Literal> <Options>? <Args>?
<rule: Options> \[ <[Option]> ** (,) \]
<rule: Args> \{ <[Element]>* \}
<rule: Option> [^][\$&%#_{}~^\s,]+
<rule: Literal> [^][\$&%#_{}~^\s]+
}xms;
'\ref{A}-\ref{B}' =~ $parser;
print Dumper(\%/);
'\ref{A}- \ref{B}' =~ $parser;
print Dumper(\%/);
__END__
# the output I get is this:
$VAR1 = {
'' => '\\ref{A}-\\ref',
'File' => {
'' => '\\ref{A}-\\ref',
'Element' => [
{
'' => '\\ref{A}',
'Command' => 'HASH(0x849d354)'
},
{
'' => '-\\ref',
'Literal' => '-\\ref'
}
]
}
};
$VAR1 = {
'' => '\\ref{A}- \\ref{B}',
'File' => {
'' => '\\ref{A}- \\ref{B}',
'Element' => [
{
'' => '\\ref{A}',
'Command' => 'HASH(0x849d4a4)'
},
{
'' => '-',
'Literal' => '-'
},
{
'' => ' \\ref{B}',
'Command' => 'HASH(0x847abf4)'
}
]
}
};