New line scan for bison

I would like to use the same flex / bison scanner / parser for the interpreter and for loading the file for interpretation. I cannot get newline parsing to work in both cases.

  • Interpreter: There is a prompt, and I can enter the commands completed by pressing ENTER.
  • File: Here is an example input file:

----- shorten ---------

begin(
    print("well done"), 1)

---- cut -------

So, in the first line there is a new line and after the '(', which you need to eat.

In my scanner. I

%%
[ \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 script, but not for the interpreter. I have to press and add Ctrl + D after ENTER. If i switch to

\n                          {   errorLineNumber++;
                                errorLineCol = 0;
                                return 0; }

Then the interpreter works, but not reading the file; which then stops after the first new line that it encounters. What is a good way to solve this problem?

Edit:

:

input: uexpr                        {   parseValue = $1; }
    | /* empty */                   {   parseValue = myNull; }
    | error                         {   parseValue = myNull; }
    ;

uexpr: list                          
    | atom                         
    ;

:, ,

\n                          {   errorLineNumber++;
                                errorLineCol = 0;
                                if (yyin == stdin) return 0; }
+1
2

, parser ypparse , .

:

language : commands ;

commands : command commands | /* empty */ ;

, script ( Ctrl-D). :

loop:
  print("prompt>")
  yyparse()
  if (empty statement)
    break

, yyparse script .

return 0; , 0 EOF , script.

\n . ( ) , , , yyparse . , , script, yyparse .

, , . return 0; , . , , script . , , yyparse.

, , , : yyparse . ( lexer , , 0). I. - (, ). ( ), yyparse , yyparse . , (, ), , . , , scanf ( : scanf - , , ).

- , , yyparse, . . . FILE * . ( YY_INPUT). , , , . libedit GNU readline.

+2

ENTER , \n. 0 , ( ^ D ). , \n.

ETA: , ENTER. , \n.

+2

All Articles