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

Lexical error when parsing Integer values #360

Open
JanPeter opened this issue Sep 7, 2017 · 6 comments
Open

Lexical error when parsing Integer values #360

JanPeter opened this issue Sep 7, 2017 · 6 comments

Comments

@JanPeter
Copy link

JanPeter commented Sep 7, 2017

I'm currently struggling with a strange problem. I want to parse the following snipped:

  type User {
    name: String = "Ignatz"
    loggedIn: Boolean = false
  }

  type Task {
    done: Boolean = true
    priority: Number = 54
  }

When I create my grammar and try to parse this snipped, I get the following error message:

[Error: Lexical error on line 9. Unrecognized text.
    ... priority: Number = 54      }
    -----------------------^]

The funny thing is, if I paste my grammar into a tool like this and choose a LRLA(1) grammar as parser type i get no errors. That's the way I create my grammar on my local machine:

./node_modules/.bin/jison --file src/grammar.json -p lalr -o src/grammar.js

That's my grammar as json:

{
  "lex": {
    "macros": {
      "digit": "[0-9]",
      "esc": "\\\\",
      "int": "-?([0-9]|[1-9][0-9]+)",
      "exp": "(?:[eE][-+]?[0-9]+)",
      "frac": "(?:\\.[0-9]+)"
    },
    "rules": [
      ["\\s+", "/* skip whitespace */"],
      ["type", "return 'TYPE'"],
      ["implements", "return 'IMPLEMENTS'"],
      ["{int}{frac}?{exp}?\\b", "return 'NUMBER';"],
      ["\"(?:{esc}[\"bfnrt/{esc}]|{esc}u[a-fA-F0-9]{4}|[^\"{esc}])*\"", "yytext = yytext.substr(1,yyleng-2); return 'STRING';"],
      ["(false|true)", "return 'BOOLEAN'"],
      ["(String|Number|Boolean)", "return 'SCALAR'"],
      ["[a-zA-Z]+", "return 'WORD'"],
      ["\\{", "return '{'"],
      ["\\}", "return '}'"],
      ["\\[", "return '['"],
      ["\\]", "return ']'"],
      ["\\(", "return '('"],
      ["\\)", "return ')'"],
      [":", "return ':'"],
      [",", "return ','"],
      [";", "return ';'"],
      ["!", "return '!'"],
      ["=", "return '='"],
      ["#", "return '#'"],
      ["->", "return '->'"],
      ["<-", "return '<-'"]
    ]
  },

  "bnf": {
    "Schema": [["ModelList", "return $1;"]],
    "ModelList": [["Model", "$$ = [ $1 ];"],
                  ["ModelList Model", "$$ = $1; $1.push($2)"]],
    "Model": [["ModelHeader ModelProperties", "$$ = $1; $1.properties = $2;"],
              ["ModelHeader ModelProperties ;", "$$ = $1; $1.properties = $2;"]],
    "ModelHeader": [["ModelName", "$$ = { name: $1 };"],
                    ["ModelName IMPLEMENTS WORD", "$$ = { name: $1, implements: $3 };"]],
    "ModelName": [["TYPE WORD", "$$ = $2;"]],
    "ModelProperties": [["{ }", "$$ = [];"],
                        ["{ PropertyList }", "$$ = $2;"]],
    "PropertyList": [["Property", "$$ = []; $$.push($1);"],
                     ["PropertyList Property", "$$ = $1; $1.push($2);"]],
    "Property": [[ "WORD : PropertyType", "$$ = $3; $3.name = $1;" ],
                 [ "WORD : PropertyType ,", "$$ = $3; $3.name = $1;" ],
                 [ "WORD : PropertyType ;", "$$ = $3; $3.name = $1;" ]],
    "Value": [["STRING", "$$ = $1;"],
              ["NUMBER", "$$ = Number(yytext);"],
              ["BOOLEAN", "$$ = $1;"]],
    "PropertyTypeScalar": [["SCALAR", "$$ = { type: $1, scalar: true };"],
                           ["SCALAR !", "$$ = { type: $1, scalar: true, required: true };"]],
    "PropertyTypeScalarValue": [[ "PropertyTypeScalar = Value", "$$ = $1; $1.value = $3" ]],
    "PropertyType": [["PropertyTypeScalar", "$$ = $1"],
                     ["PropertyTypeScalarValue", "$$ = $1"],
                     ["WORD PropertyRelation", "$$ = $2; $2.model = $1;"],
                     ["[ WORD ] PropertyRelation", "$$ = $4; $4.model = $2; $4.many = true;"]],
    "PropertyRelation": [[ "# <-", "$$ = { type: 'Relation', direction: 'from' };" ],
                         [ "# ->", "$$ = { type: 'Relation', direction: 'to' };" ],
                         [ "# <- ( WORD )", "$$ = { label: $4, type: 'Relation', direction: 'from' };" ],
                         [ "# -> ( WORD )", "$$ = { label: $4, type: 'Relation', direction: 'to' };" ]]
  }
}

I'm using the latest jison version 0.4.18

GerHobbelt added a commit to GerHobbelt/jison that referenced this issue Sep 12, 2017
GerHobbelt added a commit to GerHobbelt/jison that referenced this issue Sep 12, 2017
@GerHobbelt
Copy link
Contributor

Hm, odd. This does work with my own jison 0.6.0-191 (npm: jison-gho).

You might want to go and have a look at the internals, see what jison generated as code for your lexer, in particular the table with the regexes for your lexer rules. (The lexer is always the bottom half of the generated js parser/lexer source code file; the lexer tables are near the bottom of the file.)

@JanPeter
Copy link
Author

I ended up switching to nearleyjs which works fine for me. So a contributor can decide if he wants to close this issue, or try to fix it.

Just curious, why did you chose to fork this library instead of contributing to it?

@GerHobbelt
Copy link
Contributor

GerHobbelt commented Sep 13, 2017 via email

@GerHobbelt
Copy link
Contributor

Thanks for the https://github.com/Hardmath123/nearley reference, by the way.

@JanPeter
Copy link
Author

Well, thanks for that detailed explanation! I would've also taken that route 👍
Wish I knew about your fork before rewriting my grammar to this nearley syntax...

@metasansana
Copy link

Wow, thanks for this discussion guys. I have been wondering about your fork for a while @GerHobbelt. Just installed it and trying it out in a few existing projects.

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

3 participants