野牛 $end 错误,yyerror 不打印行,以及野牛语法规则中的问题

问题描述

我在为带有 flex/bison 的编译器创建前端时遇到了一个问题(现在我们正处于创建词法分析器和语法分析器的阶段,相应地使用 flex 和 bison ).

请注意,我对词法分析/解析和 flex/bison 完全陌生,因此非常欢迎所有提示

我目前面临的问题是,我一直收到 Bison 的换行符语法错误。以下是文件

词法分析器.l

%{
#include <stdio.h>
#include <stdlib.h>
#include "sym_hashing.h"
#include "fifostrq.h"
#include "parser.h"
FILE *new_file;
void add_bloc_number();
void reduce_bloc_number();
void singleLineComment();
void multiLineComment();
void handleError(char*);
void validateNum(char*);
void genericParse(char*);
DEQ* errQue;
%}
%option yylineno
%option noyywrap
alpha           [A-Za-z]
digit           [0-9]

identifier      {alpha}({alpha}|{digit})*
integer         (-?{digit}{1,5})
constString     \"(\\.|[^"\\])*\"
other           [\n\r\t\v\f]+

%%

"LET"                {return(LET); }

"DO"                 {return(DO); }

"LOOP"               {return(LOOP); }

"UNTIL"              {return(UNTIL); }

"WHILE"              {return(WHILE); }

"WEND"               {return(WEND); }

"SUB"                {return(SUB); }

"CALL"               {return(CALL); }

"INPUT"              {return(INPUT); }

"PRINT"              {return(PRINT); }

"IF"                 {return(IF); }

"THEN"               {return(THEN); }

"ELSE"               {return(ELSE); }

"AND"                {return(AND); }

"NOT"                {return(NOT); }

"OR"                 {return(OR); }

"RETURN"             {return(RETURN); }

"REM"                {singleLineComment();}

"+"                  {return(PLUS);}
"-"                  {return(MINUS);}
"*"                  {return(STAR);}
"/"                  {return(DIV);}

"<"                  {return(LT);}
">"                  {return(GT);}
"<="                 {return(LTEQUAL);}
">="                 {return(GTEQUAL);}
"<>"                 {return(LTGT);}

";"                  {return(SEMICOLON);}
","                  {return(COMMA);}

"("                  {return(LPAREN);}
")"                  {return(RPAREN);}
"["                  {return(LBRACK);}
"]"                  {return(RBRACK);}
"{"                  {return(LSWIGLY);}
"}"                  {return(RSWIGLY);}

"="                  {printf("Found =\n"); return(ASSIGNMENT);}
"=="                 {return(EQUALS);}

"%*"                 {multiLineComment();}

{constString}        { yylval.conststr=strdup(yytext); printf("Found %s\n",yylval.conststr); return(CONSTSTR);}

{integer}            { yylval.constnum=strdup(yytext); printf("Found %s\n",yylval.constnum); validateNum(yylval.constnum); return(CONSTINT);}

{identifier}         { yylval.name=strdup(yytext); printf("Found %s\n",yylval.name); return(IDENTIFIER);}

{other}              {}

.                    { char* yycopy=strdup(yytext); genericParse(yycopy);}

%%

void genericParse(char * val) {
    if (val[0] != '\0' && val[0] != ' ' && val != NULL) {
        handleError(val);
    }
}

void handleError(char * token) {

  char * buff = malloc(sizeof(char) * (strlen(token) + 36));
  sprintf(buff,"Lexer ERROR at line %d of token %s\n",line,token);
  insertAtHead(errQue,buff);

}

void singleLineComment() {

  int c;

  while ((c = input()) != '\n' && c != EOF);

  line++;

}

void validateNum(char * value) {

  int num = atoi(value);

  if (num < -32768 || num > 32767) {
    handleError(value);
    return;
  }


}

void add_bloc_number() {
  block_num = block_num + 1;
}

void reduce_bloc_number() {
  block_num = block_num - 1;
}

