0

私がパーサーを書いている言語には、ここで関連する 3 つの構成要素があります。ordで表される演算子は、TOK_ORD文字式を整数式にキャストし、 と[ ].、C と同様にそれぞれインデックス アクセスとフィールド アクセスに使用されます。

以下は私の優先ルールからの抜粋です。

%right TOK_ORD
%left  PREC_INDEX PREC_MEMBER

expr私の文法には、式を表す非終端記号があります。文法からの関連するスニペットを次に示します (は、ファイルTOK_IDENTで定義された識別子の正規表現です)。.l

expr     : TOK_ORD expr                         { /* semantic actions */ }
         | variable                             { /* semantic actions */ }
         ;
variable : expr '[' expr ']' %prec PREC_INDEX   { /* semantic actions */ }
         | expr '.' TOK_IDENT %prec PREC_MEMBER { /* semantic actions */ }
         ;

bison 出力ファイルからの shift/reduce 競合に関する情報は次のとおりです。

state 61

42 expr: expr . '=' expr
43     | expr . TOK_EQ expr
44     | expr . TOK_NE expr
45     | expr . TOK_LT expr
46     | expr . TOK_LE expr
47     | expr . TOK_GT expr
48     | expr . TOK_GE expr
49     | expr . '+' expr
50     | expr . '-' expr
51     | expr . '*' expr
52     | expr . '/' expr
53     | expr . '%' expr
57     | TOK_ORD expr .
72 variable: expr . '[' expr ']'
73         | expr . '.' TOK_IDENT

'['  shift, and go to state 92
'.'  shift, and go to state 93

'['       [reduce using rule 57 (expr)]
'.'       [reduce using rule 57 (expr)]
$default  reduce using rule 57 (expr)

参考までに州 92 および 93:

state 92

72 variable: expr '[' . expr ']'

TOK_FALSE      shift, and go to state 14
TOK_TRUE       shift, and go to state 15
TOK_NULL       shift, and go to state 16
TOK_NEW        shift, and go to state 17
TOK_IDENT      shift, and go to state 53
TOK_INTCON     shift, and go to state 19
TOK_CHARCON    shift, and go to state 20
TOK_STRINGCON  shift, and go to state 21
TOK_ORD        shift, and go to state 22
TOK_CHR        shift, and go to state 23
'+'            shift, and go to state 24
'-'            shift, and go to state 25
'!'            shift, and go to state 26
'('            shift, and go to state 29

expr       go to state 125
allocator  go to state 44
call       go to state 45
callargs   go to state 46
variable   go to state 47
constant   go to state 48


state 93

73 variable: expr '.' . TOK_IDENT

シフト/リデュースの競合が発生する理由がわかりません。私の文法では、インデックスとフィールドへのアクセスが よりも優先されるように明確に定義されていますがord、それだけでは十分ではないようです。

ご参考までに、はい、これは宿題ですが、課題は既に提出され、採点されています。何が起こっているのかをよりよく理解できるように、戻ってシフト/削減の競合を修正しようとしています。

4

1 に答える 1