0
votes

I'm writing parser for Python-like language, which allows to pass two types of arguments (positional and named) to functions. And, like in Python, named argument must be passed after positional. I wrote a grammar for it, but it has shift/reduce conflicts, and I can't even imagine how to write it another way.

Here's my grammar:

optionalcomma : COMMA
              | empty

arguments : posargs
         | posargs COMMA kwargs
         | kwargs
         | empty

posargs : optionalnl languageitem
        | optionalnl languageitem COMMA posargs

kwargs : optionalnl varassign optionalcomma
       | optionalnl varassign COMMA kwargs

Here, optionalnl is optional newline, languageitem is basic object that can be passed as positional argument, and varassign is variable assignment rule, which is equal to passing a named argument to function. The parser goes through arguments to first comma, and doesn't know which item (kwarg or posarg) will follow the comma; that's the problem. And I completely stuck here, can't write the correct grammar.

I'm using LALR(1) parser PLY, so advice to use GLR parsing won't help me much.

1

1 Answers

1
votes

Unless there is something unusual in the rest of your grammar, writing posargs in the usual left-recursive fashion will not produce a shift/reduce conflict:

posargs : optionalnl languageitem
        | posargs ',' optionalnl languageitem

Personally, I'd write kwargs left-recursively as well.