I'd like to use the same flex/bison scanner/parser for an interpreter and for loading a file to be interpreted. I can not get the newline parsing to work correctly in both cases.
- Interpreter: There is a prompt and I can enter commands terminated by pressing ENTER.
- File: Here is an example input file:
-----cut---------
begin(
print("well done"), 1)
----cut-------
So, there is a newline in the first line and after the '(' that should be eaten.
In my scanner.l I have
%%
[ \t] { errorLineCol += strlen(yytext); }
\n { errorLineNumber++;
errorLineCol = 0; }
("-"?[0-9])[0-9]* { errorLineCol += strlen(yytext);
yylval = stringToInteger(yytext);
return TINTEGER; }
.....
This then works for the file scenario but not for the interpreter. I the have to press and additional Ctrl+D after the ENTER. If I change to
\n { errorLineNumber++;
errorLineCol = 0;
return 0; }
Then the interpreter works but not the file reading; which then stops after the first newline it encounters. What is a good way to tackle this issue?
Edit:
Here is the top level of the parser:
input: uexpr { parseValue = $1; }
| /* empty */ { parseValue = myNull; }
| error { parseValue = myNull; }
;
uexpr: list
| atom
;
Possible Solution: seems to be to use
\n { errorLineNumber++;
errorLineCol = 0;
if (yyin == stdin) return 0; }