gpt4 book ai didi

c - parser.y :79. 33-41 : symbol character is used, 但未定义为 token 且没有规则

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

我的解析器有问题,我无法解决这个问题。我不断收到错误:parser.y:79.33-41: symbol character is used, but is not defined as a token and has no rules。我已将行放在 ** **。

下面是我的解析器代码:

%token          PLUS TIMES DIVIDE SUBTRACT BRA KET EQUALS NOT_EQUAL LESS_THAN GREATER_THAN GREATER_THAN_OR_EQUAL
%token COMA SEMICOLON ARROW DECIMAL ENDDO ENDFOR ENDIF ENDP ENDWHILE ELSE CODE OF TYPE DECLARATIONS CHARACTER
%token INTEGER REAL IF THEN DO WHILE FOR IS BY TO WRITE NEWLINE READ NOT AND OR ID LESS_THAN_OR_EQUAL APOSTROPHE

%%
Program : block
| ENDP
| ID
;
block : DECLARATIONS declaration_block CODE statement_list
| CODE statement_list
;
declaration_block : ID OF TYPE SEMICOLON
| ID_list OF TYPE SEMICOLON
| ID_list OF TYPE SEMICOLON declaration_block
;
ID_list : ID
| ID COMA ID_list
;
type : CHARACTER
| INTEGER
| REAL
;
statement_list : statement
| statement_list SEMICOLON statement
;
statement : assignment_statement
| if_statement
| do_statement
| while_statement
| for_statement
| write_statement
| read_statement
;
assignment_statement : expression ARROW ID
;
if_statement : IF conditional THEN statement_list ELSE statement_list ENDIF
;
do_statement : DO statement_list WHILE conditional ENDDO
;
while_statement : WHILE conditional DO statement_list ENDWHILE
;
for_statement : FOR ID IS expression BY expression TO expression DO statement_list ENDFOR
;
write_statement : WRITE BRA output_list KET write_statement NEWLINE
;
read_statement : READ BRA ID KET
;
output_list : value
| output_list COMA value
;
conditional : expression comparator expression
| NOT conditional
| expression comparator expression AND conditional
| expression comparator expression OR conditional
;
comparator : EQUALS
| NOT_EQUAL
| LESS_THAN
| GREATER_THAN
| LESS_THAN_OR_EQUAL
| GREATER_THAN_OR_EQUAL
;
expression : term
| expression PLUS term
| expression SUBTRACT term
;
term : value
| term TIMES value
| term DIVIDE value
;
value : ID
| constant
| BRA expression KET
;
constant : number_constant
| character_constant
;
**character_constant : APOSTROPHE character APOSTROPHE**
;
number_constant : INTEGER
| SUBTRACT INTEGER
| SUBTRACT INTEGER DECIMAL INTEGER
| INTEGER DECIMAL INTEGER
;
%%

下面是我的词法分析器:

%{
#ifdef PRINT
#define TOKEN(i) printf("Token: " #i "\n");
#else
#define TOKEN(i) return (i);
#endif
%}

delim [ \r\n\t]
ws {delim}+
digit [0-9]
character [a-zA-Z]
INTEGER {digit}+
id {character}({character}|{digit})*
character_constant ('{character}')


%%
{ws} ; /* Do Nothing */

"+" TOKEN(PLUS)
"*" TOKEN(TIMES)
"/" TOKEN(DIVIDE)
"-" TOKEN(SUBTRACT)
"(" TOKEN(BRA)
")" TOKEN(KET)
...
"'" TOKEN(APOSTROPHE)
ENDP TOKEN(ENDP)
CODE TOKEN(CODE)
OF TOKEN(OF)
TYPE TOKEN(TYPE)
DECLARATIONS TOKEN(DECLARATIONS)
character TOKEN(CHARACTER)
{INTEGER} TOKEN(INTEGER)
...
{id} TOKEN(ID)
{character_constant} TOKEN(character_constant)
%%

从我的 BNF 到我的解析器,我的整个文件的拼写都是正确的,有谁知道这里的问题是什么。我尝试将其更改为字母而不是字符,但这只会给我带来另一个错误。

最佳答案

错误是无法将单个字符与词法分析器中的标识符区分开来。您已经正确地添加了一个词法分析器规则来匹配它,它返回一个名为 character_constant 的标记,但是您还为不需要的字符常量设置了一个解析器规则,因为它已经在词法分析器中匹配了。词法分析器不应返回 APOSTROPHE 作为标记,因为它会在 character_constant 标记中匹配。

您应该只从 bison 中删除 character_constant 的规则,并将 character_constant 添加到与 %token 中的词法分析器匹配的标记列表中声明。

我通常建议对 token 名称使用大写字母,以避免混淆哪个是终端,哪个是非终端。

关于c - parser.y :79. 33-41 : symbol character is used, 但未定义为 token 且没有规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53184359/

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