gpt4 book ai didi

c - 为什么我在这个 bison 语法中收到这么多 'useless rule/token' 警告?

转载 作者:太空宇宙 更新时间:2023-11-04 02:44:07 26 4
gpt4 key购买 nike

我正在为 C 编写一个简单的 bison 语法,到目前为止有以下规则:

program                 : declaration_list
;

declaration_list : declaration_list declaration
| declaration
;

declaration : var_declaration
| fun_declaration
;

var_declaration : type_specifier var_decl_list EOL
;

scoped_var_declaration : scoped_type_specifier var_decl_list EOL
;

var_decl_list : var_decl_list COMMA var_decl_initialise
| var_decl_initialise
;

var_decl_initialise : var_decl_id
| var_decl_id COLON simple_expression
;

var_decl_id : ID
| ID LSQUARE INT_VAL RSQUARE
;

scoped_type_specifier : type_specifier
;

type_specifier : INT
| FLOAT
| DOUBLE
| CHAR
| BOOL
;

fun_declaration : type_specifier ID LPAREN params RPAREN
| ID LPAREN params RPAREN statement
;

params : /*Empty*/
| param_list
;

param_list : param_list EOL param_type_list
| param_type_list
;

param_type_list : type_specifier param_id_list
;

param_id_list : param_id_list COMMA param_id
| param_id
;

param_id : ID
| ID LSQUARE RSQUARE
;

statement : expression_stmt
| compound_stmt
| selection_stmt
| iteration_stmt
| return_stmt
| break_stmt
;

compound_stmt : LBRACE local_declarations statement_list RBRACE
;

local_declarations : /*Empty*/
| local_declarations scoped_var_declaration
;

statement_list : /*Empty*/
| statement_list statement
;

expression_stmt : expression EOL
| EOL
;

selection_stmt : IF LPAREN simple_expression RPAREN statement
| IF LPAREN simple_expression RPAREN statement ELSE statement
;

iteration_stmt : WHILE LPAREN simple_expression RPAREN statement // TODO: for
;

return_stmt : RETURN EOL
| RETURN expression EOL
;

break_stmt : BREAK EOL
;

expression : mutable '=' expression
| mutable ADD_ASSIGN expression
| mutable SUB_ASSIGN expression
| mutable '*' expression
| mutable DIV_ASSIGN expression
| mutable BITWISE_AND_ASSIGN expression
| mutable BITWISE_OR_ASSIGN expression
| mutable INCREMENT
| mutable DECREMENT
| simple_expression
;

simple_expression : simple_expression LOGICAL_OR and_expression
| and_expression
;

and_expression : and_expression LOGICAL_AND unary_rel_expression
| and_expression
;

unary_rel_expression : '!' unary_rel_expression
| rel_expression
;

rel_expression : sum_expression relop sum_expression
| sum_expression
;

relop : LT_EQUAL_TO
| '<'
| '>'
| GT_EQUAL_TO
| EQUAL_TO
| NOT_EQUAL_TO
;

sum_expression : sum_expression sumop term
| term
;

sumop : '+'
| '-'
;

term : term mulop unary_expression
| unary_expression
;

mulop : '*'
| '/'
| '%'
;

unary_expression : unaryop unary_expression
| factor
;

unaryop : '-'
| '*'
;

factor : immutable
| mutable
;

mutable : ID
| ID LSQUARE expression RSQUARE
;

immutable : LPAREN expression RPAREN
| call
| constant
;

call : ID LPAREN args RPAREN
;

args : /*Empty*/
| arg_list
;

arg_list : arg_list COMMA expression
| expression
;

constant : number
| boolean
| CHAR_VAL
| STRING_VAL
;

number : INT_VAL
| DOUBLE_VAL
;

boolean : TRUE
| FALSE
;

但是当我尝试用 bison 编译时,我收到一大堆警告,说几乎所有的规则和标记都是无用的:

parser.y: warning: 21 nonterminals useless in grammar
parser.y: warning: 52 rules useless in grammar
parser.y:127.27-40: warning: nonterminal useless in grammar: selection_stmt
parser.y:128.27-40: warning: nonterminal useless in grammar: iteration_stmt
parser.y:85.45-61: warning: nonterminal useless in grammar: simple_expression
parser.y:174.56-69: warning: nonterminal useless in grammar: and_expression
parser.y:178.54-73: warning: nonterminal useless in grammar: unary_rel_expression
parser.y:183.27-40: warning: nonterminal useless in grammar: rel_expression
parser.y:186.42-46: warning: nonterminal useless in grammar: relop
parser.y:186.27-40: warning: nonterminal useless in grammar: sum_expression
parser.y:198.42-46: warning: nonterminal useless in grammar: sumop
parser.y:198.48-51: warning: nonterminal useless in grammar: term
parser.y:206.32-36: warning: nonterminal useless in grammar: mulop
parser.y:206.38-53: warning: nonterminal useless in grammar: unary_expression
parser.y:215.27-33: warning: nonterminal useless in grammar: unaryop
parser.y:216.27-32: warning: nonterminal useless in grammar: factor
parser.y:223.27-35: warning: nonterminal useless in grammar: immutable
parser.y:232.27-30: warning: nonterminal useless in grammar: call
parser.y:236.37-40: warning: nonterminal useless in grammar: args
parser.y:240.27-34: warning: nonterminal useless in grammar: arg_list
parser.y:233.27-34: warning: nonterminal useless in grammar: constant
parser.y:247.27-32: warning: nonterminal useless in grammar: number
parser.y:248.27-33: warning: nonterminal useless in grammar: boolean
parser.y:85.27-61: warning: rule useless in grammar: var_decl_initialise: var_decl_id COLON simple_expression
parser.y:127.27-40: warning: rule useless in grammar: statement: selection_stmt
parser.y:128.27-40: warning: rule useless in grammar: statement: iteration_stmt
parser.y:148.27-70: warning: rule useless in grammar: selection_stmt: IF LPAREN simple_expression RPAREN statement
parser.y:149.27-85: warning: rule useless in grammar: selection_stmt: IF LPAREN simple_expression RPAREN statement ELSE statement
parser.y:152.27-73: warning: rule useless in grammar: iteration_stmt: WHILE LPAREN simple_expression RPAREN statement
parser.y:171.27-43: warning: rule useless in grammar: expression: simple_expression
parser.y:174.27-69: warning: rule useless in grammar: simple_expression: simple_expression LOGICAL_OR and_expression
parser.y:175.27-40: warning: rule useless in grammar: simple_expression: and_expression
parser.y:178.27-73: warning: rule useless in grammar: and_expression: and_expression LOGICAL_AND unary_rel_expression
parser.y:179.27-40: warning: rule useless in grammar: and_expression: and_expression
parser.y:182.27-50: warning: rule useless in grammar: unary_rel_expression: '!' unary_rel_expression
parser.y:183.27-40: warning: rule useless in grammar: unary_rel_expression: rel_expression
parser.y:186.27-61: warning: rule useless in grammar: rel_expression: sum_expression relop sum_expression
parser.y:187.27-40: warning: rule useless in grammar: rel_expression: sum_expression
parser.y:190.27-37: warning: rule useless in grammar: relop: LT_EQUAL_TO
parser.y:191.27-29: warning: rule useless in grammar: relop: '<'
parser.y:192.27-29: warning: rule useless in grammar: relop: '>'
parser.y:193.27-37: warning: rule useless in grammar: relop: GT_EQUAL_TO
parser.y:194.27-34: warning: rule useless in grammar: relop: EQUAL_TO
parser.y:195.27-38: warning: rule useless in grammar: relop: NOT_EQUAL_TO
parser.y:198.27-51: warning: rule useless in grammar: sum_expression: sum_expression sumop term
parser.y:199.27-30: warning: rule useless in grammar: sum_expression: term
parser.y:202.27-29: warning: rule useless in grammar: sumop: '+'
parser.y:203.27-29: warning: rule useless in grammar: sumop: '-'
parser.y:206.27-53: warning: rule useless in grammar: term: term mulop unary_expression
parser.y:207.27-42: warning: rule useless in grammar: term: unary_expression
parser.y:210.27-29: warning: rule useless in grammar: mulop: '*'
parser.y:211.27-29: warning: rule useless in grammar: mulop: '/'
parser.y:212.27-29: warning: rule useless in grammar: mulop: '%'
parser.y:215.27-50: warning: rule useless in grammar: unary_expression: unaryop unary_expression
parser.y:216.27-32: warning: rule useless in grammar: unary_expression: factor
parser.y:219.27-29: warning: rule useless in grammar: unaryop: '-'
parser.y:220.27-29: warning: rule useless in grammar: unaryop: '*'
parser.y:223.27-35: warning: rule useless in grammar: factor: immutable
parser.y:224.27-33: warning: rule useless in grammar: factor: mutable
parser.y:231.27-50: warning: rule useless in grammar: immutable: LPAREN expression RPAREN
parser.y:232.27-30: warning: rule useless in grammar: immutable: call
parser.y:233.27-34: warning: rule useless in grammar: immutable: constant
parser.y:236.27-47: warning: rule useless in grammar: call: ID LPAREN args RPAREN
parser.y:239.5: warning: rule useless in grammar: args: /* empty */
parser.y:240.27-34: warning: rule useless in grammar: args: arg_list
parser.y:243.27-51: warning: rule useless in grammar: arg_list: arg_list COMMA expression
parser.y:244.27-36: warning: rule useless in grammar: arg_list: expression
parser.y:247.27-32: warning: rule useless in grammar: constant: number
parser.y:248.27-33: warning: rule useless in grammar: constant: boolean
parser.y:249.27-34: warning: rule useless in grammar: constant: CHAR_VAL
parser.y:250.27-36: warning: rule useless in grammar: constant: STRING_VAL
parser.y:253.27-33: warning: rule useless in grammar: number: INT_VAL
parser.y:254.27-36: warning: rule useless in grammar: number: DOUBLE_VAL
parser.y:257.27-30: warning: rule useless in grammar: boolean: TRUE
parser.y:258.27-31: warning: rule useless in grammar: boolean: FALSE

我花了好几个小时查看它,并尝试检查使用选项 -r all 运行 bison 时获得的 .output 文件,但我无法弄清楚我的错误在哪里马德群岛我知道“无用”意味着永远不会达到规则,但我看不到断开连接的位置。

最佳答案

第175行有错字,我想应该是这样的:

and_expression          : and_expression LOGICAL_AND unary_rel_expression
| unary_rel_expression
;

...警告将消失。

关于c - 为什么我在这个 bison 语法中收到这么多 'useless rule/token' 警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29100609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com