0

I am trying an example which checks the correctness of for loop in flex and bison. Here is the flex file:

%{
    #include "ch.tab.h"
    extern int yylval;
%}

%%
for {return (FOR);}
"(" {return (OPBR);}
")" {return (CLBR);}
";" {return (SEMIC);}
"=" {return (EQU);}
"<"|">" {return (RELOP);}
"++" {return (INC);}
"--" {return (DEC);}
[a-zA-Z]+ {yylval=yytext[0];return(ID);}
[0-9]+ {yylval=atoi(yytext);return(NUM);}
%%

int yywrap()

{

  return 1;

}

and bison file:

%{
    #include <stdio.h>
    int flag=0;
%}

%token FOR OPBR CLBR SEMIC RELOP EQU ID NUM RELOP INC DEC

%%
S:FOR OPBR E1 SEMIC E2 SEMIC E3 CLBR {printf("Accepted!");flag=1;}
 ;

E1: ID EQU ID
  | ID EQU NUM
  ;

E2: ID RELOP ID
  | ID RELOP NUM
  ;

E3: ID INC
  | ID DEC
  ;

%%

int main()
{
    return yyparse();
}

yyerror(const char *msg)
{
    if(flag==0);
    printf("Not Accepted!");
}

Everything runs fine the only the first time. When I run an example for(i = 0; i < 2; i++), the first time it prints Accepted, when I run the same example again without closing the command prompt I get Not Accepted!. Why?

Is there a way to make the exe such that as long as I am inputting correct syntax, it will keep on saying Accepted and waits for another new input?

1 Answer 1

1

Because your grammar accepts precisely one for statement, which therefore must be the entire input.

If you want to be able to accept multiple statements, you need your grammar to produce multiple statements. So you might use a non-terminal like:

input: /* Empty */ | input S;

which will accept any number (including 0) of Ss. (Of course, you need that to be the start symbol, so you either need a %start input directive, or you need to put that production before any other productions.)

That will continue to parse as long as valid statements are entered, but it will fail when it cannot parse a statement. What you would probably prefer is to produce an error message but continue parsing when you encounter an incorrect statement. One simple way to do that is with an error production like:

S: error SEMIC { yyerror("Invalid for statement"); }

which will do error recovery by discarding tokens until it finds a semicolon, and then resume normal parsing. (In that action, I'm assuming that you modify yyerror to print the string which it is passed as an argument, rather than ignoring it.)


By the way, normal bison style is to use UPPER case for tokens (FOR), lower case for non terminals (statement), and single-character literals for single-character tokens (';').

For the single-character literals to work with flex scanners, you need to return the character rather than a token name from your scanner action:

";"  { return ';'; }

A common way to do that without having to write a large number of boilerplate rules is to put a catch-all fallback lexical rule at the end of your flex rules:

.    { return yytext[0]; }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.