void multiLineComment() {

  int c,flag = 0;

  for (;;) {
    while ((c = input()) != '*' && c != EOF) {
      if (c == '\n') {
        line++;
      }
    }
    if (c == '*') {
      while ((c = input()) == '*') {
        if (c == '\n') {
          line++;
        }
      }
      if (c == '%') {
        break;
      }
    }
    if (c == EOF) {
      printf("ERROR: EOF in comment\n");
      break;
    }
  }
}

gram.y

%{
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include "fifostrq.h"
#include "sym_tab.h"
extern int column;
extern void yyrestart( FILE *input_file );
extern char *yytext;
extern FILE *yyin;
extern int yylineno;
extern int block_num;
int yyerror(const char *s);
int column;
%}
%locations
%define parse.error verbose
%define parse.lac full
%printer { fputs($$,yyo); } IDENTIFIER

%union
{
    char *name;
    char *constnum;
    char *conststr;
}

%token IDENTIFIER
%token CONSTINT CONSTSTR
%token LET DO LOOP UNTIL WHILE WEND SUB
%token CALL INPUT PRINT IF THEN ELSE AND
%token NOT OR RETURN ASSIGNMENT NEWLINE
%token PLUS MINUS STAR DIV LT GT LTEQUAL GTEQUAL
%token LTGT SEMICOLON COMMA LPAREN RPAREN
%token LSWIGLY RSWIGLY EQUALS LBRACK RBRACK
%token MISC

%right ASSIGNMENT
%left LT GT LTEQUAL GTEQUAL LTGT EQUALS
%left AND OR
%right NOT
%left PLUS MINUS
%left STAR DIV

%type <name> IDENTIFIER
%type <constnum> CONSTINT
%type <conststr> CONSTSTR

%start program

%%
program:
    statement_or_func
    newline_statement_or_func_op
;

newline_statement_or_func_op:
    /*empty*/
    | NEWLINE statement_or_func newline_statement_or_func_op
;

statement_or_func:
    sequence
    | func_stat
;

sequence:
    statement | sequence le statement
;

le:
    NEWLINE
    | SEMICOLON
;

brackets_seq:
    LSWIGLY sequence RSWIGLY
;

brack_or_stat:
    brackets_seq
    | statement
;

statement:
    /*empty*/
    | decl_stat assignment_stat
    | if_stat
    | do_until_stat
    | while_stat
    | return_stat
    | print_stat
    | input_stat
    | func_stat
    | call_stat
    /*comment stat is not needed,they are removed by the lexer*/
;

decl_stat:
    LET declaration | decl_stat COMMA declaration
;

declaration:
    IDENTIFIER
    | assignment_stat
;

assignment_stat:
    IDENTIFIER ASSIGNMENT expression
;

if_stat:
    IF LPAREN condition RPAREN THEN brack_or_stat
    elsepart
;

elsepart:
    /*empty*/
    | ELSE brack_or_stat
;

do_until_stat:
    DO brack_or_stat LOOP UNTIL LPAREN condition RPAREN
;

while_stat:
    WHILE LPAREN condition RPAREN brack_or_stat WEND
;

return_stat:
    RETURN LPAREN expression RPAREN
;

/*with the grammar rule specified in the pdf,PRINT "*"; wouldn't work*/
print_stat:
    PRINT CONSTSTR le
    | PRINT CONSTSTR COMMA expression_list
;

expression_list:
    expression | expression_list COMMA expression
;

input_stat:
    INPUT CONSTSTR COMMA IDENTIFIER | input_stat COMMA IDENTIFIER
;

func_stat:
    SUB IDENTIFIER funcbody
;

funcbody:
    formalpars brackets_seq
;

formalpars:
    LPAREN /*empty*/ RPAREN
    | LPAREN formalparlist RPAREN
;

formalparlist:
    IDENTIFIER | formalparlist COMMA IDENTIFIER
;

call_stat:
    CALL IDENTIFIER actualpars
;

actualpars:
    LPAREN /*empty*/ RPAREN
    | LPAREN actualparlist RPAREN
;

