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

Maximum Call Stack Size Exceeded while parsing custom grammar #10

Open
fabianfreyer opened this issue Apr 30, 2019 · 1 comment
Open

Comments

@fabianfreyer
Copy link

fabianfreyer commented Apr 30, 2019

Parsing the following grammar:

RValue ::= Number | InfixExpression
InfixExpression ::= RValue InfixOperator RValue
InfixOperator ::= "+" | "-" | "*" | "/"
Number ::= "-"? ("0" | [1-9] [0-9]*) ("." [0-9]+)? (("e" | "E") ( "-" | "+" )? ("0" | [1-9] [0-9]*))?

Fails with the following error message:

/home/user/service-xsharp/service/node_modules/ebnf/dist/Parser.js:53
        name: name.replace(decorationRE, '').replace(preDecorationRE, ''),
                   ^

RangeError: Maximum call stack size exceeded
    at String.replace (<anonymous>)
    at parseRuleName (/home/user/service-xsharp/service/node_modules/ebnf/dist/Parser.js:53:20)
    at options.forEach.phases (/home/user/service-xsharp/service/node_modules/ebnf/dist/Parser.js:256:47)
    at Array.forEach (<anonymous>)
    at Parser.parse (/home/user/service-xsharp/service/node_modules/ebnf/dist/Parser.js:233:25)
    at options.forEach.phases (/home/user/service-xsharp/service/node_modules/ebnf/dist/Parser.js:282:51)
    at Array.forEach (<anonymous>)
    at Parser.parse (/home/user/service-xsharp/service/node_modules/ebnf/dist/Parser.js:233:25)
    at options.forEach.phases (/home/user/service-xsharp/service/node_modules/ebnf/dist/Parser.js:282:51)
    at Array.forEach (<anonymous>)
@menduz
Copy link
Member

menduz commented May 1, 2019

In this case we have an invalid state machine for the parser, but the error isn't happy at all.

RValue -> InfixExpression -> RValue -> InfixExpression -> RValue -> InfixExpression ...

That is called left recursion in parsers, and this generator doesn't support it.

Though, you can reconfigure your grammar:

{ ws=explicit }

InfixExpression ::= RValue (InfixOperator RValue)* {simplifyWhenOneChildren=true}
RValue ::= Number WS*
InfixOperator ::= WS* ("+" | "-" | "*" | "/") WS*
Number ::= "-"? ("0" | [1-9] [0-9]*) ("." [0-9]+)? (("e" | "E") ( "-" | "+" )? ("0" | [1-9] [0-9]*))?

WS ::= ' '

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