4

私は jison に非常に慣れていませんが、役立つクエリ パーサーをまとめることができました。「a == 1 and b == 1 and c == 1」のような文字列を次のようなオブジェクトに解析できるパーサーを作成しようとしています

{and: [
  {a: {eq: 1}},
  {b: {eq: 1}},
  {c: {eq: 2}}
]}

一方、「a == 1 or b == 1 and c == 1」のような文字列は、次のようなオブジェクトに解析する必要があります

{or: [
  {a: {eq: 1}},
  {and: [
    {b: {eq: 1}},
    {c: {eq: 1}}
  ]}
]}

私の文法はこれまでのところ次のようになっています。

%lex

%%
\s+       /*skip whitespace*/
\"(\\.|[^"])*\"          yytext = yytext.substr(1, yyleng-2); return 'STRING';
"=="                     return '==';
and[^\w]                 return 'and';
or[^\w]                  return 'or';
[0-9]+(?:\.[0-9]+)?\b    return 'NUMBER';
[a-zA-Z][\.a-zA-Z0-9_]*  return 'SYMBOL';
<<EOF>>                  return 'EOF';

/lex

%left 'or'
%left 'and'
%left '=='

%start expressions

%%

expressions
  : e EOF
    {$$ = $1;}
  ;

e
  : property '==' value
    { $$ = {}; $[$1] = {eq: $3}; }
  | boolAnd 
    { $$ = {and: $1}}
  | boolOr 
    { $$ = {or: $1}}
  ;

boolAnd
  : boolAnd 'and' e
    {$$ = $1; $1.push($3);}
  | e 'and' e
    {$$ = [$1, $3];}
  ;

boolOr
  : boolOr 'or' e
    {$$ = $1; $1.push($3);}
  | e 'or' e
    {$$ = [$1, $3];}
  ;

property
  : SYMBOL
    {$$ = $1;}
  ;

value
  : NUMBER
    {$$ = Number(yytext);}
  | STRING
    {$$ = yytext; }
  ;

次の競合エラーが表示されます。

Conflict in grammar: multiple actions possible when lookahead token is and in state 4
- reduce by rule: e -> boolAnd
- shift token (then go to state 11)
Conflict in grammar: multiple actions possible when lookahead token is or in state 5
- reduce by rule: e -> boolOr
- shift token (then go to state 12)

States with conflicts:
State 4
  e -> boolAnd . #lookaheads= EOF and or
  boolAnd -> boolAnd .and e #lookaheads= EOF and or
State 5
  e -> boolOr . #lookaheads= EOF and or
  boolOr -> boolOr .or e #lookaheads= EOF or and

私が間違っていることについて提案を提供できる人はいますか? どうもありがとう

4

1 に答える 1