Archive

Posts Tagged ‘dimensional analysis’

Parsing Fortran 95

December 20th, 2009 Derek-Jones No comments

I have been looking at doing some dimensional analysis of the Climategate code and so needed a Fortran parser.

The last time I used Fortran in anger the modern compilers were claiming conformance to the 1977 standard and since then we have had Fortran 90 (with a minor revision in 95) and Fortran 03. I decided to take the opportunity to learn something about the new features by writing a Fortran parser that did not require a symbol table.

The Eli project had a Fortran 90 grammar that was close to having a form acceptable to bison and a few hours editing and debugging got me a grammar containing 6 shift/reduce conflicts and 1 reduce/reduce conflict. These conflicts looked like they could all be handled using glr parsing. The grammar contained 922 productions, somewhat large but I was only interested in actively making use of parts of it.

For my lexer I planned to cut and paste an existing C/C++/Java lexer I have used for many projects. Now this sounds like a fundamental mistake, these languages treat whitespace as being significant while Fortran does not. This important difference is illustrated by the well known situation where a Fortran lexer needs to lookahead in the character stream to decide whether the next token is the keyword do or the identifier do5i (if 1 is followed by a comma it must be a keyword):

      do 5 i = 1 , 10
      do 5 i = 1 . 10        ! assign 1.10 to do5i
5     continue

In my experience developers don’t break up literals or identifier names with whitespace and so I planned to mostly ignore the whitespace issue (it would simplify things if some adjacent keywords were merged to create a single keyword).

In Fortran the I/O is specified in the language syntax while in C like languages it is a runtime library call involving a string whose contents are interpreted at runtime. I decided to to ignore I/O statements by skipping to the end of line (Fortran is line oriented).

Then the number of keywords hit me, around 190. Even with the simplifications I had made writing a Fortran lexer looked like it would be a lot of work; some of the keywords only had this status when followed by a = and I kept uncovering new issues. Cutting and pasting somebody else’s lexer would probably also involve a lot of work.

I went back and looked at some of the Fortran front ends I had found on the Internet. The GNU Fortran front-end was a huge beast and would need serious cutting back to be of use. moware was written in Fortran and used the traditional six character abbreviated names seen in ‘old-style’ Fortran source and not a lot of commenting. The Eli project seemed a lot more interested in the formalism side of things and Fortran was just one of the languages they claimed to support.

The Open Fortran Parser looked very interesting. It was designed to be used as a parsing skeleton that could be used to produce tools that processed source and already contained hooks that output diagnostic output when each language production was reduced during a parse. Tests showed that it did a good job of parsing the source I had, although there was one vendor extension used quiet often (an not documented in their manual). The tool source, in Java, looked straightforward to follow and it was obvious where my code needed to be added. This tool was exactly what I needed :-)

Dimensional analysis of source code

May 28th, 2009 Derek-Jones 1 comment

The idea of restricting the operations that can be performed on a variable based on attributes appearing in its declaration is actually hundreds of years old and is more widely known as dimensional analysis. Readers are probably familiar with the concept of type checking where, for instance, a value having a floating-point type is not allowed to be added to a value having a pointer type. Unfortunately many of those computer languages that support the functionality I am talking about (e.g., Ada) also refer to it as type checking and differentiate it from the more common usage by calling it strong typing. The concept would be much easier for people to understand if a different term were used, e.g., unit checking or even dimension checking.

Dimensional analysis, as used in engineering and the physical sciences, relies on the fact that quantities are often expressed in terms of a small number of basic attributes, e.g., mass, length and time; velocity is calculated by dividing a length by a time, LT^{-1} and area is calculated by multiplying two lengths, L^{2}. Adding a length quantity to a velocity has no physical meaning and suggests that something is wrong with the calculation, while dividing velocity by time, LT^{-2}, can be interpreted as acceleration. Dividing two quantities that have the same units results in what is known as a dimensionless number.

