javascript - Debugging in Jison -
i'm using jison write parser. grammar:
{ "program": [ ["statements eof", "return $1;"] ], "statements": [ ["statement", "$$ = $1;"], ["statements statement", "$$ = $1 + '\\n' + $2;"] ], "statement": [ ["expression newline", "$$ = $1 + ';';"] ], "expression": [ ["number", "$$ = yytext;"], ["expression expression", "$$ = $1 + ', ' + $2;"] ] }
when run following error message:
conflict in grammar: multiple actions possible when lookahead token number in state 9 - reduce rule: expression -> expression expression - shift token (then go state 5) states conflicts: state 9 expression -> expression expression . #lookaheads= newline number expression -> expression .expression expression -> .number expression -> .expression expression
what supposed make of debug message? how explain message in simple english? period in expression -> expression expression .
mean? .expression
, .number
? how different expression
, number
respectively?
what supposed make of debug message?
a grammar conflict means parser can reach state follow multiple rules, doesn't have enough information determine 1 follow (or worse, grammar ambiguous). you'll have tweak grammar eliminate conflicts. often, takes practice right.
what period in expression -> expression expression . mean?
the period represents position of parser. so, in rule, parser have parsed 2 expressions, , in state 9. when period @ end of rule, means rule can "reduced", , grouped single expression
non-terminal in case. however, can reduce if next token (the lookahead) newline
or number
.
in expression -> .number
, parser has encountered number token, can "shift", move new state.
the conflict occurs because parser can reduce or shift when encounters number
token.
edit: resolve conflict, need split expression rule distinct non-terminals. having same non-terminal in sequence bound produce conflicts.
e.g.
{ "program": [ ["statements eof", "return $1;"] ], "statements": [ ["statement", "$$ = $1;"], ["statements statement", "$$ = $1 + '\\n' + $2;"] ], "statement": [ ["expression newline", "$$ = $1 + ';';"] ], "expression": [ ["expression expression_base", "$$ = $1 + ', ' + $2;"], ["expression_base", "$$ = $1;"] ], "expression_base": [ ["number", "$$ = yytext;"] ] }
here's nice resource more background on these types of grammars.
Comments
Post a Comment