zaach / jison

Bison in JavaScript.
http://jison.org
4.36k stars 450 forks source link

Jison fails to parse lex rules beginning with "-?" #264

Open wentaoshang opened 9 years ago

wentaoshang commented 9 years ago

Found this error when I tried to write a rule (in Flex/Bison format) to parse integers (both positive and negative):

%lex
%%

-?[0-9]+    return 'INT'

/lex

%start integer

%%

integer
    : INT EOF { return Number($1); }
    ;

I got an error when I tried to parse this file:

/usr/local/lib/node_modules/jison/lib/cli.js:170
        throw new Error('Could not parse jison grammar');
              ^
Error: Could not parse jison grammar
    at Object.processGrammars (/usr/local/lib/node_modules/jison/lib/cli.js:170:15)
    at processGrammar (/usr/local/lib/node_modules/jison/lib/cli.js:72:27)
    at processInputFile (/usr/local/lib/node_modules/jison/lib/cli.js:106:22)
    at Object.cliMain [as main] (/usr/local/lib/node_modules/jison/lib/cli.js:131:9)
    at Object.<anonymous> (/usr/local/lib/node_modules/jison/lib/cli.js:185:9)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

Currently I have to use "[-]?[0-9]+" to walk around this issue.

The Jison version I'm using is 0.4.15

nickbabcock commented 9 years ago

Have you tried:

%lex
%%

"-"?[0-9]+    return 'INT'

/lex

%start integer

%%

integer
    : INT <<EOF>> { return Number($1); }
    ;

From the documentation on lexical analysis:

[T]here is ... one minor inconvenience compared to Flex patterns, namely exact string patterns must be placed in quotes.

Does this solve your problem?

wentaoshang commented 9 years ago

Yes, this one works. But it is equally annoying as writing the square brackets.

It is a little surprising that patterns like

a?[0-9]+

works fine for jison without the need to quote. The problem only occurs with a certain set of punctuators like -. That's why I suspect there is bug (or a feature?)