gpt4 book ai didi

c - Bison 说开始符号不派生任何句子

转载 作者:太空宇宙 更新时间:2023-11-04 01:45:48 29 4
gpt4 key购买 nike

我正在编写基本的编译器,但很早就卡住了。当我尝试运行我的 bison 文件时,它会抛出错误(在下面)我不知道为什么会这样,并且已经为此苦苦挣扎了很长一段时间。

错误:

compiler/parser.y: warning: 9 nonterminals useless in grammar [-Wother]
compiler/parser.y: warning: 32 rules useless in grammar [-Wother]
compiler/parser.y:34.1-7: fatal error: start symbol program does not derive any sentence
program : DECLARE declaration IN commands END

弹性:

%option noyywrap
%{
#include <stdio.h>
#include "parser.tab.h"
%}

NUMBER [0-9]+
PID [_a-z]+

WHITESPACE [ \t\r]+

%x COMMENT
%%

<INITIAL>{
"[" BEGIN(COMMENT);

\n yylineno++;
{WHITESPACE}

{NUMBER} {
printf("Number: %s\n", yytext);
yylval.ival = (char*) strdup(yytext);
return NUM;
}

{PID} {
printf("PID: %s\n", yytext);
yylval.sval = (char*) strdup(yytext);
return PID;
}

":=" return ASSIGN;

"+" return ADD;
"-" return SUB;
"*" return MUL;
"/" return DIV;
"%" return MOD;

"=" return EQ;
"!=" return NEQ;
"<" return LT;
">" return GT;
"<=" return LE;
">=" return GE;

")" return R_BRACKET;
"(" return L_BRACKET;
";" return SEMICOLON;
":" return COLON;


"DECLARE" return DECLARE;
"IN" return IN;
"END" return END;

"IF" return IF;
"ELSE" return ELSE;
"ENDIF" return ENDIF;

"WHILE" return WHILE;
"DO" return DO;
"ENDWHILE" return ENDWHILE;
"ENDDO" return ENDDO;

"FOR" return FOR;
"FROM" return FROM;
"TO" return TO;
"DOWNTO" return DOWNTO;
"ENDFOR" return ENDFOR;

"READ" return READ;
"WRITE" return WRITE;
}

<COMMENT>{
"]" BEGIN(INITIAL);
[^\n]+\n yylineno++;
}

%%

Bison :

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

extern int yylineno;

int yylex(void);
void yyerror(const char *);

int error = 0;
%}

%union{
char* sval;
char* ival;
}

%token <sval> DECLARE /* Declarations block */
%token <sval> IN END /* Code block */
%token <sval> IF THEN ELSE ENDIF /* Conditional block */
%token <sval> WHILE DO ENDWHILE ENDDO /* While-do and Do-while loop block */
%token <sval> FOR FROM TO DOWNTO ENDFOR /* For loop block */
%token <sval> READ WRITE
%token <sval> ASSIGN
%token <sval> ADD SUB MUL DIV MOD /* Arithmetic operators */
%token <sval> EQ NEQ LT GT LE GE /* Boolean operators */
%token <sval> L_BRACKET R_BRACKET SEMICOLON COLON /* Symbols */
%token <ival> NUM
%token <sval> PID

%%

program : DECLARE declaration IN commands END
;

declaration : declaration PID SEMICOLON
| declaration PID L_BRACKET NUM COLON NUM R_BRACKET SEMICOLON
;

commands : commands command
| command
;

command : id ASSIGN expression SEMICOLON
| IF condition THEN commands ELSE commands ENDIF
| IF condition THEN commands ENDIF
| WHILE condition DO commands ENDWHILE
| DO commands WHILE condition ENDDO
| FOR PID FROM value TO value DO commands ENDFOR
| FOR PID FROM value DOWNTO value DO commands ENDFOR
| READ id SEMICOLON
| WRITE value SEMICOLON
;

expression : value
| value ADD value
| value SUB value
| value MUL value
| value DIV value
| value MOD value
;

condition : value EQ value
| value NEQ value
| value LT value
| value GT value
| value LE value
| value GE value
;

value : NUM
| id
;

id : PID
| PID L_BRACKET PID R_BRACKET
| PID L_BRACKET NUM R_BRACKET
;

%%

void yyerror(const char *msg) {
fprintf(stderr, "Compiling error: %s\n", msg);
}

如果您想知道,我从另一个文件运行 main 函数,但我认为这不是问题所在。

最佳答案

你的语法说有一个程序,你必须有一个声明:

program         :   DECLARE declaration ...

而获得声明的唯一途径就是这两条规则

declaration :   declaration ...
| declaration ...

但这两者都要求您已经有一个声明。由于您一开始什么都没有,而且如果您已经拥有一个声明,您将永远无法拥有任何声明。

因此你永远无法解析一个程序

关于c - Bison 说开始符号不派生任何句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53937174/

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