Handling multiple errors in Bison parser

18 views Asked by At

I'm working on a project where I have a Bison parser that parses a custom language. I've encountered an issue where the parser stops after encountering the first error in the input file, but I need it to continue parsing and capture multiple errors.

When the parser encounters an error (e.g., a syntax error), it calls the yyerror() function to report the error. However, after reporting the first error, the parser stops processing further input and returns to the caller. As a result, I'm only able to capture and report one error at a time, even if there are multiple errors in the input file. for example : In my input file, I have a series of variable declarations, but there are intentional syntax errors to test the error handling mechanism. Here's a simplified version of the input file (.txt) :

    int a;
    int b;
    int c;
    int d, e,f;
    int 
    flot
    float g, i,k;
    int
    k
    int b;

I expect my Bison parser to report syntax errors on each line where they occur. For example, in this input file, I expect to see errors reported for lines 5, 6, 7, and 8.

Currently, my Bison parser only reports the first syntax error encountered

I'm seeking guidance on how to modify my parser to handle multiple errors and continue parsing until the end of the input file. Ideally, I'd like the parser to report all encountered errors and provide information on their respective line numbers.

Here's a simplified version of my Flex and Bison files:

file.l: `

`%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Part_B.tab.h"
%}

// Lexical rules...

%%

// Lexical rules...

%%

int yywrap() {
    return 1;
}

int yyerror(FILE* fp, const char* msg) {
    fprintf(stderr, "Error | Line: %d %s\n", yylineno, msg);
    return 0;
}

file.y:

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Part_B.tab.h"

// External declarations...

%}

// Bison rules...

%%

// Bison rules...

%%

int main() {
   //file 
    FILE *input_file = fopen(file_path, "r");
    if (input_file == NULL) {
        fprintf(stderr, "Error: Unable to open input file");
        return 1;
    }
    // Redirect input to the file
    yyrestart(input_file);
    


   yyparse(input_file);

    return 0;
}
0

There are 0 answers