zaach / jison

Bison in JavaScript.
http://jison.org
4.33k stars 448 forks source link

CustomScanner Unexpected EOF #362

Closed DominikGri closed 6 years ago

DominikGri commented 6 years ago

Hi, i uses the Custom Scanner "AlphabetScanner" from the documentation.

var Parser = require("jison").Parser;

function AlphabetScanner() {
 /* ... */
};

var parser = new Parser(grammar);
parser.lexer = new AlphabetScanner();
console.log('parse: ', parser.parse('Teststring'));

But i always get "Error: Parse error on line NaN: Unexpected 'EOF'"!

GerHobbelt commented 6 years ago

Which jison version are you using?

DominikGri commented 6 years ago

Version ^0.4.18

GerHobbelt commented 6 years ago

If you try this with jison-gho, it works. (npm: https://www.npmjs.com/package/jison-gho, github: https://github.com/GerHobbelt/jison)

Note that the jison-gho fork can be more strict when it comes to custom lexers: see that package's examples:


If OTOH you wish to work with vanilla jison (0.4.x), you might want to go and code-inspect and/or debug=step through the generated parser source code.

Two things jump out from your OP:

  1. NaN for the line number is probably due to the custom lexer not tracking a yylineno member value (insert handwave as I haven't checked vanilla parser kernel code whether it uses the yylloc or yylineno!)

  2. 'unexpected EOF' MAY also be due to a mismatch between your grammar and the lexed input: if your grammar doesn't expect an EOF when the lexer spits one out, this is exactly the error message to expect.

GerHobbelt commented 6 years ago

(See the jison-gho example jison files mentioned above for a working sample grammar which should accept your test input ("Teststring"))