- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

[-+]?([0-9]*\.?[0-9]+|[0-9]+\.)(E(+|-)?[0-9]+)?

I have just been reading a book on FLEX and BISON, the book suggests that one form of Fortran numbers requires a very complex pattern spelt out above.

The book is interesting.

Link Copied

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Technically, that first [-+]? is not part of a number in Fortran. See here. But this is the same pattern used in many languages.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

For example, consider the surprisingly difficult job of writing a pattern to match Fortran-style numbers, which consist of an optional sign, a string of digits that may contain a decimal point, optionally an exponent that is the letter `E`

, an optional sign, and a string of digits. A pattern for an optional sign and a string of digits is simple enough:

[-+]?[0-9]+

Levine's book on Flex and Bison discusses in some detail the issue of Fortran and other language numbers. it is quite interesting, what is rather interesting is the ability to write simple programs using grammar rules rather than coding, the development of the rules to read all those numbers recently in a csv file brings to mind the horror's, but this is a bit simpler - pity we do not have this option in Fortran.

/* even more like Unix wc */ %option noyywrap %{ int chars = 0; int words = 0; int lines = 0; %} %% [a-zA-Z]+ { words++; chars += strlen(yytext); } \n { chars++; lines++; } . { chars++; } %%

It is all just learning and fun.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

`[-+]?([0-9]*\.?[0-9]+|[0-9]+\.)(E(+|-)?[0-9]+)?`

BNF metasyntax used by the Fortran standard is, in the instance of a REAL literal constant, helpful in understanding the pattern.

In Fortran, a different aspect of the above quoted pattern is the use of `D` to denote double precision. This does get nonintuitive relative to any math instruction during one's education and it also gets in the way of electronic exchange of data with/from the Fortran processor

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Interestingly the B in BNF is the developer of Fortran. A Bison and Flex for Fortran would be interesting. But the syntax for Bison and Flex is obscure and the manuals all use the same samples from the original and they exactly repeat the instructions.

Like all things improving one's knowledge helps with the Fortran.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page