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

Exception thrown trying to use parser generated by 0.4.14 #246

Closed
markwatkinson opened this issue Sep 14, 2014 · 27 comments
Closed

Exception thrown trying to use parser generated by 0.4.14 #246

markwatkinson opened this issue Sep 14, 2014 · 27 comments

Comments

@markwatkinson
Copy link

I'm using Jison 0.4.14 to try to generate a parser for this grammar, which is created using this script. The parser is generated, but warns of two conflicts (which I don't think should affect this, but I may be wrong).

Then trying to use it in a Node (v0.10.25) REPL, I'm getting:

> var p = require('./parser.js').parser
undefined
> p
{ yy: {} }
> p.parse("1")
TypeError: Object prototype may only be an Object or null
    at Function.create (native)
    at Object.parse (/home/mark/projects/loljs/parser.js:312:24)
    at repl:1:4
    at REPLServer.self.eval (repl.js:110:21)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.EventEmitter.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)
    at ReadStream.onkeypress (readline.js:99:10)

The line it's referencing (parser.js:312) is

var lexer = Object.create(this.lexer);

and this.lexer is undefined. I can't see any other reference to this.lexer anywhere in the file.

Here's the generated parser: https://gist.github.com/markwatkinson/c211a15e08d43c71e447

0.4.15 hits the same problem.
0.4.13 seems to work ok

davidagraf added a commit to davidagraf/doi2bib that referenced this issue Sep 25, 2014
@davidagraf
Copy link

Same problem here. Because I am using jison via gulp-jison, created a temp fork of gulp-jison with an updated jison dependency: https://github.com/davidagraf/gulp-jison

@rogierschouten
Copy link

+1

2 similar comments
@disjukr
Copy link

disjukr commented Oct 18, 2014

+1

@highwaycoder
Copy link
Contributor

+1

@flyon
Copy link

flyon commented Dec 9, 2014

excact same problem here, but using grunt, not gulp.
trying to include and use a generated parser resolves in an error at "var lexer = Object.create(this.lexer);"
I have now reverted to 0.4.13 and that indeed solves the problem for me, for now.

@JamesMessinger
Copy link

+1

@flipchart
Copy link

Same problem on 0.4.15 as well. Using module-deps and browser-pack (components of browserify) to do custom bundling, but not entirely sure that's the cause

@nolanlawson
Copy link
Contributor

Ran into this myself today. I can try to find a fix. Unless someone else has already fixed it, and it's just hanging out in a fork somewhere?

@nolanlawson
Copy link
Contributor

OK, I found a fix ^

lukasz-karolewski pushed a commit to lukasz-karolewski/jison that referenced this issue May 31, 2015
Fix this.lexer undefined, fixes zaach#246
@zaach zaach closed this as completed in 7bcf8b7 Aug 18, 2015
zaach added a commit that referenced this issue Aug 18, 2015
Fix this.lexer undefined, fixes #246
@Blackhol3
Copy link

I still have the same problem.

@mezzario
Copy link

me as well, 0.4.15

@Blackhol3
Copy link

@mezzario I've discovered the problem was, at least for me, that no lexer is included by default. I've personally written a simple one myself, as described in the documentation, but I guess you can also simply add the following code at the beginning of your grammar file:

%lex
%%
/lex

@pietersv
Copy link

pietersv commented Oct 9, 2015

Aha, a trivial lexer didn't work for the more complex examples, needed to create an actual lexer.

@cosmicexplorer
Copy link

Pretty much all the documentation here implied that we should use (snipped from docs):

var parser = require("./calculator").parser;
console.log(parser.parse("4 * 5")); // => TypeError

where ./calculator.js is the exported javascript output from jison for the calculator grammar described in the docs. This is wrong and was causing the error message shown in this thread. Instead, just calling .parse works:

var parse = require("./calculator").parse;
console.log(parse("4 * 5")); // => 20

I don't know why this works (didn't spend too much time looking into it), but it probably has something to do with the definition

