1

入力ファイルで発生した構文エラーの数をコードで出力したいと考えています。これが私のコードです:

%{
#include <stdio.h>
#include <math.h>
void yyerror(char *);
extern int yylval;
extern FILE *yyin;
extern FILE *yyout;
extern yylineno;
extern int yyparse(void);
extern int yylex(void);
extern int yywrap() { return 1; }
extern char* yytext;
int errors;
%}

%debug
%start m_class


%token IF ELSE INT CHAR CLASS NEW GURISE VOID WHILE
%token PUBLIC PROTECTED PRIVATE STATIC FINAL ABSTRACT
%token PLUS MINUS MUL DIV MODULO
%token EQ NEQ GRT LT GREQ LEQ
%token OR AND NOT
%token AR_PAR DEK_PAR AR_AGK DEK_AGK AR_STRO DEK_STRO
%token SEMICOLON ANATHESI COMA
%token MY_INT SINT MY_CHAR ID

%right          ANATHESI
%left           OR AND
%nonassoc       EQ NEQ GRT LT GREQ LEQ
%left           PLUS MINUS MUL DIV MODULO
%right          NOT
%right          "then" ELSE

%%

m_class: m_class class_declaration
        | class_declaration
        | error "\n" {yyerrok; errors++; yyclearin;}
        ;

class_declaration: CLASS ID class_body
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;

class_body: AR_STRO variable_declaration constructor method_declaration DEK_STRO
        | error "\n" {yyerrok; errors++;  yyclearin;}
  ;

variable_declaration:variable variable_declaration
        |variable
        |array_declaration
        |array_declaration variable_declaration
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;

variable:  var_type ID SEMICOLON
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;

var_type: INT
        |CHAR
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;

array_declaration: ID ANATHESI NEW var_type AR_AGK MY_INT DEK_AGK SEMICOLON
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;


constructor: modifier ID AR_STRO variable_declaration DEK_STRO
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;

modifier: PUBLIC
        | PROTECTED
        | PRIVATE
        | STATIC
        | FINAL
        | ABSTRACT
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;
method_declaration: modifier meth_type ID parameters meth_body
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;

meth_type: VOID
        | var_type
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;

parameters: AR_PAR par_body DEK_PAR
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;

par_body: var_type ID
        | par_body COMA var_type ID
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;

meth_body: AR_STRO bodybuilder DEK_STRO
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;

bodybuilder: statement GURISE expression SEMICOLON
        |statement bodybuilder
        |statement
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;

statement: anathesh
        | if_statement
        | while_statement
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;

anathesh:atath SEMICOLON
        | atath numeric_expression SEMICOLON
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;

atath: ID ANATHESI orisma
        |ID AR_AGK MY_INT DEK_AGK ANATHESI orisma
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;

orisma: ID
        |MY_INT
        |SINT
        |MY_CHAR
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;

expression: testing_expression
        | numeric_expression
        | logical_expression
        | ID
        | MY_INT
        | SINT
        | MY_CHAR
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;

numeric_expression:  expression PLUS  expression
        | expression MINUS expression
        | expression MUL expression
        | expression DIV expression
        | expression MODULO expression
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;

testing_expression: expression EQ expression
        | expression NEQ expression
        | expression GRT expression
        | expression LT expression
        | expression GREQ expression
        | expression LEQ expression
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;
logical_expression: expression OR expression
        | expression AND expression
        | expression NOT expression
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;

if_statement: IF sin8iki statement      %prec "then"
        | IF sin8iki statement ELSE statement
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;


sin8iki: AR_PAR testing_expression DEK_PAR
        | AR_PAR logical_expression DEK_PAR
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;

while_statement: WHILE sin8iki statement
        | error "\n" {yyerrok; errors++;  yyclearin;}
        ;


%%


void yyerror(char *s) {
        errors++;
printf("\n------- ERROR AT LINE #%d.\n\n", yylineno);
fprintf(stderr, "%d: error: '%s' at '%s', yylval=%u\n", yylineno, s, yytext, yylval);

}

int main (int argc, char **argv) {
        ++argv;
        --argc;
        errors=0;
        if (argc > 0)
              yyin = fopen (argv[0], "r");
        else
                yyin = stdin;
        yyout = fopen ("output","w");
        yyparse();
        if(errors==0)
                printf("komple");
        else
                printf("la8oi: %d", errors);
        return 0;
}

yyerrok を変更しようとしましたが、できないようです。また、yyparse を for ループに入れようとしました。入力ファイルには5つの構文エラーがありますが、1つしか出力されません!!!!! 何か案は??????

4

3 に答える 3

0

変数 yynerrs には、これまでに発生した構文エラーの数が含まれています。通常、この変数はグローバルです。ただし、純粋なパーサーを要求すると、アクションのみがアクセスできるローカル変数になります。

リンクを参照

于 2015-03-12T13:47:12.597 に答える