When I pass a file to bison (the usual way) it works fine, it parses the file and checks the grammar. However when I pass the same string with:
yy_scan_string(string);
yylex();
yyparse();
It crashes after the first token, it says it is expecting $end. Should I somehow reset something? The examples on here don’t say anything.
You’re using Bison incorrectly. When using Bison, you only call yyparse(). You don’t call yylex()–that function will be called by yyparse() whenever it needs a token.
What you’re doing is setting string as the input, calling yylex which finds a token and advances the input stream. Then you call yyparse(). This yyparse() call is expecting to find something on the input from the current point (after first token which was grabbed by yylex()) to the end of input which reduces to a sentence in your grammar.
The problem is that it already missed a token because of your yylex() call.