exports.parse = function () { return parser.parse.apply(parser, arguments); };

at the bottom of the generated file, and the difference from parser.parse (maybe .parse() isn't part of parser's prototype? idk).

@gentwo
Copy link

gentwo commented Jan 5, 2016

The problem seems to be that when the parser is generated using code, the lexer does not get generated. Not sure if this is by design.

To elaborate:

jison.Parser.generate

Doesn't appear to generate the lexer at all, even though the rules are there in the grammar file.
Hence lexer is undefined. The following lines

/* generated by jison-lex 0.3.4 */
var lexer = (function(){
[...]
return lexer;
})();
parser.lexer = lexer;

simply don't exist in the generated parser.

Using the command line, it seems to work fine. i.e.,

$ jison grammar.jison

Does generate the lexer.

Both methods of accessing the parser. ie

var parser = require("./calculator").parser;
console.log(parser.parse("4 * 5")); // => TypeError

and

var parse = require("./calculator").parse;
console.log(parse("4 * 5")); // => 20

works for me when using the parser generated from the command line.

@cosmicexplorer
Copy link

You're right, both seem to work now. Not sure what silly thing I was doing then.

@gentwo
Copy link

gentwo commented Jan 6, 2016

Any idea what I'm doing wrong that's causing the lexer to not be generated? Do I need to pass some additional options? I am calling it like this:

var fs = require("fs");
var jison = require("jison");

var grammar = fs.readFileSync("grammar.jison", "utf8");
var parser = new jison.Parser(grammar);

// generate source, ready to be written to disk
var parserSource = parser.generate({moduleName: "myParser"});

@yosbelms
Copy link

yosbelms commented Jan 6, 2016

@gentwo I'm generating with the following code:

    var
    fs      = require('fs'),
    bnf     = require('jison/node_modules/ebnf-parser'),
    lex     = require('jison/node_modules/lex-parser'),
    grammar = bnf.parse(fs.readFileSync('./src/bnf/cor.y', 'utf8')),
    lexer   = lex.parse(fs.readFileSync('./src/bnf/cor.l', 'utf8'));

    grammar.lex = lexer;
    gen         = new jison.Generator(grammar, opts);
    parserSrc   = gen.generate(opts);

    fs.writeFileSync('./src/parser.js', parserSrc);

you can find the complete source code in https://github.com/yosbelms/cor/blob/master/make starting from line 102

@gentwo
Copy link

gentwo commented Jan 11, 2016

@yosbelms Awesome! Thanks! That's exactly what I was looking for. This means I need to separate my grammar and lexing rules :-(

Also, is this the expected use or is this a bug?

@mbroadst
Copy link

@Blackhol3 Yeah I realized the err of my ways shortly after writing the comment, and therefore deleted it. Do you happen to know of an existing ansi c lexer written for jison? I'm going through an existing lex spec for it, but I'm sure I'll screw it up the first time here :)

@Blackhol3
Copy link

@mbroadst No, I don't know any, sorry. But lexers can generally be relatively easily written by yourself, if necessary :-).

@mbroadst
Copy link

mbroadst commented Oct 10, 2016

@pietersv just when I finished my own :) thanks for the resource!

This seems to be taken mostly from the ANSI C lex grammar provided here, and I think is incompatible with jison. I'm unclear how to implement some of those functions, but I've converted the majority to reflect the examples provided in the repo here. I'll commit it back if it ends up working.

@irwansyahwii
Copy link

Still happening on 0.4.14 and above also some versions below. Why someone closed the issue since I can't see any working solution from the above proposed solutions

@cosmicexplorer
Copy link

How are you generating the parser and what is the error message?

@irwansyahwii
Copy link

@cosmicexplorer I generated the parser using this:

$ jison grammar.jison

And I got lexer undefined error message. I tried using the JavaScriptCore grammar from the examples

@Blackhol3
Copy link

@irwansyahwii The root of problem is stated clearly in numerous messages of this thread: you should define a lexer, either inside your .jison file, or with a custom scanner.

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