Hello, I'm having trouble figuring out how to express my grammar and was hoping someone could help. I've tried rewriting various ways and looking for some options that might change behavior, but I haven't been able to figure it out.
I have a language with variable assignment and simple commands and doesn't care about whitespace. So,
PAx=42 # variable assignment to "PAx"
PAx # PA command with argument x
I have a grammar, but it insists on spaces after command names. I've tried hiding assignment behind a prioritized rule and tried setting the command lexeme priority, but I always get parse errors when parsing "PAx". I have a simplified grammer which exhibits the issue,
#!/usr/bin/perl
use warnings; use strict; use 5.028;
use Marpa::R2 8.000000;
use Data::Dumper;
my $grammar = 'Marpa::R2::Scanless::G'->new({ source => \(<<'RULES') });
:default ::= action => [name,values]
lexeme default = latm => 1
:start ::= Program
Program ::= Statement+
Statement ::= Command terminator
|| Assign terminator
Command ::= command arg
Assign ::= variable equal value
# Doesn't help: :lexeme ~ command priority => 2
command ~ 'PA' | 'PR'
arg ~ [\w]+
equal ~ '='
value ~ [0-9]+
variable ~ [\w]+
terminator ~ [;\n]
:discard ~ whitespace
whitespace ~ [ \t]+
RULES
# This parses correctly, line 2 is a command, line 3 is assignment.
my $ok = <<TEXT;
x=23
PA x
PAx=42
TEXT
say Dumper($grammar->parse(\$ok));
# I want to generate same tree as above,
# but my grammar wants line 2 to be an assignment.
my $bad = <<TEXT;
x=23
PAx
PAx=42
TEXT
# Error in SLIF parse: No lexeme found at line 2, column 4
say Dumper($grammar->parse(\$bad));
Is there some trick to this? Did I miss someting in the documentation?
Any suggestions?
Thanks!
- Dean