我的代码中有以下语法。
Rule 0 S' -> program
Rule 1 write -> WRITE LPAREN expression_list RPAREN SEMICOLON
Rule 2 read -> READ LPAREN expression_list RPAREN SEMICOLON
Rule 3 program -> function
Rule 4 function -> identifier identifier formal_parameters block
Rule 5 function -> VOID identifier formal_parameters block
Rule 6 formal_parameters -> LPAREN formal_parameter formal_parameters_list RPAREN
Rule 7 formal_parameters -> LPAREN empty RPAREN
Rule 8 formal_parameters_list -> SEMICOLON formal_parameter formal_parameters_list
Rule 9 formal_parameters_list -> empty
Rule 10 empty -> <empty>
Rule 11 formal_parameter -> expression_parameter
Rule 12 formal_parameter -> function_parameter
Rule 13 function_parameter -> VOID identifier formal_parameters
Rule 14 function_parameter -> INTEGER identifier formal_parameters
Rule 15 function_parameter -> identifier identifier formal_parameters
Rule 16 expression_parameter -> identifier_list COLON INTEGER
Rule 17 expression_parameter -> VAR identifier_list COLON INTEGER
Rule 18 identifier_list -> identifier
Rule 19 identifier_list -> identifier_list COMMA identifier
Rule 20 block -> body
Rule 21 block -> labels body
Rule 22 block -> labels variables body
Rule 23 block -> labels variables functions body
Rule 24 block -> labels functions body
Rule 25 block -> variables body
Rule 26 block -> variables functions body
Rule 27 block -> functions body
Rule 28 functions -> FUNCTIONS function_list
Rule 29 function_list -> function
Rule 30 function_list -> function function_list
Rule 31 variables -> VARS identifier_list COLON type SEMICOLON
Rule 32 type -> INTEGER
Rule 33 labels -> LABELS identifier_list SEMICOLON
Rule 34 body -> LBRACES stamement_list RBRACES
Rule 35 stamement_list -> statement stamement_list
Rule 36 stamement_list -> empty
Rule 37 statement -> unlabeled_statement
Rule 38 statement -> compound
Rule 39 unlabeled_statement -> assignment
Rule 40 unlabeled_statement -> function_call_statement
Rule 41 unlabeled_statement -> goto
Rule 42 unlabeled_statement -> return
Rule 43 unlabeled_statement -> conditional
Rule 44 unlabeled_statement -> repetitive
Rule 45 unlabeled_statement -> empty_statement
Rule 46 unlabeled_statement -> write
Rule 47 unlabeled_statement -> read
Rule 48 goto -> GOTO identifier SEMICOLON
Rule 49 return -> RETURN return_optional
Rule 50 return_optional -> expression
Rule 51 return_optional -> empty
Rule 52 function_call_statement -> function_call SEMICOLON
Rule 53 function_call -> IDENTIFIER LPAREN expression_list_and_empty RPAREN
Rule 54 expression_list_and_empty -> empty
Rule 55 expression_list_and_empty -> expression_list
Rule 56 expression_list -> expression
Rule 57 expression_list -> expression_list COMMA expression
Rule 58 repetitive -> WHILE LPAREN expression RPAREN compound
Rule 59 compound -> LBRACES compound_list RBRACES
Rule 60 compound_list -> unlabeled_statement
Rule 61 compound_list -> compound_list unlabeled_statement
Rule 62 empty_statement -> SEMICOLON
Rule 63 assignment -> identifier EQUALS expression SEMICOLON
Rule 64 variable -> identifier
Rule 65 variable -> identifier LBRACKETS expression_list RBRACKETS
Rule 66 expression -> simple_expression expression_optional
Rule 67 expression_optional -> relational_operator simple_expression
Rule 68 expression_optional -> empty
Rule 69 relational_operator -> COMPAREEQUAL
Rule 70 relational_operator -> NOTEQQUAL
Rule 71 relational_operator -> LESS
Rule 72 relational_operator -> LESSEQUAL
Rule 73 relational_operator -> GREATQUAL
Rule 74 relational_operator -> GREAT
Rule 75 conditional -> IF LPAREN expression RPAREN compound
Rule 76 conditional -> IF LPAREN expression RPAREN compound ELSE compound
Rule 77 simple_expression -> term simple_expression_list
Rule 78 simple_expression_list -> additive_operator term simple_expression_list
Rule 79 simple_expression_list -> empty
Rule 80 additive_operator -> PLUS
Rule 81 additive_operator -> MINUS
Rule 82 term -> factor multiplicative_operator_list
Rule 83 multiplicative_operator -> TIMES
Rule 84 multiplicative_operator -> DIVIDE
Rule 85 multiplicative_operator_list -> multiplicative_operator factor multiplicative_operator_list
Rule 86 multiplicative_operator_list -> empty
Rule 87 factor -> NUMBER
Rule 88 factor -> variable
Rule 89 factor -> LPAREN expression RPAREN
Rule 90 factor -> function_call
Rule 91 identifier -> IDENTIFIER
以及以下错误
WARNING: 1 shift/reduce conflict
! shift/reduce conflict for IDENTIFIER resolved as shift
LBRACES reduce using rule 10 (empty -> .)
GOTO reduce using rule 10 (empty -> .)
RETURN reduce using rule 10 (empty -> .)
IF reduce using rule 10 (empty -> .)
WHILE reduce using rule 10 (empty -> .)
SEMICOLON reduce using rule 10 (empty -> .)
WRITE reduce using rule 10 (empty -> .)
READ reduce using rule 10 (empty -> .)
RBRACES reduce using rule 10 (empty -> .)
NUMBER shift and go to state 93
LPAREN shift and go to state 95
IDENTIFIER shift and go to state 98
我试图解决问题,但最终导致了更多的冲突,之前我遇到了其他冲突,但是我总是在解决后立即进行保存,但是最后我设法解决了这个问题,但是现在我不能轻松识别它。
有人有想法吗?
您忘了以SEMICOLON终止返回声明。由于return语句采用可选表达式,并且可能后跟表达式语句,因此RETURN
令牌后跟表达式是不明确的。因此发生冲突。