actualparlist:
    actualparitem | actualparlist COMMA actualparitem
;

actualparitem:
    expression | IDENTIFIER
;

/*comment stat is not needed,it is removed by the parser regardless...*/

condition:
    boolterm | condition OR boolterm
;

boolterm:
    boolfactor | boolterm AND boolterm
;

boolfactor :
    NOT LBRACK condition RBRACK
    | LBRACK condition RBRACK
    | expression relational_oper expression
;

expression:
    optional_sign term | expression add_oper optional_sign term
;

term:
    factor | term mul_oper factor
;

factor:
    constant | LPAREN expression RPAREN | IDENTIFIER | call_stat
;

constant:
    CONSTINT | CONSTSTR
;

relational_oper:
    EQUALS | LT | LTEQUAL | LTGT | GTEQUAL | GT
;

add_oper:
    PLUS | MINUS
;

mul_oper:
    STAR | DIV
;

optional_sign:
    /*empty*/
    | add_oper
;

%%

int yydebug;

int main(int argc,char *argv[]){
    #ifdef YYDEBUG
        yydebug = 1;
    #endif

    createDEQ(&errQue);

    if (argc != 2)
        printf("\nUsage: lexyy <input file name> \n");
    else
        if ((yyin = fopen(argv[1],"r")) == NULL)
            printf("\n<%s> not found.\n",argv[1]);
        else {
            yyrestart(yyin);
            yyparse();
            fclose(yyin);
        }
    printQueue(errQue);
    freeDEQ( & errQue);

    return 0;
}

int yyerror(const char *token) {
    fflush(stdout);
    char *buff = malloc(sizeof(char) * 1000);
    sprintf(buff,"\n-----------------------\n%s\n%*s\n%*s\tLine: %d\tColumn: %d\tBlock: %d\n-----------------------\n",yytext,column,"^",token,yylineno,block_num);
    insertAtHead(errQue,buff);
    return 0;
}

static int yyprint(FILE *file,int type,YYSTYPE value) {
    if (type == IDENTIFIER)
        fprintf (stderr," = %s",value.name);
    return 1;
}

BNF

<PROGRAM> ::= < STATEMENT-OR-FUNC >
(‘\n’ < STATEMENT-OR-FUNC >)*
<STATEMENT-OR-FUNC> ::= < SEQUENCE > |
<FUNC-STAT>
<SEQUENCE> ::= <STATEMENT> ( <LE> <STATEMENT> )*
<LE> ::= ‘\n’ | ‘;’
<BRACKETS-SEQ> ::= { <SEQUENCE> }
<BRACK-OR-STAT> ::= <BRACKETS-SEQ> | <STATEMENT>
<STATEMENT> ::= ε |
<DECL-STAT>
<ASSIGNMENT-STAT> |
<IF-STAT> |
<DO-UNTIL-STAT> |
<WHILE-STAT> |
<RETURN-STAT> |
<PRINT-STAT> |
<INPUT-STAT> |
<FUNC-STAT> |
<CALL-STAT> |
<COMMENT-STAT>
< DECL-STAT > ::= LET ASSIGNMENT-STAT (,DECLaraTION)*
< DECLaraTION > ::= ID | ASSIGNMENT-STAT
<ASSIGNMENT-STAT> ::= ID = <EXPRESSION>

...and others (irrelevant,read on)

输入文件

LET a,a1=2,n,i,max1=-1
a = 5;
b = 4;

使用下面评论回复中提供的修复程序,我仍然收到此错误

Starting parse
Entering state 0
Reading a token: --(end of buffer or a NUL)
--accepting rule at line 29 ("LET")
Next token is token LET ()
Shifting token LET ()
Entering state 1
Reading a token: --accepting rule at line 100 (" ")
--accepting rule at line 98 ("a")
Next token is token IDENTIFIER ( = (null))
Shifting token IDENTIFIER ( = (null))
Entering state 23
Reducing stack by rule 2 (line 43):
   $1 = token IDENTIFIER ( = (null))
