Skip to content
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

Can't parse a certain grammar #14

Open
cbeust opened this issue Jun 21, 2019 · 2 comments
Open

Can't parse a certain grammar #14

cbeust opened this issue Jun 21, 2019 · 2 comments

Comments

@cbeust
Copy link

cbeust commented Jun 21, 2019

I'm trying to use cakeparse to write the grammar for my shell, https://github.com/cbeust/kash and I can't seem to define a simple grammar to allow either "and" commands:

a && b && c

or "pipe" commands:

a | b | c

Here is what I have, in short:

        val andCommand = singleCommand and zeroOrMore(andAnd and singleCommand)
        val pipeCommand = singleCommand and zeroOrMore(pipe and singleCommand)
        val goal = andCommand or pipeCommand

With that goal, I can only parse one of these two lines but never both.

Am I missing something?

@sargunv
Copy link
Owner

sargunv commented Jun 22, 2019

Sounds like you want to parse something like echo hello && echo world | cat? You'd do something like:

val op = andAnd or pipe
val goal = singleCommand and zeroOrMore(op and singleCommand)

If you want to have the concept of operator precedence, a slightly more complex solution is required. The test module contains a calculator grammar that implements operator precedence (+-*/ operators, also parens): https://github.com/sargunv/cakeparse/blob/master/src/test/kotlin/me/sargunvohra/lib/cakeparse/Calc.kt

Btw, I should warn you that I've only used this library for small toy projects, so it's not very well battle tested. If you do decide to continue to use it anyway, I'd love to hear your feedback and experiences with it.

@sargunv
Copy link
Owner

sargunv commented Jun 22, 2019

On second reading, if you mean parsing something like:

a && b && c
a  | b  | c

but not something like

a && b | c

then the grammar would look like

val andCommand = singleCommand and zeroOrMore(andAnd and singleCommand)
val pipeCommand = singleCommand and zeroOrMore(pipe and singleCommand)
val command = (andCommand or pipeCommand) before terminator
val goal = zeroOrMore(command)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants