我试图用 jison ( http://zaa.ch/jison/docs/ ) 编写简单的解析器,但停留在描述文本中。
%lex
%%
[\s\n\t]+ return 'TK_SPACE';
[0-9]+("."[0-9]+)?\b return 'TK_NUMBER';
[a-zA-Z]+([a-zA-Z0-9]+)?\b return 'TK_WORD';
<<EOF>> return 'EOF';
/lex
%start document
%%
document
: nodes EOF
{ console.log($1); }
| EOF
;
nodes
: nodes node
{ $1.push($2); $$ = $1; }
| node
{ $$ = [$1]; }
;
node
: text
;
text
: text text_element
{ $$ = $1 + $2; }
| text_element
;
text_element
: TK_NUMBER
| TK_WORD
| TK_SPACE
;
此语法编译时带有警告。
Conflict in grammar: multiple actions possible when lookahead token is TK_SPACE in state 5
- reduce by rule: node -> text
- shift token (then go to state 9)
Conflict in grammar: multiple actions possible when lookahead token is TK_WORD in state 5
- reduce by rule: node -> text
- shift token (then go to state 8)
Conflict in grammar: multiple actions possible when lookahead token is TK_NUMBER in state 5
- reduce by rule: node -> text
- shift token (then go to state 7)
States with conflicts:
State 5
node -> text . #lookaheads= TK_SPACE TK_WORD TK_NUMBER EOF
text -> text .text_element #lookaheads= EOF TK_NUMBER TK_WORD TK_SPACE
text_element -> .TK_NUMBER
text_element -> .TK_WORD
text_element -> .TK_SPACE
但是,如果我尝试解析文本,它就可以正常工作。这不是代码的完整版本,只是带有文本的版本。我想node
在特征中附加节点。