Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2008-03

Patch for lexlib

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Felipe Monteiro de Carvalho

Hello,

I am writing scanners using plex, and I found a problem with the current lexlib. It has a function called yywrap which will close the input and the output, and there is no way to prevent that. I would like to parse the same file several times, and simply closing the file, reopening it and reseting it somehow doesn't work. It soon crashes. If I prevent yywrap from closing the file (only possible changing lexlib), then everything works good.

I searched how people solve this in C, and there there is some pre-processor magic which allows them declare their own yywrap function. All yywrap declarations I saw in my search would just return true, so I added a variable called yycloseiowhendone, which is initially true and when set to false will do what everyone wants when redeclaring yywrap: Just return true

Please review the following patch. Existing applications shouldn't be affected by it. If accepted, I'll apply it to trunk, thanks.

Index: h2pas/lexlib.pas
=================================================================== --- h2pas/lexlib.pas (revision 10497)
+++ h2pas/lexlib.pas (working copy)
@@ -45,6 +45,8 @@
yytext : String; (* matched text (should be considered r/o) *) yyleng : Byte (* length of matched text *)

   absolute yytext;

+yycloseiowhendone : Boolean; (* Identifies if yyinput and yyoutput
+ shall be closed when yylex ends *)

 (* I/O routines:

@@ -307,7 +309,11 @@

 function yywrap : Boolean;
   begin
-    close(yyinput); close(yyoutput);
+    if yycloseiowhendone then
+    begin
+      close(yyinput);
+      close(yyoutput);
+    end;
     yywrap := true;
   end(*yywrap*);
@@ -401,6 +407,7 @@
   end(*yyclear*);
 begin
+  yycloseiowhendone := True;
   assign(yyinput, '');
   assign(yyoutput, '');
   reset(yyinput);

Reply by email

Florian Klaempfl
to Felipe Monteiro de Carvalho
Show quoted text 13 lines
Hello, I am writing scanners using plex, and I found a problem with the current lexlib. It has a function called yywrap which will close the input and the output, and there is no way to prevent that. I would like to parse the same file several times, and simply closing the file, reopening it and reseting it somehow doesn't work. It soon crashes. If I prevent yywrap from closing the file (only possible changing lexlib), then everything works good. I searched how people solve this in C, and there there is some pre-processor magic which allows them declare their own yywrap function.

Wouldn't it be better to make yywrap a procedure variable with a default value?

Reply by email

Felipe Monteiro de Carvalho
to Florian Klaempfl

Indeed. Like this?

Index: h2pas/lexlib.pas
=================================================================== --- h2pas/lexlib.pas (revision 10497)
+++ h2pas/lexlib.pas (working copy)
@@ -115,7 +115,10 @@
file. In particular, yywrap may arrange for more input and return false in which case the yylex routine resumes lexical analysis. *)

-function yywrap : Boolean;
+type
+  yywrap_t = function (): Boolean;
+var
+  yywrap: yywrap_t;
   (* The default yywrap routine supplied here closes input and output files
      and returns true (causing yylex to terminate). *)

@@ -305,10 +308,11 @@

 (* yywrap: *)
-function yywrap : Boolean;
+function lexlib_yywrap : Boolean;
   begin
-    close(yyinput); close(yyoutput);
-    yywrap := true;
+    close(yyinput);
+    close(yyoutput);
+    lexlib_yywrap := true;
   end(*yywrap*);
 (* Internal routines: *)
@@ -401,6 +405,7 @@
   end(*yyclear*);
 begin
+  yywrap := @lexlib_yywrap;
   assign(yyinput, '');
   assign(yyoutput, '');
   reset(yyinput);

Reply by email

Michael Van Canneyt
to Florian Klaempfl: Wouldn't it be better to make yywrap a procedure variable with a default value?
Florian Klaempfl wrote 17 lines
Felipe Monteiro de Carvalho schrieb: > Hello, > > I am writing scanners using plex, and I found a problem with the > current lexlib. It has a function called yywrap which will close the > input and the output, and there is no way to prevent that. I would > like to parse the same file several times, and simply closing the > file, reopening it and reseting it somehow doesn't work. It soon > crashes. If I prevent yywrap from closing the file (only possible > changing lexlib), then everything works good. > > I searched how people solve this in C, and there there is some > pre-processor magic which allows them declare their own yywrap > function. Wouldn't it be better to make yywrap a procedure variable with a default value?

I was going to propose the same thing.

Michael.

Reply by email