Archive

Archive for the ‘Datatypes’ Category

Parsing without a symbol table

December 19th, 2008 Derek-Jones 1 comment

When processing C/C++ source for the first time through a compiler or static analysis tool there are invariably errors caused by missing header files (often because the search path has not been set) or incorrectly defined, or not defined, macro names. One solution to this configuration problem is to be able to process source without handling preprocessing directives (e.g., skipping them, such as not reading the contents of header files or working out which arm of a conditional directive is applicable). Developers can do it, why not machines?

A few years ago GLR support was added to Bison, enabling it to process ambiguous grammars, and I decided to create a C parser that simply skipped all preprocessing directives. I knew that at least one reasonably common usage would generate a syntax error:

func_call(a,
#if SOME_FLAG
b_1);
#else
b_2);
#endif

c);
and wanted to minimize its consequences (i.e., cascading syntax errors to the end of the file). The solution chosen was to parse the source a single statement or declaration at a time, so any syntax error would be localized to a single statement or declaration.

Systems for parsing ambiguous grammars work on the basis that while the input may be locally ambiguous, once enough tokens have been seen the number of possible parses will be reduced to one. In C (and even more so in C++) there are some situations where it is impossible to resolve which of several possible parses apply without declaration information on one or more of the identifiers involved (a traditional parser would maintain a symbol table where this information could be obtained when needed). For instance, x * y; could be a declaration of the identifier y to have type x or an expression statement that multiplies x and y. My parser did not have a symbol table and even if it did the lack of header file processing meant that its contents would only contain a partial set of the declared identifiers. The ambiguity resolution strategy I adopted was to pick the most likely case, which in the example is the declaration parse.

Other constructs where the common case (chosen by me and I have yet to get around to actually verifying via measurement) was used to resolve an ambiguity deadlock included:

f(p);      // Very common, 
            // confidently picked function call as the common case
(m)*p;   // Not rare,
            // confidently picked multiplication as the common case
(s) - t;      // Quiet rare,
               // picked binary operator as the common case
(r) + (s) - t; // Very rare,
                  //an iteration on the case above

At the moment I am using the parser to measure language usage, so less than 100% correctness can be tolerated. Some of the constructs that cause a syntax error to be generated every few hundred statement/declarations include:

offsetof(struct tag, field_name)  // Declarators cannot be 
                                            //function arguments