-> $$ = nterm identifier ()
Stack Now 0 1
Entering state 24
Reading a token: --accepting rule at line 78 (",")
Next token is token COMMA ()
Reducing stack by rule 28 (line 107):
   $1 = nterm identifier ()
-> $$ = nterm declaration ()
Stack Now 0 1
Entering state 25
Next token is token COMMA ()
Shifting token COMMA ()
Entering state 49
Reading a token: --accepting rule at line 100 (" ")
--accepting rule at line 98 ("a1")
Next token is token IDENTIFIER ( = (null))
Shifting token IDENTIFIER ( = (null))
Entering state 23
Reducing stack by rule 2 (line 43):
   $1 = token IDENTIFIER ( = (null))
-> $$ = nterm identifier ()
Stack Now 0 1 25 49
Entering state 24
Reading a token: --accepting rule at line 87 ("=")
Next token is token ASSIGNMENT ()
Shifting token ASSIGNMENT ()
Entering state 48
Reading a token: --accepting rule at line 95 ("2")
Next token is token CONSTINT ()
Reducing stack by rule 88 (line 264):
-> $$ = nterm optional_sign ()
Stack Now 0 1 25 49 24 48
Entering state 62
Next token is token CONSTINT ()
Shifting token CONSTINT ()
Entering state 97
Reducing stack by rule 76 (line 249):
   $1 = token CONSTINT ()
-> $$ = nterm constant ()
Stack Now 0 1 25 49 24 48 62
Entering state 104
Reducing stack by rule 72 (line 245):
   $1 = nterm constant ()
-> $$ = nterm factor ()
Stack Now 0 1 25 49 24 48 62
Entering state 103
Reading a token: --accepting rule at line 78 (",")
Next token is token COMMA ()
Reducing stack by rule 69 (line 235):
-> $$ = nterm mul_oper_op ()
Stack Now 0 1 25 49 24 48 62 103
Entering state 130
Reducing stack by rule 71 (line 241):
   $1 = nterm factor ()
   $2 = nterm mul_oper_op ()
-> $$ = nterm term ()
Stack Now 0 1 25 49 24 48 62
Entering state 102
Next token is token COMMA ()
Reducing stack by rule 66 (line 226):
-> $$ = nterm add_oper_op ()
Stack Now 0 1 25 49 24 48 62 102
Entering state 126
Reducing stack by rule 68 (line 232):
   $1 = nterm optional_sign ()
   $2 = nterm term ()
   $3 = nterm add_oper_op ()
-> $$ = nterm expression ()
Stack Now 0 1 25 49 24 48
Entering state 78
Reducing stack by rule 30 (line 112):
   $1 = nterm identifier ()
   $2 = token ASSIGNMENT ()
   $3 = nterm expression ()
-> $$ = nterm assignment_stat ()
Stack Now 0 1 25 49
Entering state 26
Reducing stack by rule 29 (line 108):
   $1 = nterm assignment_stat ()
-> $$ = nterm declaration ()
Stack Now 0 1 25 49
Entering state 79
Next token is token COMMA ()
Shifting token COMMA ()
Entering state 49
Reading a token: --accepting rule at line 100 (" ")
--accepting rule at line 98 ("n")
Next token is token IDENTIFIER ( = (null))
Shifting token IDENTIFIER ( = (null))
Entering state 23
Reducing stack by rule 2 (line 43):
   $1 = token IDENTIFIER ( = (null))
-> $$ = nterm identifier ()
Stack Now 0 1 25 49 79 49
Entering state 24
Reading a token: --accepting rule at line 78 (",")
Next token is token COMMA ()
Reducing stack by rule 28 (line 107):
   $1 = nterm identifier ()
-> $$ = nterm declaration ()
Stack Now 0 1 25 49 79 49
Entering state 79
Next token is token COMMA ()
Shifting token COMMA ()
Entering state 49
Reading a token: --accepting rule at line 100 (" ")
--accepting rule at line 98 ("i")
Next token is token IDENTIFIER ( = (null))
Shifting token IDENTIFIER ( = (null))
Entering state 23
Reducing stack by rule 2 (line 43):
   $1 = token IDENTIFIER ( = (null))
