Yacc%定义parse.error verbose生成错误

时间:2018-01-07 13:11:38

标签: yacc lex

当我试图从我的“语法错误”中获得更多时,我似乎使用了这么多网站上描述的方式,但由于某种原因,所有人似乎都会创建自己的错误。

我在输入文件的第5行得到了标准的“语法错误”...所以我想添加更好的错误处理,以便我可以看到究竟是什么问题。但

var array = [{
      "name": "Field 1",
      "carac": "Format carte mère",
      "caracvalue": [
        "1",
        "2"
      ]
    },
    {
      "name": "Field 2",
      "carac": "Format carte mère",
      "caracvalue": [
        "3",
        "4"
      ]
    }
  ],
  collection = {
    caracvalue: []
  };

array.forEach(function(obj) {
  collection.caracvalue = collection.caracvalue.concat(obj.caracvalue);
});

console.log(collection);

然而,它给了我这个;

%define parse.error verbose

以下是我的文件,只要你保持建设性,随意评论更多然后只是错误部分,欢迎任何帮助:) (只要错误也得到修复:P) 提前谢谢!

lex文件;

error: %define variable 'parse.error' is not used

yacc文件;

%option nounput yylineno
%{
#include "yaccTest.tab.h"
void InvalidToken();
void extern yyerror (char *s);
%}

whitespace      [ \t\r\v\f]
linefeed        \n

%%

";"                     {return SEMICOLON;}
"="                     {return EQ;}
"+"                     {return PLUS;}
"-"                     {return MINUS;}
"*"                     {return MULTIPLY;}
"/"                     {return DEVIDE;}
"("                     {return BO;}
")"                     {return BC;}
"^"                     {return POWER;}

"print"                 {return PRINT;}

[a-zA-Z][a-zA-Z0-9]*    {yylval.charValue = yytext[0]; return IDENTIFIER;}
[0-9]+                  {yylval.intValue = atoi(yytext); return NUMBER;}
{whitespace}            {;}
.                       {InvalidToken();}

%%

void yyerror(char *s) {
fprintf(stderr, "\nERROR ON LINE %d : \n %s\n", yylineno, s);
exit(0);
}

void InvalidToken(){
printf("ERROR ON LINE %d : \n Invalid Token %s\n", yylineno,yytext);
exit(0);
}

int yywrap (void) {return 1;}

输入文件;

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

int getVariableValue(char varID);
extern int yylineno;
int varIDs[52] = {0};
int varValues[52] = {0};
%}

%define parse.lac full
%define parse.error verbose

%union YYSTYPE {int intValue; char charValue;}

%token COLON SEMICOLON ST SE EQ GE GT PLUS MINUS MULTIPLY DEVIDE BO BC CBO CBC POWER LOOP PRINT
%token <intValue> NUMBER
%token <charValue> IDENTIFIER CHAR

%type <charValue> declaration expression
%type <intValue> numval

%right EQ
%left PLUS MINUS
%left MULTIPLY DEVIDE
%left POWER

%%

declaration : IDENTIFIER EQ expression
| declaration IDENTIFIER EQ expression
;

expression : numval SEMICOLON
| PRINT BO numval BC SEMICOLON              {printf("Printing");}
;

numval : NUMBER                 {$$ = $1;}
| NUMBER PLUS NUMBER        {$$ = $1 + $3;}
| NUMBER MINUS NUMBER       {$$ = $1 - $3;}
| NUMBER MULTIPLY NUMBER    {$$ = $1 * $3;}
| NUMBER DEVIDE NUMBER      {$$ = $1 / $3;}
| NUMBER POWER NUMBER       {int i;int j = $1;for(i = 1; i < $3; i++){j=j*$1;};$$ = j;}
;

%%

int getVariableValue(char varID) {
int i, j, localTemp;
for (i=0;i<((sizeof(varIDs)/sizeof(varIDs[0])));i++) {
    if (varID == varIDs[i]) {
        localTemp = varValues[i];
    }
}
return localTemp;
}

int setVariableValue(char varID, int varValue) {
    int i, varPresent = 0;
    for (i=0;i<((sizeof(varIDs)/sizeof(varIDs[0])));i++) {
        if (varID == varIDs[i]) {
            varValues[i] = varValue;
            varPresent = 1;
        }
    }

    if (varPresent == 0) {
        for (i=0;i<((sizeof(varIDs)/sizeof(varIDs[0])));i++) {
            if (&(varIDs[i]) == NULL) {
                if (&(varValues[i]) == NULL) {
                    varIDs[i] = varID;
                    varValues[i] = varValue;
                }
                else {
                    missingVarIDError(varID, varValue);
                }
            }
            else {
                notEnoughStorageError(varID, varValue);
            }
        }
    }
}

int missingVarIDError(char *id, int val){
printf("\nERROR ON LINE %d : \nIdentifier '%s' not found, but assigned location DOES have a value; %s",yylineno,id,val);
exit(0);
}

int notEnoughStorageError(char *id, int val){
printf("\nERROR ON LINE %d : \nIdentifier '%s' did not fit in StorageArray, '%3' not stored!",yylineno,id,val);
exit(0);
}

int main (void) {
return yyparse ( );
return 0;
}

0 个答案:

没有答案