int f(p, q)
int p;     // Tries to reduce this as a declaration without handling
char q;   // it as part of an old style function definition
{
 
MACRO(+); // Preprocessing expands to something meaningful

Some of these can be handled by extensions to the grammar, while others could be handled by an error recovery mechanism that recognized likely macro usage and inserted something appropriate (e.g., a dummy expression in the MACRO(x) case).

Average distance between two fields

December 2nd, 2008 Derek-Jones No comments

If I randomly pick two fields from an aggregate type definition containing N fields what will be the average distance between them (adjacent fields have distance 1, if separated by one field they have distance 2, separated by two fields they have distance 3 and so on)?

For example, a struct containing five fields has four field pairs having distance 1 from each other, three distance 2, two distance 2, and one field pair having distance 4; the average is 2.

The surprising answer, to me at least, is (N+1)/3.

Proof: The average distance can be obtained by summing the distances between all possible field pairs and dividing this value by the number of possible different pairs.

                  Distance 1  2  3  4  5  6
Number of fields
            4              3  2  1
            5              4  3  2  1
            6              5  4  3  2  1
            7              6  5  4  3  2  1

The above table shows the pattern that occurs as the number of fields in a definition increases.

In the case of a definition containing five fields the sum of the distances of all field pairs is: (4*1 + 3*2 + 2*3 + 1*4) and the number of different pairs is: (4+3+2+1). Dividing these two values gives the average distance between two randomly chosen fields, e.g., 2.

Summing the distance over every field pair for a definition containing 3, 4, 5, 6, 7, 8, … fields gives the sequence: 1, 4, 10, 20, 35, 56, … This is sequence A000292 in the On-Line Encyclopedia of Integer sequences and is given by the formula n*(n+1)*(n+2)/6 (where n = N − 1, i.e., the number of fields minus 1).

Summing the number of different field pairs for definitions containing increasing numbers of fields gives the sequence: 1, 3, 6, 10, 15, 21, 28, … This is sequence A000217 and is given by the formula n*(n + 1)/2.

Dividing these two formula and simplifying yields (N + 1)/3.

www.wenn.com
FireStats icon Powered by FireStatswww.tinynibbles.com cialis fast delivery

cialis price 100 mg

internet pharmacy propecia

canada viagra

drug generic propecia

canadian healthcare viagra

levitra discount

levitra sales uk

bio viagra herbal

cheapest price propecia cheap

cialis next day delivery

buy levitra online from canada

buy viagra online

levitra in india

cheap propecia no prescription

online propecia uk

order levitra online

online propecia prescriptions

can i get viagra in mexico

online cialis

canadian pharmacy viagra

low cost propecia

overnight delivery viagra

cialis discount

canada levitra

cialis professional 100 mg

50 mg cialis

buy propecia without prescription

how much is viagra

generic propecia fda approved

levitra online no prescription

buy cialis cannada

indian cialis

buy cialis in usa

generic propecia for sale

cialis quick shipment

buy viagra on line

generic cialis from india

getting cialis from canada

female viagra pills

discount levitra rx

cheapest propecia uk

indian cialis generic

how much cialis

how much to buy viagra in pounds

cialis one a day

buy cheap generic levitra

cheap propecia uk

ganeric cialis

buy levitra online no prescription

buy prescription propecia without

generic viagra india

buy 5 mg cialis

overnight delivery cialis

levitra online overnight delivery

cialis next day

mexico levitra

buy propecia canada

cheap fast levitra

generic propecia 5mg

get propecia online pharmacy

cialis cheap

levitra in canada

levitra online us

5 mg daily cialis

cheap viagra from uk

levitra tabs

canadian healthcare pharmacy

cheap propecia online prescription

generic propecia online pharmacy

cialis refractory

cheap cialis

levitra now online

cialis professional no prescription

cost levitra low

mail order propecia

cheapest overnight cialis

buy generic cialis

cialis price in canada

online viagra gel to buy

order cheap propecia

canada viagra generic

online levitra

cheap levitra

cialis professional 20 mg

how to get viagra

generic propecia sale

cost of viagra

canada online pharmacy propecia

cialis price

name brand cialis

buying levitra online

mexico pharmacy cialis

does generic cialis work

canada cheap propecia

buy cialis online uk

cialis and diarrhea

next day delivery cialis

generic viagra canada

buy viagra without prescription

order cheapest propecia online

buy online prescription propecia

levitra for sale

generic cialis soft tabs

generic levitra canada

buy dosages levitra

levitra online prescription

buy propecia now

cialis 5 mg buy

low cost canadian viagra

cialis vs levitra

buying propecia

buy viagra

buy cialis 5 mg

generic levitra cheap

cialis generic 100 mg

lowest propecia 1 mg

bestellen levitra online

cialis headaches

canadian pharmacy

buy generic viagra india rx

cheapest viagra online

discount levitra online

cheap viagra canada or india

cialis transdermal

get levitra online

brand viagra professional

canadian viagra india

canada viagra pharmacies scam

buy propecia online from usa pharmacy

cialis soft pills

fda levitra

buy canada levitra

cheap discount levitra

cialis daily dosage pharmacy

cialis and ketoconazole

order cheap levitra

buy propecia where

cialis overnight delivery

online pharmacy propecia viagra

levitra cheap fast

cialis by mail

cialis 20 mg

cialis strenght mg

daily dosage cialis

canada propecia prescription

discount generic propecia

cialis 100 mg generic

best price cialis

discount cialis india

info levitra

buy real viagra online

get cialis

buy propecia online pharmacy

once daily cialis

lowest priced propecia

buy cheap levitra online

cheapest viagra usa

levitra order prescription

natural viagra

buy generic levitra

cheapest prices for viagra

generic levitra vardenafil

cialis tablets foreign

mail online order propecia

canada online pharmacy levitra

cialis to buy

cialis delivered overnight

generic viagra 100 mg

ordering propecia online

cheapest propecia sale uk

levitra online

cheap viagra online

order propecia

discount us propecia

cialis woman

buy cialis fedex shipping

buy real cialis

cheap levitra without prescription

best viagra

buy viagra mexico

buying propecia online

cialis uk

buy levitra uk

5 mg original brand cialis

online propecia prescription

canadian drugs propecia

generic viagra canadian

best price levitra

cheap cialis soft

buy cialis once daily

gele viagra

healthcare canadian pharmacy

cialis cheap us pharmacy

lowest cost levitra

cialis dosage mg

cialis on women

indian generic levitra

combine cialis and levitra

hydrochlorothiazide cialis

generic cialis next day shipping

cialis online

lowest price propecia

generic propecia finasteride

buy viagra online cheap us

how much does cialis cost

canada generic propecia

cialis 50 mg

generic viagra made in usa

cialis 100 mg

buying cialis next day delivery

lowest price for propecia

discount propecia rx

canadian healthcare

generic propecia effective

buy propecia generic

buy generic propecia

buy levitra us

levitra prescription

buying generic propecia

get levitra

cheap levitra prescription

online pharmacy propecia renova

canadian pharmacy discount code viagra

cheap cialis from india

levitra buy online

next day viagra

brand name cialis overnight

lowest propecia prices

buy cheap levitra

china viagra

canadian pharmacies cialis

buy propecia online

levitra mg

buy cheap generic propecia

canadian viagra and healthcare

cialis in mexico

cost of propecia

buying viagra in canada

cialis daily

cheap prescription propecia

online cheap viagra

cost of daily cialis

best way to use cialis

discount propecia online

cheapest viagra

online generic cialis 100 mg

once a day viagra

buy levitra vardenafil

generic levitra overnight delivery

buy cialis without prescription

buy branded viagra

low price levitra

buy discount viagra

buy propecia prescriptions online

cheap propecia 5mg

buying online propecia

mail order levitra

levitra viagra online

levitra 10mg

lowest price propecia best

buy cialis usa

how to get cialis in canada

indian viagra

cialis pharmacy

levitra where to buy

buy propecia in the uk

buy cheapest propecia

buy fast propecia

buy levitra overnight

levitra low price

generic viagra made in india

how strong is 5 mg of cialis

buy levitra online viagra

cheap canadian viagra

brand name cialis

cialis for woman

levitra pill

levitra cost

ordering cialis gel

buy viagra germany canadian meds

herbal propecia

generic cialis sale

levitra canadian

low cost levitra

buy now propecia

buy propecia on line

levitra online sales

lowest propecia prices in canada

cheap levitra uk

discount levitra purchase

order generic levitra

levitra mail order

levitra viagra cialis

buy cialis canada

order prescription propecia

genuine cialis pills

best price generic propecia

canadian propecia rx

cialis and canada custom