-> $$ = nterm identifier ()
Stack Now 0 1 25 49 79 49 79 49
Entering state 24
Reading a token: --accepting rule at line 78 (",")
Next token is token COMMA ()
Reducing stack by rule 28 (line 107):
   $1 = nterm identifier ()
-> $$ = nterm declaration ()
Stack Now 0 1 25 49 79 49 79 49
Entering state 79
Next token is token COMMA ()
Shifting token COMMA ()
Entering state 49
Reading a token: --accepting rule at line 100 (" ")
--accepting rule at line 98 ("max1")
Next token is token IDENTIFIER ( = (null))
Shifting token IDENTIFIER ( = (null))
Entering state 23
Reducing stack by rule 2 (line 43):
   $1 = token IDENTIFIER ( = (null))
-> $$ = nterm identifier ()
Stack Now 0 1 25 49 79 49 79 49 79 49
Entering state 24
Reading a token: --accepting rule at line 87 ("=")
Next token is token ASSIGNMENT ()
Shifting token ASSIGNMENT ()
Entering state 48
Reading a token: --accepting rule at line 95 ("-1")
Next token is token CONSTINT ()
Reducing stack by rule 88 (line 264):
-> $$ = nterm optional_sign ()
Stack Now 0 1 25 49 79 49 79 49 79 49 24 48
Entering state 62
Next token is token CONSTINT ()
Shifting token CONSTINT ()
Entering state 97
Reducing stack by rule 76 (line 249):
   $1 = token CONSTINT ()
-> $$ = nterm constant ()
Stack Now 0 1 25 49 79 49 79 49 79 49 24 48 62
Entering state 104
Reducing stack by rule 72 (line 245):
   $1 = nterm constant ()
