Regular Expressions in Lex (Lexing Fortran)

Bill Roberts bill at hao.UUCP
Sat Jul 26 01:34:38 AEST 1986


Thanks to all who responded to my earlier query (msgs # 9080).  I finally got
it figured out the other day and thought some people might be interested in the 
solution.  To restate the problem:
  I needed a regular expression to use as part of a lex input file to dig out 
  all syntactically valid instances of f77 open statements, in their various
  guises.  That is, I want to find things like
       
	if (fred .eq. betty)
     1   open(unit=1, file=fred, .... )

     and

	open(unit=1, ....,
     #  more..............,
     @  xya, abc, and more,
	  .
	  .
	  .
     n  and finally this)

     as well as regular things like

	open (fred the open statement)

  In addition, I wanted to ignore any commented open statements.  One solution 
  is this: (note that this is a Lex input file)

C	\n"     ".
A	[ \t{C}]*
B	[ \t]
D	[^=\n]
%%
^{B}+{D}*o{A}p{A}e{A}n{A}\(.*({C}.*)*\)[ ]*\n  {
			return(OPENR);
			}
.	;

\n	;

  This seems to work on all of the cases I've stated above (are there others?).
  It even handles the cases where the 'open' word is separated by spaces, tabs,
  or newlines (as is allowed in fortran, arg!).  I hope this might be of 
  interest or help to someone out there.  It was quite enlightening (and 
  frustrating) to me.  I only had to read the Lex paper
  ("Lex - A Lexical Analyzer Generator", M.E. Lesk and E. Schmidt) n times!

							Bill Roberts
							NCAR/HAO
							Boulder, CO
							!hao!bill



More information about the Comp.unix mailing list