0

我已经有一段时间没有使用 Flex 或 Bison 完成任何工作,回到大学。但是,我一直在尝试使用 Jison 推出我自己的轻量级 BBCode 解析器,以此作为周末的尝试。

我的问题涉及一个奇怪的问题,我被告知解析器正在寻找一个它没有找到先决条件令牌的令牌。我不认为我解释得很好(或者我理解实际发生的事情),所以这里是代码:

%lex
%%

\s+         /* Consume whitespace */
"[b"        {return 'BOLD';}
"[i"        {return 'ITAL';}
"[s"        {return 'STRIKE';}
"[url="     {return 'URLEQ';}
"[url"      {return 'URL';}
"[img"      {return 'IMG';}
"[quote"    {return 'QUOT';}
"[code"     {return 'CODE';}
"[style"\s+"size="    {return 'STYLSIZ';}
"[style"\s+"color="   {return 'STYLCOL';}
"[color="    {return 'COL';}
"[list"     {return 'LIST';}
"[table"    {return 'TABLE';}
"[tr]"       {return 'TROW';}
"[td]"       {return 'TDEL';}

"[*]"       {return 'LITEM';}

"]"         {return 'CLOSE';}

"[/b]"      {return 'BOLDEND';}
"[/i]"      {return 'ITALEND';}
"[/s]"      {return 'STRIKEEND';}
"[/url]"    {return 'URLEND';}
"[/quote]"  {return 'QUOTEND';}
"[/code]"   {return 'CODEEND';}
"[/style]"  {return 'STYLEND';}
"[/color]"  {return 'COLEND';}
"[/list]"   {return 'LISTEND';}
"[/table]"  {return 'TABLEEND';}
"[/tr]"     {return 'TROWEND';}
"[/td]"     {return 'TDELEND';}
<<EOF>>     {return 'EOF';}
[a-zA-Z]+    {return 'NTOK';}

/lex

%start EXPR

%%

EXPR
    : CONTENT EOF { typeof console !== 'undefined' ? console.log($1) : print($1); return $1; }
    ;

CONTENT
    : BOLD CLOSE CONTENT BOLDEND            { $$ = "<b>"+$3+"</b>"; }
    | ITAL CLOSE CONTENT ITALEND            { $$ = "<i>"+$3+"</i>"; }
    | STRIKE CLOSE CONTENT STRIKEEND        { $$ = "<s>"+$3+"</s>"; }
    | URL CLOSE CONTENT URLEND              { $$ = "<a href=\""+$3+"\">"+$3+"</a>"; }
    | URLEQ NTOK CLOSE CONTENT URLEND       { $$ = "<a href=\""+$2+"\">"+$4+"</a>"; }
    | IMG CLOSE NTOK IMGEND                 { $$ = "<img src=\""+$3+"\"></img>"; }
    | QUOT CLOSE CONTENT QUOTEND            { $$ = "<blockquote>"+$3+"</b>"; }
    | CODE CLOSE CONTENT CODEEND            { $$ = "<pre>"+$3+"</b>"; }
    | STYLSIZ NTOK CLOSE CONTENT STYLEND    { $$ = "<span style=\"+$2+\">"+$4+"</b>"; }
    | STYLCOL NTOK CLOSE CONTENT STYLEND    { $$ = "<span style=\"color:"+$2+";\">"+$4+"</b>"; }
    | COL NTOK CLOSE CONTENT COLEND         { $$ = "<span style=\"color:"+$2+";\">"+$4+"</b>"; }
    | LIST CLOSE LITEMS LISTEND             { $$ = "<ul>"+$3+"</ul>"; }
    | TABLE CLOSE TDATA TABLEEND            { $$ = "<table>"+$3+"</table>"; }
    | NTOK
    ;

LITEMS
    : LITEM CONTENT LITEMS                  { $$ = "<li>"+$2+"</li>"; }
    ;

TDATA
    : TROW CLOSE CONTENT TROWEND            { $$ = "<tr>"+$3+"</tr>"; }
    | TDEL CLOSE CONTENT TDELEND            { $$ = "<td>"+$3+"</td>"; }
    ;

当我对字符串运行它时:

"a [b]Test, log 1 [/b] This is a story about a [url=\"http://google.com\"]person[/url]"

我收到错误:

Error: Parse error on line 1:
a [b]Test, log 1 [/b]
--^
Expecting 'EOF', 'BOLDEND', 'ITALEND', 'STRIKEEND', 'URLEND', 'QUOTEND', 'CODEEND', 'STYLEND', 'COLEND', 'LITEM', 'TROWEND', 'TDELEND', got 'BOLD'

非常感谢任何对错误的提醒。

4

1 回答 1

1

该语法/词法分析器中的任何内容都不会接受普通文本,除了NTOK(仅按字母顺序排列)。Soa被解析为NTOK,并且唯一允许的产生式NTOK需要后跟EOF. 我认为解析器期待大量的东西,也是EOF语法压缩的结果。

于 2013-10-28T00:53:49.760 回答