I am trying to write a grammatical recognizer using flex and bison to determine if an input string is in L(G), where the language is a union of:
L(G) = {a^i b^j c^k d^l e^m} where i,j,k,l,m > 0 and i=m and k=l
and
L(G) = {e^i d^j c^k b^l a^m} where i,j,k,l,m > 0 and i=2m k=3l and j=2
Right now I have it working fine, but only when using the tokens in the languages. If I include any other token it seems to get ignored and the test passes or fails based on the other allowed tokens. This is problematic because it allows for strings such as "abcdef" to pass the parse even though "f" is not in the language.
The erroneous input that I am testing now is "abcdef". The "abcde" part is correct and gives the correct output, but adding the "f" to the end causes both the syntax error message from yyerror("syntax error"), and the "congratulations; parse succeeded" print statement from main to print.
Using "fabcde" does the same thing I described above. It is giving me the error but it's also giving me the success print statement. I'm using "if(yyparse() == 0))" to print the success statement in main and I'm thinking that might be the culprit here, although I had the same issues when I moved the print statements into the .y file and just used yyparse() and return(1) in main.
Here is my .in file (minus includes):
%%
a return A;
b return B;
c return C;
d return D;
e return E;
. yyerror("syntax error\n\nSorry, Charlie, input string not in L(G)\n"); /* working but still prints success message too */
%%
Here is my .y file (minus includes):
%token A
%token B
%token C
%token D
%token E
%% /* Grammar Rules */
string: as bs cs ds es
{
if(($1 == $5) && ($3 == $4)) {
return(0);
}
else
{
return(-1);
}
}
;
string: es ds cs bs as
{
if(($1 == (2 * $5) && ($3 == (3 * $4)) && ($2 = 2)) {
return(0);
}
else
{
return(-1);
}
}
;
as: A as {$$ = $2 +1;}
;
as: A {$$ = 1;}
;
bs: B bs {$$ = $2 +1;}
;
bs: B {$$ = 1;}
;
cs: C cs {$$ = $2 +1;}
;
cs: C {$$ = 1;}
;
ds: D ds {$$ = $2 +1;}
;
ds: D {$$ = 1;}
;
es: E es {$$ = $2 +1;}
;
es: E {$$ = 1;}
;
%%
my .c file is simple and just returns "congratulations; parse successful" if yyparse() == 0, and "input string is not in L(G)" otherwise.
Everything works perfectly fine when the input strings only include a, b, c, d, and e. I just need to figure out how to make the parser give a syntax error without a success statement if there's any token besides them in the input string.
Here is an image that will help show my issue: The first two parses work as intended. The third one shows my issue.
f
, for example, to produce an error return. – ricireturn yyerror("syntax error\n\nSorry, Charlie, input string not in L(G)\n");
unless youryyerror
function actually returns something (which most of them do not). – rici