Dimensional analysis can be used to check a calculation involving physical quantities for internal consistency and as a method for trying to deduce the combinations of quantities that an unknown equation might contain based on the physical units the result is known to be represented in.

The frink language has units of measure checking built into it.

How might dimensional analysis be used to check source code for internal consistency? Consider the following code:

x = a / b;
c = a;
y = c / b;
if (x + y ...
...
z = x + b;

c is assigned a’s value and is therefore assumed to have the same units of measurement. The value assigned to y is calculated by dividing c by b and the train of reasoning leading to the assumption that it has the same units of measurement as x is easy to follow. Based on this analysis there is nothing suspicious about adding x and y, but adding x and b looks wrong (it would be perfectly ok if all of the variables in this code were dimensionless).

A number of tools have been written to check source code expressions for internal consistency e.g., Fortran (Automated computation and consistency checking of physical dimensions and units in scientific programs), C++ (Applied Template Metaprogramming in SI units) and C (Annotation-less Unit Type Inference for C), but so far only one PhD.

Providing a mechanism for developers to add unit information to variable declarations would enable compilers to perform consistency checks and reduce the likelihood of false positives being reported (because dimensionless values can generally be combined together in any way). It is too late in the day for such a major feature to be added to the next revision of the C++ standard; the C standard is also being revised but the committee is currently being very conservative and insists that any proposed new constructs already be implemented in at least one compiler.

www.wenn.com
FireStats icon Powered by FireStatswww.tinynibbles.com buy cheapest propecia

buy cheap generic levitra

buy prescription propecia without

levitra mail order

best price cialis

buy viagra online

discount levitra online

cialis transdermal

drug generic propecia

cialis overnight delivery

buy levitra online from canada

online propecia prescription

how much is viagra

ganeric cialis

best way to use cialis

brand cialis for sale

discount cialis india

cialis buy overnight

cialis by mail

cheap viagra online

lowest cost levitra

cost levitra low

cialis vs levitra

daily dosage cialis

order propecia

levitra online prescription

cheapest propecia sale uk

cialis on women

generic levitra vardenafil

generic propecia effective

cheap propecia online prescription

generic levitra purchase

levitra prescription

canada online pharmacy levitra

brand viagra over the net

buying cialis soft tabs 100 mg

levitra online sales

cialis from mexico

buy propecia canada

generic levitra overnight delivery

online generic cialis 100 mg

cialis online

how to get viagra

lowest priced propecia

cheapest viagra online

canada propecia prescription

generic viagra canadian

cialis next day

cialis discount

lowest price on non generic levitra

levitra mg

generic propecia for sale

next day viagra

levitra low price

buy viagra china

cheap levitra

brand name cialis

canada viagra

levitra discount

info levitra

canadian online pharmacy cialis

buy propecia online from usa pharmacy

cheapest overnight cialis

buy canada levitra

indian viagra

generic cialis soft tabs

mail order levitra

online ordering propecia

order cheapest propecia online

cialis next day delivery

next day delivery cialis

china viagra

online cheap viagra

canadian healthcare

generic levitra canada

cheap fast levitra

ordering propecia online

levitra 10mg

cialis alternative

cialis woman

canadian propecia rx

cheap propecia uk

buying propecia

levitra pill

5 mg daily cialis

canadian healthcare pharmacy

cialis soft pills

cheap propecia online

cialis price in canada

levitra cost

canada online pharmacy propecia

generic cialis sale

buying levitra online

cialis overnight

cialis fast delivery usa

best price generic propecia

buy 5 mg cialis

cheap levitra without prescription

can i get viagra in mexico

canadian drugs propecia

buy dosages levitra

generic viagra canada

buy propecia where

canada meds viagra

get levitra online

how much does cialis cost

cialis and diarrhea

canada levitra

cialis delivered overnight

cheap viagra from uk

online viagra gel to buy

buy cialis online uk

buy cheap levitra

levitra sales uk

cheap levitra tablets

lowest price levitra

buy cialis 5 mg

canadian healthcare viagra

cheapest propecia prescription

name brand cialis

cialis 5 mg buy

buy propecia on line

levitra buy online

buy generic viagra india rx

canadian viagra 50mg

buying generic cialis mexico rx

overnight delivery viagra

how much cialis

generic viagra india

buy cheap levitra online

bestellen levitra online

levitra cheap fast

how to buy cialis in canada

buy cialis in usa

cheap levitra prescription

levitra where to buy

buying cialis

buy generic levitra

buy cheap generic propecia

indian cialis

buy cialis usa

mexico pharmacy cialis

canada viagra pharmacies scam

buy levitra overnight

discount levitra purchase

levitra now online

buy propecia now

levitra for sale

generic viagra made in india

buy can from i propecia who

cialis to buy

buy real viagra online

buy cialis cannada

generic propecia sale

buy now propecia

discount drug propecia

cialis strenght mg

buy cialis canada

order prescription propecia

get levitra

cialis quick shipment

low cost canadian viagra

cialis professional 20 mg

canadian pharmacy discount code viagra

discount levitra rx

generic propecia alternative

online cialis

buy cialis once daily

cialis one a day

get cialis

cialis cheap

lowest price propecia best

levitra online overnight delivery

indian generic levitra

female viagra pills

get propecia online pharmacy

overnight delivery cialis

cialis from canada

buy real cialis

order cheap levitra

natural viagra

lowest price propecia

cheap cialis from india

cialis daily in canada

mail order propecia

generic propecia finasteride

buy online prescription propecia

cheap levitra uk

cialis en mexico

generic propecia 5mg

once daily cialis

cheapest viagra

buy cialis online canada

genuine cialis pills

buy cialis without prescription

online propecia prescriptions

buy discount viagra

levitra online us

cialis dosage mg

canadian pharmacy viagra

cialis 5 mg italia

does generic cialis work

buy branded viagra

50 mg cialis

cialis profesional

cialis price 100 mg

buy generic propecia

i need to buy propecia

generic cialis next day shipping

getting cialis from canada

cialis fast delivery

order viagra or levitra

canada cheap propecia

buying propecia online

cialis uk

how much to buy viagra in pounds

cialis 100 mg

buy generic cialis

cialis prescription

levitra in canada

mail online order propecia

cialis 50 mg

cialis 5 mg

discount us propecia

best price for propecia

buy propecia without prescription

cheapest prices for viagra

fda levitra

low cost levitra

generic propecia fda approved

order levitra online

buy fast propecia

cialis 20 mg

buy viagra online cheap us

5 mg original brand cialis

generic cialis from india

buy viagra germany canadian meds

cost of viagra

lowest propecia 1 mg

online levitra

indian cialis generic

ordering cialis gel

cialis tablets foreign

buying cialis next day delivery

obtain viagra without prescription

generic viagra 100 mg

buy viagra on line

cheapest price propecia cheap

levitra online no prescription

get cialis online

hydrochlorothiazide cialis

buy propecia generic

brand name cialis overnight

best price levitra

levitra order prescription

cialis tablets

cialis refractory

buy propecia online

levitra next day delivery

internet pharmacy propecia

generic viagra made in usa

brand viagra professional

canadian pharmacy

mexico levitra

order generic levitra

discount propecia propecia

cost of propecia

cialis headaches

for sale levitra

canada viagra generic

cialis cheap us pharmacy

cialis for woman

cialis and ketoconazole

once a day viagra

buy propecia online pharmacy

how to get cialis in canada

buy levitra vardenafil

bio viagra herbal

cialis daily

lowest price for propecia

cialis professional 100 mg

levitra viagra cialis

cheap canadian viagra

cialis price

cheap cialis soft

generic propecia online pharmacy

buy levitra us

cialis in mexico

how strong is 5 mg of cialis

buy viagra without prescription

canadian viagra india

cheapest viagra usa

buy viagra

best price for generic cialis

buy viagra mexico

canadian viagra

discount generic propecia

canada generic propecia

discount propecia rx

canadian viagra and healthcare