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 No comments

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 daily dosage cialis

levitra where to buy

discount cialis india

canada online pharmacy propecia

buy cialis online canada

cialis soft pills

low cost canadian viagra

cialis 5 mg italia

cialis daily dosage pharmacy

online levitra

canadian pharmacy cialis

cheap viagra canada or india

online propecia prescriptions

buy generic viagra india rx

mail online order propecia

canadian viagra and healthcare

buy cialis for daily use

generic viagra made in usa

cialis fast delivery

how strong is 5 mg of cialis

discount propecia online

get levitra online

cialis generic 100 mg

buy cialis usa

cheapest viagra

levitra mg

cheapest overnight cialis

generic propecia finasteride

cheap prescription propecia

cialis and ketoconazole

cialis no prescription

cialis professional 100 mg

levitra 10mg

generic levitra vardenafil

buy levitra online viagra

generic propecia alternative

generic viagra india

online viagra gel to buy

cost levitra low

cheap levitra prescription

buying cialis

generic levitra online

buy viagra china

how much is viagra

cialis from canada

cialis prescription

cialis fast delivery usa

generic viagra made in india

cialis headaches

generic levitra canada

generic propecia online pharmacy

generic propecia sale

canada levitra

female viagra pills

buy cialis 5 mg

best price for propecia

generic levitra overnight delivery

cialis vs levitra

obtain viagra without prescription

how to get cialis in canada

levitra online prescription

cialis quick shipment

cialis for woman

buy propecia now

levitra canadian

canadian healthcare pharmacy

buy propecia online from usa pharmacy

buy propecia cheap

natural viagra

i need to buy propecia

buy propecia online

low price levitra

lowest price levitra

cialis 100 mg generic

cheapest propecia sale uk

canadian pharmacy discount code viagra

50 mg cialis

discount drug propecia

cialis overnight delivery

canadian online pharmacy cialis

get cialis online

how much does cialis cost

internet pharmacy propecia

getting cialis from canada

generic propecia 5mg

buying levitra online

generic viagra canada

buy real cialis

canada meds viagra

order cheap levitra

levitra from canadian pharmacy

buy real viagra online

levitra low price

cialis delivered overnight

levitra viagra cialis

cialis tablets

cialis woman

cheap propecia uk

best price generic propecia

levitra buy online

buy dosages levitra

levitra tabs

low cost propecia

buy propecia online pharmacy

cheap propecia online

cialis online

levitra mail order

once a day viagra

buy cialis online uk

canadian drugs propecia

buy 5 mg cialis

best way to use cialis

cialis uk

mail order propecia

cialis tablets foreign

online pharmacy propecia renova

ganeric cialis

cheapest prices for viagra

discount propecia rx

5 mg original brand cialis

buy cialis once daily

cheapest viagra online

best viagra

buy cialis without prescription

levitra prescription

canadian viagra 50mg

order cheapest propecia online

cialis en mexico

online cialis

levitra order prescription

discount levitra purchase

cheap levitra without prescription

can i get viagra in mexico

buy online prescription propecia

buy cheap generic propecia

cheap fast levitra

order levitra online

canadian propecia rx

cialis from mexico

cialis daily in canada

cialis discount

canadian pharmacies cialis

cialis refractory

levitra next day delivery

indian cialis

cialis and canada custom

brand name cialis

brand name cialis overnight

discount generic propecia

lowest propecia 1 mg

5 mg daily cialis

levitra sales uk

mexico pharmacy cialis

online propecia prescription

cheap levitra

generic viagra 100 mg

overnight delivery cialis

cialis next day delivery

buy cheap levitra

does generic cialis work

ordering cialis gel

lowest propecia prices

for sale levitra

canadian viagra

buy cheap generic levitra

buy canada levitra

buy generic propecia

cialis on women

cialis pharmacy

buy prescription propecia without

levitra online

buying generic cialis mexico rx

canada generic propecia

canada viagra

cheap viagra online

cialis by mail

mail order levitra

buy propecia without prescription

cost of propecia

generic viagra canadian

cheap cialis

cheapest price propecia cheap

levitra online us

cheap cialis from india

levitra now online

generic cialis next day shipping

cialis 20 mg

online propecia uk

online cheap viagra

lowest propecia prices in canada

lowest cost levitra

once daily cialis

buying online propecia

lowest price propecia best

buy generic cialis

buying cialis next day delivery

best price cialis

cheap propecia online prescription

buying cialis soft tabs 100 mg

generic viagra online

order prescription propecia

levitra online no prescription

next day viagra

buy generic levitra

combine cialis and levitra

discount levitra rx

cheap levitra uk

canadian pharmacy viagra

cialis professional no prescription

get cialis

canada viagra pharmacies scam

next day delivery cialis

herbal propecia

info levitra

indian generic levitra

buy levitra uk

canadian viagra india

gele viagra

cialis 5 mg

buy viagra germany canadian meds

lowest priced propecia

china viagra

generic cialis from india

cheap canadian viagra

buy cialis fedex shipping

cialis buy overnight

buy propecia where

cialis in mexico

cost of daily cialis

cheap propecia no prescription

name brand cialis

buy propecia generic

canada viagra generic

buy propecia on line

cheap discount levitra

buying propecia online

generic levitra cheap

brand viagra over the net

cialis profesional

lowest price propecia

lowest price on non generic levitra

canadian healthcare viagra

levitra viagra online

cialis discounts

cialis daily

cheapest viagra usa

buying propecia

how much cialis

cialis 50 mg

buy levitra overnight

cialis price 100 mg

buy can from i propecia who

cheap viagra from uk

discount us propecia

buy viagra on line

bio viagra herbal

cialis to buy

cialis cheap

buy fast propecia

cheap levitra tablets

cialis purchase

online generic cialis 100 mg

levitra pill

drug generic propecia

hydrochlorothiazide cialis

levitra online overnight delivery

buy cialis cannada

cialis 100 mg

cialis strenght mg

buy propecia in the uk

buy cheap levitra online

levitra online sales

levitra for sale

discount levitra online

cialis and diarrhea

levitra in canada

cialis 5 mg buy

buy levitra us

lowest price for propecia

online pharmacy propecia viagra

generic cialis soft tabs

buying generic propecia

buy levitra online from canada

cialis one a day

generic propecia fda approved

order generic levitra

canadian pharmacy

best price propecia

canada online pharmacy levitra

order viagra or levitra

best price levitra

buy viagra without prescription

levitra cheap fast

how to get viagra

buy viagra online

generic propecia effective

online ordering propecia

buy propecia prescriptions online

buy viagra online cheap us

canadian healthcare