OR rule follow a NonTerminal with OR rule will fail?
See original GitHub issuestart -> A | B A -> a | b B -> c | d | e
const {createToken, Lexer, Parser, EOF} = require('chevrotain');
const util = require('util');
const a = createToken({name: 'a', pattern: /a/});
const b = createToken({name: 'b', pattern: /b/});
const c = createToken({name: 'c', pattern: /c/});
const d = createToken({name: 'd', pattern: /d/});
const e = createToken({name: 'e', pattern: /e/});
let allTokens = [
a, b, c, d, e, EOF
];
class UserParser extends Parser {
constructor(input) {
super(input, allTokens);
const $ = this;
$.RULE('start', () => {
return $.OR([
{ALT: () => $.SUBRULE($.A)},
{ALT: () => $.SUBRULE($.B)}
]);
});
$.RULE('A', () => {
return $.OR([
{ALT: () => $.CONSUME(a)},
{ALT: () => $.CONSUME(b)}
]);
});
$.RULE('B', () => {
return $.OR([
{ALT: () => $.CONSUME(c)},
{ALT: () => $.CONSUME(d)},
{ALT: () => $.CONSUME(e)}
]);
});
}
}
let lexer = new Lexer(allTokens);
const parser = new UserParser([]);
// ONLY ONCE
function parseInput(text) {
const lexingResult = lexer.tokenize(text);
parser.input = lexingResult.tokens;
let ast = parser.start();
if (parser.errors.length > 0) {
for (let i = 0; i < parser.errors.length; i++) {
console.log(parser.errors[i]);
}
}
return ast;
}
const inputText = "c";
let json = parseInput(inputText);
console.log(util.inspect(json, {depth: null, colors: "auto"}));
Error MESSAGE: [NoViableAltException: Expecting: one of these possible Token sequences:
- [a]
- [b] but found: ‘c’] How can i solve this problem without modify my grammar.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Bison - nonterminal useless in grammar - Stack Overflow
Bison doesn't require any specific order for the rules. With exception of the first rule that has the privilege to be the one...
Read more >Necessary and sufficient conditions for LL(1) - Calgary
the rule which should be chosen when developing a nonterminal must be determined by that nonterminal and the (at most) next token on...
Read more >Lecture 11, Start and Follow Sets, LR1 Parsers
This rule tells us nothing about the set of following symbols for <a>. On the other hand, regardless of whether b, c, and...
Read more >Lexical and Syntax Analysis - TINMAN
parsing subprogram for the following rule would cause infinite recursion: ... In many cases, a grammar that fails the pairwise disjointness test can...
Read more >Parser Rules - antlr/antlr4 - GitHub
A semantic predicate that evaluates to false during prediction renders the surrounding alternative nonviable. Prediction occurs when a rule is predicting which ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It seems you forgot to call this.performSelfAnalysis() See: https://github.com/SAP/chevrotain/blob/master/examples/grammars/json/json.js#L107-L110
I am leaving this issue open to think again if I can detect the lack of this call and produce a clear error message without suffering from a performance penalty.
Looks like even when using a NOOP pattern there is still some performance regression.
I suppose the documentation would have to suffice for now.