我想知道如何解决下面语法中的reduce/reduce冲突。有问题的规则是:LDA HASH表达式——当我删除它时,冲突就会消失。我已经尝试将LDA-xxx解析规则分解为单独的规则,但我仍然遇到了冲突。这个规则到底有什么问题?我该如何解决它?
%%
program:
program statement NEWLINE { }
|
;
statement:
instruction {
printf("Opcode: %c Address Mode: %d Operand: %dn", opcode[0], address_mode, operand);
numInstructions++;
}
| assignment {
}
;
instruction: ADC expression {
}
| CLC {
address_mode = IMPLIED_MODE;
opcode[0] = 0x18;
fwrite(opcode, 1, 1, output);
}
| CLD {
address_mode = IMPLIED_MODE;
opcode[0] = 0xd8;
fwrite(opcode, 1, 1, output);
}
| LDA expression {
opcode[0] = 0xad;
}
| LDA HASH expression {
opcode[0] = 0xa9;
address_mode = IMMEDIATE_MODE;
if( operand > 255 ) {
printf("syntax error. Immediate value too large. The value must lie in the range between 0..255. Line: %dn", yylineno-1);
exit(1);
}
}
| LDA expression COMMA IDENTIFIER {
// LDA operand, x | LDA operand, y
opcode[0] = 0xbd;
}
| STA expression {
}
| RTS {
address_mode = IMPLIED_MODE;
opcode[0] = 0x60;
fwrite(opcode, 1, 1, output);
}
;
expression: number { $$ = $1;
operand = $1;
}
| HASH number {
operand = $2;
}
;
number:
NUMBER { $$ = $1; }
| HEXADECIMAL { $$ = $1; }
| BINARY { $$ = $1; }
;
assignment:
IDENTIFIER EQUALS expression { $$ = $3; }
;
%%
我刚刚发现语法中的错误。问题在于;表达式:数字;则规则部分简化为";HASH数";导致减少/减少与";LDA HASH表达式";规则解决该问题的方法是删除";HASH数";表达式规则中的术语。