question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OR rule follow a NonTerminal with OR rule will fail?

See original GitHub issue

start -> 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:

  1. [a]
  2. [b] but found: ‘c’] How can i solve this problem without modify my grammar.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
bd82commented, Aug 5, 2018

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.

0reactions
bd82commented, Aug 5, 2018

Looks like even when using a NOOP pattern there is still some performance regression.

    private checkStaticAnalysis() {
        if (this.selfAnalysisDone === false) {
            throw Error("Oops I did it again!")
        }
        else {
            this.checkStaticAnalysis = NOOP
        }
    }

I suppose the documentation would have to suffice for now.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found