I am trying to get a handle on jison, which is a javascript implementation of Bison. My specific language I am trying to parse looks like this:
foo(10)
bar()
foo(20)
baz()
I want to parse this into something like:
return [
{ func: 'foo', arg: 10 },
{ func: 'bar' },
{ func: 'foo', arg: 20 },
{ func: 'baz' }
]
So far, my jison definition looks like this:
var grammar = {
'lex': {
'rules': [
['\\s+', '/* skip whitespace */'],
['[0-9]+\\b', 'return "INTEGER";'],
['\\(', 'return "OPEN_PAREN"'],
['\\)', 'return "CLOSE_PAREN"'],
['[\\w]+\\s*(?=\\()', 'return "FUNC_NAME"'],
['$', 'return "LINE_END"']
]
},
'bnf': {
"expression": [["e LINE_END", "return $1;"]],
"e": [
["FUNC_NAME OPEN_PAREN INTEGER CLOSE_PAREN", "$$ = { func: $1, arg: $3 };"],
["FUNC_NAME OPEN_PAREN CLOSE_PAREN", "$$ = { func: $1 };"]
]
}
};
When I run this, I get this error:
Error: Parse error on line 1:
forward(10)turnLeft()forward(2
-----------^
Expecting 'LINE_END', got 'FUNC_NAME'
at Parser.parseError (eval at createParser (/Users/user/project/node_modules/jison/lib/jison.js:1327:13), <anonymous>:30:21)
at Parser.parse (eval at createParser (/Users/user/project/node_modules/jison/lib/jison.js:1327:13), <anonymous>:97:22)
at Object.<anonymous> (/Users/user/project/generate-parser.js:39:20)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
at startup (bootstrap_node.js:193:16)
Clearly, I am not understanding something to do with line endings separating individual statements. I've looked at the examples for jison and I've read and re-read the Bison docs but still do not fully understand the "Semantic Actions" aspect.
What am I missing here? In my mind, I have defined e
in two terminal forms, and the nonterminal form e LINE_END
.
Any help would be greatly appreciated, thanks!