New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dumb question #1

Closed
sam0x17 opened this Issue Dec 1, 2018 · 4 comments

Comments

Projects
None yet
2 participants
@sam0x17

sam0x17 commented Dec 1, 2018

It's been a long time since I used CFGs, and I was wondering, what is the trick to get a production to allow itself to be repeated an arbitrary number of times? Everything that I try seems to create a shift/reduce conflict. For example, I want to be able to have a potentially infinite number of expressions separated by white-space, how would I do that with this grammar:

S = EXPRESSION;
EXPRESSION = NUMBER;
NUMBER = FLOAT | INTEGER;
FLOAT = "[\+\-]?[0-9]*[\.][0-9]+";
INTEGER = "[0-9]+";
WHITESPACE = "[ \t\n]+";

Thanks so much for making this btw

@sam0x17

This comment has been minimized.

sam0x17 commented Dec 1, 2018

e.g. I thought this would work but it creates a conflict:

EXPRESSION = NUMBER | EXPRESSION EXPRESSION;
@DanilaFe

This comment has been minimized.

Owner

DanilaFe commented Dec 1, 2018

Hi!
First of all, thanks for trying out Pegasus. I've reduced the issue to the following grammar:

S = A;
A = "a" | A A;

Intuitively, I don't see why this would be a shift/reduce conflict, either. However, I do know that this grammar is ambiguous, which may have something to do with it. I found that rewriting as

S = ALIST;
ALIST = A | A ALIST;
A = "a";

fixes the issue, though it does lead to more right-heavy parse trees (and adds an extra nonterminal to the mix).

Also, I notice that you have a WHITESPACE production that isn't used anywhere. I don't think this will work, as Pegasus currently doesn't have support for ignoring terminals (parsing will just always fail since there is no way to reduce the WHITESPACE production.)

This project is young, and I am currently swamped with finals, but the following features are planned:

  • Add proper support for ignoring tokens (like whitespace)
  • Add error output that is more descriptive than "shift/reduce conflict"
@sam0x17

This comment has been minimized.

sam0x17 commented Dec 2, 2018

@DanilaFe thanks that helps a lot. Yes I'm aware the WHITESPACE production isn't used, that's because I don't know how to write EXPRESSION to properly use it, but I'll follow your advice and see if that leads to something.

@sam0x17

This comment has been minimized.

sam0x17 commented Dec 2, 2018

that worked perfectly, thanks:

S = EXPRESSIONLIST;
EXPRESSIONLIST = EXPRESSION | EXPRESSION EXPRESSIONLIST;
EXPRESSION = NUMBER | WHITESPACE;
NUMBER = FLOAT | INTEGER;
FLOAT = "[\+\-]?[0-9]*[\.][0-9]+";
INTEGER = "[0-9]+";
WHITESPACE = "[ \t\n]+";

@sam0x17 sam0x17 closed this Dec 2, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment