2

Problem in parsing/identifying double quoted string from the big expression.

use strict;
use Marpa::R2;
use Data::Dumper;

my $grammar = Marpa::R2::Scanless::G->new({
    default_action => '[values]',
    source => \(<<'END_OF_SOURCE'),

:start ::= expression

expression ::= expression OP expression
expression ::= expression COMMA expression
expression ::= func LPAREN PARAM RPAREN
expression ::= PARAM
PARAM ::= STRING | REGEX_STRING

:discard    ~ sp
sp          ~ [\s]+

COMMA                      ~ [,]
STRING                     ~ [^ \/\(\),&:\"~]+
REGEX_STRING               ~ yet to identify
OP                         ~ ' - ' | '&'
LPAREN                     ~ '('
RPAREN                     ~ ')'
func                       ~ 'func'

END_OF_SOURCE
});

my $recce = Marpa::R2::Scanless::R->new({grammar => $grammar});

my $input1 = "func(foo)&func(bar)"; -> able to parse it properly by parsing foo and bar as STRING LEXEME.

my $input2 = "\"foo\""; -> Here, I want to parse foo as regex_string LEXEME. REGEX_STRING is something which is enclosed in double quotes.

my $input3 = "func(\"foo\") - func(\"bar\")"; -> Here, func should be taken as func LEXEME, ( should be LPAREN, ) should be RPAREN, foo as REGEX_STRING, - as OP and same for func(\"bar\")

my $input4 = "func(\"foo\")"; -> Here, func should be taken as func LEXEME, ( should be LPAREN, ) should be RPAREN, foo as REGEX_STRING

print "Trying to parse:\n$input\n\n";
$recce->read(\$input);
my $value_ref = ${$recce->value};
print "Output:\n".Dumper($value_ref);

What did i try : 1st method: My REGEX_STRING should be something : REGEX_STRING -> ~ '\"([^:]*?)\"'

If i try putting above REGEX_STRING in the code with input expression as my $input4 = "func(\"foo\")"; i get error like :

Error in SLIF parse: No lexeme found at line 1, column 5 * String before error: func( * The error was at line 1, column 5, and at character 0x0022 '"', ... * here: "foo") Marpa::R2 exception

2nd method:

Tried including a rule like :

PARAM ::= STRING | REGEX_STRING
REGEX_STRING ::= '"' QUOTED_STRING '"'

STRING ~ [^ \/\(\),&:\"~]+
QUOTED_STRING ~ [^ ,&:\"~]+

The problem here is-> Input is given using:

my $input4 = "func(\"foo\")";

So, here it gives error because there are now two ways to parse this expression, either whole thing between double quotes which is func(\"foo\") is taken as QUOTED_STRING or func should be taken as func LEXEME and so on.

Please help how do i fix this thing.

zubug55
  • 729
  • 7
  • 27

2 Answers2

1
use 5.026;
use strictures;
use Data::Dumper qw(Dumper);
use Marpa::R2 qw();

my $grammar = Marpa::R2::Scanless::G->new({
    bless_package => 'parsetree',
    source        => \<<'',
:default ::= action => [values] bless => ::lhs
lexeme default = bless => ::name latm => 1
:start ::= expression
expression ::= expression OP expression
expression ::= expression COMMA expression
expression ::= func LPAREN PARAM RPAREN
expression ::= PARAM
PARAM ::= STRING | REGEXSTRING
:discard    ~ sp
sp          ~ [\s]+
COMMA           ~ [,]
STRING          ~ [^ \/\(\),&:\"~]+
REGEXSTRING     ::= '"' QUOTEDSTRING '"'
QUOTEDSTRING    ~ [^ ,&:\"~]+
OP              ~ ' - ' | '&'
LPAREN          ~ '('
RPAREN          ~ ')'
func            ~ 'func'

});
# say $grammar->show_rules;

for my $input (
    'func(foo)&func(bar)', '"foo"', 'func("foo") - func("bar")', 'func("foo")'
) {
    my $r = Marpa::R2::Scanless::R->new({
        grammar => $grammar,
#         trace_terminals => 1
    });
    $r->read(\$input);
    say "# $input";
    say Dumper $r->value;
}
daxim
  • 39,270
  • 4
  • 65
  • 132
  • In my question I have mentioned that in the second method I tried PARAM ::= STRING | REGEX_STRING REGEX_STRING ::= '"' QUOTED_STRING '"' STRING ~ [^ \/\(\),&:\"~]+ QUOTED_STRING ~ [^ ,&:\"~]+ ; which is similar to what you are doing – zubug55 Mar 22 '18 at 18:52
  • i understood the problem; the problem is, I have included rules like : PARAM ::= STRING | REGEX_STRING REGEX_STRING ::= '"' QUOTED_STRING '"' STRING ~ [^ \/\(\),&:\"~]+ QUOTED_STRING ~ [^ \/,&:\"~]+ ; here regex of STRING and QUOTED_STRING is different, then only it gives error – zubug55 Mar 22 '18 at 19:05
  • If i replace QUOTED_STRING with STRING; it will not give error – zubug55 Mar 22 '18 at 19:06
  • Can you suggest how do i resolve this? My regex of STRING and Quoted_STRING has to be different only – zubug55 Mar 22 '18 at 19:06
  • PLz help on this question: https://stackoverflow.com/questions/50108574/parse-single-quoted-string-using-marpar2-perl – zubug55 Apr 30 '18 at 21:39
0

2nd method posted in question worked for me. I just have to include :

lexeme default = latm => 1

in my code.

zubug55
  • 729
  • 7
  • 27