-> $$ = nterm factor ()
Stack Now 0 1 25 49 79 49 79 49 79 49 24 48 62
Entering state 103
Reading a token: --accepting rule at line 76 ("
")
Next token is token NEWLINE ()
Reducing stack by rule 69 (line 235):
-> $$ = nterm mul_oper_op ()
Stack Now 0 1 25 49 79 49 79 49 79 49 24 48 62 103
Entering state 130
Reducing stack by rule 71 (line 241):
   $1 = nterm factor ()
   $2 = nterm mul_oper_op ()
-> $$ = nterm term ()
Stack Now 0 1 25 49 79 49 79 49 79 49 24 48 62
Entering state 102
Next token is token NEWLINE ()
Reducing stack by rule 66 (line 226):
-> $$ = nterm add_oper_op ()
Stack Now 0 1 25 49 79 49 79 49 79 49 24 48 62 102
Entering state 126
Reducing stack by rule 68 (line 232):
   $1 = nterm optional_sign ()
   $2 = nterm term ()
   $3 = nterm add_oper_op ()
-> $$ = nterm expression ()
Stack Now 0 1 25 49 79 49 79 49 79 49 24 48
Entering state 78
Reducing stack by rule 30 (line 112):
   $1 = nterm identifier ()
   $2 = token ASSIGNMENT ()
   $3 = nterm expression ()
-> $$ = nterm assignment_stat ()
Stack Now 0 1 25 49 79 49 79 49 79 49
Entering state 26
Reducing stack by rule 29 (line 108):
   $1 = nterm assignment_stat ()
-> $$ = nterm declaration ()
Stack Now 0 1 25 49 79 49 79 49 79 49
Entering state 79
Next token is token NEWLINE ()
Reducing stack by rule 25 (line 97):
-> $$ = nterm comma_declaration_op ()
Stack Now 0 1 25 49 79 49 79 49 79 49 79
Entering state 117
Reducing stack by rule 26 (line 99):
   $1 = token COMMA ()
   $2 = nterm declaration ()
   $3 = nterm comma_declaration_op ()
-> $$ = nterm comma_declaration_op ()
Stack Now 0 1 25 49 79 49 79 49 79
Entering state 117
Reducing stack by rule 26 (line 99):
   $1 = token COMMA ()
   $2 = nterm declaration ()
   $3 = nterm comma_declaration_op ()
-> $$ = nterm comma_declaration_op ()
Stack Now 0 1 25 49 79 49 79
Entering state 117
Reducing stack by rule 26 (line 99):
   $1 = token COMMA ()
   $2 = nterm declaration ()
   $3 = nterm comma_declaration_op ()
-> $$ = nterm comma_declaration_op ()
Stack Now 0 1 25 49 79
Entering state 117
Reducing stack by rule 26 (line 99):
   $1 = token COMMA ()
   $2 = nterm declaration ()
   $3 = nterm comma_declaration_op ()
-> $$ = nterm comma_declaration_op ()
Stack Now 0 1 25
Entering state 50
Next token is token NEWLINE ()
Shifting token NEWLINE ()
Entering state 42
Reducing stack by rule 10 (line 70):
   $1 = token NEWLINE ()
-> $$ = nterm le ()
Stack Now 0 1 25 50
Entering state 80
Reducing stack by rule 27 (line 103):
   $1 = token LET ()
   $2 = nterm declaration ()
   $3 = nterm comma_declaration_op ()
   $4 = nterm le ()
-> $$ = nterm decl_stat ()
Stack Now 0
Entering state 14
Reading a token: --accepting rule at line 98 ("a")
Next token is token IDENTIFIER ( = (null))
Shifting token IDENTIFIER ( = (null))
Entering state 23
Reducing stack by rule 2 (line 43):
   $1 = token IDENTIFIER ( = (null))
-> $$ = nterm identifier ()
Stack Now 0 14
Entering state 46
Reading a token: --accepting rule at line 100 (" ")
--accepting rule at line 87 ("=")
Next token is token ASSIGNMENT ()
Shifting token ASSIGNMENT ()
Entering state 48
Reading a token: --accepting rule at line 100 (" ")
--accepting rule at line 95 ("5")
Next token is token CONSTINT ()
Reducing stack by rule 88 (line 264):
-> $$ = nterm optional_sign ()
Stack Now 0 14 46 48
Entering state 62
Next token is token CONSTINT ()
Shifting token CONSTINT ()
Entering state 97
Reducing stack by rule 76 (line 249):
   $1 = token CONSTINT ()
-> $$ = nterm constant ()
Stack Now 0 14 46 48 62
Entering state 104
Reducing stack by rule 72 (line 245):
   $1 = nterm constant ()
-> $$ = nterm factor ()
Stack Now 0 14 46 48 62
Entering state 103
Reading a token: --accepting rule at line 77 (";")
Next token is token SEMICOLON ()
Reducing stack by rule 69 (line 235):
-> $$ = nterm mul_oper_op ()
Stack Now 0 14 46 48 62 103
Entering state 130
Reducing stack by rule 71 (line 241):
   $1 = nterm factor ()
   $2 = nterm mul_oper_op ()
-> $$ = nterm term ()
Stack Now 0 14 46 48 62
Entering state 102
Next token is token SEMICOLON ()
Reducing stack by rule 66 (line 226):
-> $$ = nterm add_oper_op ()
Stack Now 0 14 46 48 62 102
Entering state 126
Reducing stack by rule 68 (line 232):
   $1 = nterm optional_sign ()
   $2 = nterm term ()
   $3 = nterm add_oper_op ()
-> $$ = nterm expression ()
Stack Now 0 14 46 48
Entering state 78
Reducing stack by rule 30 (line 112):
   $1 = nterm identifier ()
   $2 = token ASSIGNMENT ()
   $3 = nterm expression ()
-> $$ = nterm assignment_stat ()
Stack Now 0 14
Entering state 47
Reducing stack by rule 16 (line 85):
   $1 = nterm decl_stat ()
   $2 = nterm assignment_stat ()
-> $$ = nterm statement ()
Stack Now 0
Entering state 13
Next token is token SEMICOLON ()
Shifting token SEMICOLON ()
Entering state 43
Reducing stack by rule 11 (line 71):
   $1 = token SEMICOLON ()
-> $$ = nterm le ()
Stack Now 0 13
Entering state 45
Reading a token: --accepting rule at line 76 ("
")
Next token is token NEWLINE ()
Reducing stack by rule 15 (line 83):
-> $$ = nterm statement ()
Stack Now 0 13 45
Entering state 77
Next token is token NEWLINE ()
Shifting token NEWLINE ()
Entering state 42
Reducing stack by rule 10 (line 70):
   $1 = token NEWLINE ()
-> $$ = nterm le ()
Stack Now 0 13 45 77
Entering state 45
Reading a token: --accepting rule at line 98 ("b")
Next token is token IDENTIFIER ( = (null))
Reducing stack by rule 15 (line 83):
-> $$ = nterm statement ()
Stack Now 0 13 45 77 45
Entering state 77
Next token is token IDENTIFIER ( = (null))
Reducing stack by rule 7 (line 60):
-> $$ = nterm le_statement_op ()
Stack Now 0 13 45 77 45 77
Entering state 116
Reducing stack by rule 8 (line 62):
   $1 = nterm le ()
   $2 = nterm statement ()
   $3 = nterm le_statement_op ()
-> $$ = nterm le_statement_op ()
Stack Now 0 13 45 77
Entering state 116
Reducing stack by rule 8 (line 62):
   $1 = nterm le ()
   $2 = nterm statement ()
   $3 = nterm le_statement_op ()
-> $$ = nterm le_statement_op ()
Stack Now 0 13
Entering state 44
Reducing stack by rule 9 (line 66):
   $1 = nterm statement ()
   $2 = nterm le_statement_op ()
-> $$ = nterm sequence ()
Stack Now 0
Entering state 12
Reducing stack by rule 5 (line 56):
   $1 = nterm sequence ()
-> $$ = nterm statement_or_func ()
Stack Now 0
Entering state 11
Next token is token IDENTIFIER ( = (null))
Reducing stack by rule 3 (line 50):
-> $$ = nterm newline_statement_or_func_op ()
Stack Now 0 11
Entering state 41
Reducing stack by rule 1 (line 38):
   $1 = nterm statement_or_func ()
   $2 = nterm newline_statement_or_func_op ()
-> $$ = nterm program ()
Stack Now 0
Entering state 10
Next token is token IDENTIFIER ( = (null))
Error: popping nterm program ()
Stack Now 0
Cleanup: discarding lookahead token IDENTIFIER ( = (null))
Stack Now 0

^
Syntax error,unexpected IDENTIFIER,expecting $end

Process finished with exit code 1

从我的角度来看,我不明白这有什么意义。它找到了一个标识符(由于 yyerror 函数损坏,我无法弄清楚它指的是哪个令牌),它是 null (?) 并且它期待一个 EOF(?)

Next token is token IDENTIFIER ( = (null))

在接受 'b' 标识符后,它错误地将(某些东西?我不明白是哪个字符)标识为标识符,而不是在 EQUALS、CONSTINT 和 LE 标记上进行识别。

编辑: 我添加了更多调试信息(行、列、块)

^
Syntax error,expecting $end Line: 3 Column: 0   Block: 0

输入如下:

SUB fib (n,ret)
{
LET
low,high;
IF ( n == 0 OR n == 1)
THEN
ret = n;
ELSE
{
high = call fib(n-1,high);
low = call fib(n-2,low)
ret = high + low;
}
RETURN ret
}

我收到此错误

ret
^
Syntax error,expecting $end Line: 3 Column: 0   Block: 0

这显然与上述相同。出于某种原因,在分号之后,它总是期望一个 $end。我无法弄清楚这种行为是从哪里引起的。据我所知,它将堆栈减少到状态 0,即:

State 0

    0 $accept: . program $end

...但问题是我不明白为什么它会减少到这个,或者如何解决这个问题。

对这三点的任何帮助都非常感谢。

非常感谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)