Archive

Archive for the ‘Datatypes’ Category

Parsing without a symbol table

December 19th, 2008 Derek-Jones No comments

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 5 mg italia

levitra now online

buy cheap generic propecia

buy viagra mexico

buy cialis online canada

how strong is 5 mg of cialis

buy levitra overnight

cost levitra low

cialis one a day

cialis on women

cheapest propecia sale uk

info levitra

cialis discount

levitra tabs

generic viagra canada

how much cialis

cialis 50 mg

canadian viagra and healthcare

canadian healthcare

buy propecia in the uk

best price for propecia

herbal propecia

cialis 100 mg generic

cheap viagra online

online propecia uk

indian generic levitra

cheap viagra canada or india

cialis cheap

buy propecia prescriptions online

5 mg original brand cialis

order prescription propecia

cheap prescription propecia

cheapest propecia uk

bestellen levitra online

lowest price for propecia

buy propecia where

low price levitra

lowest propecia prices

cheap canadian viagra

gele viagra

obtain viagra without prescription

levitra online no prescription

cheap propecia online

cialis cheap us pharmacy

levitra discount

online ordering propecia

generic levitra canada

canadian healthcare pharmacy

buy real viagra online

how to buy cialis in canada

discount levitra purchase

cialis refractory

cheapest viagra

levitra cost

name brand cialis

cialis 100 mg

cialis discounts

generic cialis sale

mail online order propecia

canada cheap propecia

generic propecia 5mg

generic viagra online

levitra where to buy

buying online propecia

combine cialis and levitra

generic viagra made in usa

order cheapest propecia online

brand name cialis

cialis woman

buy levitra us

cheap fast levitra

canadian pharmacies cialis

cost of viagra

buy cialis 5 mg

generic propecia for sale

cialis transdermal

cheapest prices for viagra

levitra 10mg

buy propecia on line

buy cialis without prescription

brand name cialis overnight

hydrochlorothiazide cialis

lowest propecia prices in canada

buy levitra vardenafil

online pharmacy propecia renova

china viagra

buy discount viagra

brand viagra over the net

50 mg cialis

bio viagra herbal

buy viagra online

levitra online prescription

canadian healthcare viagra

discount generic propecia

generic propecia online pharmacy

generic propecia effective

cialis no prescription

buy generic cialis

levitra for sale

generic viagra canadian

buy cialis canada

best price levitra

buy cialis cannada

canada online pharmacy levitra

discount levitra rx

canada generic propecia

online cheap viagra

for sale levitra

buy branded viagra

buying propecia

indian viagra

best price generic propecia

indian cialis generic

online levitra

buy dosages levitra

how much to buy viagra in pounds

cialis daily in canada

generic propecia fda approved

buy cialis fedex shipping

buy levitra online from canada

buy viagra germany canadian meds

buy cheapest propecia

discount propecia online

buying generic cialis mexico rx

cialis 5 mg buy

get propecia online pharmacy

levitra online

cheap viagra from uk

lowest price levitra

cialis daily dosage pharmacy

cheap levitra

levitra order prescription

generic levitra purchase

cialis by mail

cialis and ketoconazole

internet pharmacy propecia

discount propecia propecia

once a day viagra

buying viagra in canada

canadian pharmacy discount code viagra

ordering propecia online

levitra online us

online pharmacy propecia viagra

lowest cost levitra

cialis to buy

buying cialis next day delivery

cialis overnight delivery

lowest price propecia best

buy prescription propecia without

canadian drugs propecia

buy propecia online prescription

buy cheap levitra

levitra buy online

buy propecia generic

buy cheap generic levitra

cialis and diarrhea

buy propecia cheap

cialis soft pills

cialis professional 100 mg

online propecia prescription

cheapest price propecia cheap

generic propecia finasteride

best price for generic cialis

generic viagra made in india

get levitra

levitra cheap fast

canadian viagra india

cialis price 100 mg

how to get viagra

cialis purchase

brand cialis for sale

online viagra gel to buy

getting cialis from canada

cialis tablets foreign

buy canada levitra

cialis buy overnight

overnight delivery cialis

cialis vs levitra

drug generic propecia

cialis overnight

cheap propecia no prescription

buy fast propecia

buy propecia online from usa pharmacy

lowest price on non generic levitra

buy generic propecia

canada viagra generic

cheap propecia 5mg

levitra viagra cialis

cheap levitra without prescription

buying generic propecia

levitra pill

cialis fast delivery

cialis profesional

how much is viagra

cialis professional 20 mg

cheap discount levitra

buy propecia without prescription

buying levitra online

buying propecia online

buy now propecia

cialis uk

cialis from mexico

cialis price

generic levitra overnight delivery

cheapest viagra usa

buy online prescription propecia

cialis tablets

levitra mail order

lowest propecia 1 mg

buy real cialis

buy propecia now

order viagra or levitra

buy 5 mg cialis

buy propecia canada

how to get cialis in canada

buy can from i propecia who

cheap levitra prescription

buy cialis for daily use

generic viagra 100 mg

canada online pharmacy propecia

buy viagra without prescription

canadian pharmacy

canada propecia prescription

generic cialis soft tabs

cialis price in canada

cialis delivered overnight

canada viagra pharmacies scam

cialis for woman

buy levitra online no prescription

mexico levitra

cost of daily cialis

how much does cialis cost

canadian viagra

indian cialis

next day delivery cialis

buying cialis soft tabs 100 mg

levitra online sales

mail order levitra

lowest priced propecia

cialis 20 mg

cialis daily

generic levitra cheap

discount levitra online

canada viagra

cheap levitra uk

cialis online

buy levitra online viagra

cialis next day

best way to use cialis

cialis headaches

canada meds viagra

low cost propecia

generic cialis from india

cialis generic 100 mg

levitra sales uk

levitra viagra online

generic cialis next day shipping

cheap propecia uk

cialis next day delivery

online propecia prescriptions

low cost levitra

best price propecia

cialis fast delivery usa

buy generic levitra

cialis dosage mg

levitra in canada

online cialis

overnight delivery viagra

cialis from canada

fda levitra

buy levitra uk

generic propecia sale

can i get viagra in mexico

cialis professional no prescription

cheap order prescription propecia

brand viagra professional

levitra mg

cialis strenght mg

generic viagra india

5 mg daily cialis

levitra next day delivery

genuine cialis pills

buy viagra on line

i need to buy propecia

cheap cialis from india

buy viagra

order propecia

levitra prescription

buy propecia online

levitra canadian

daily dosage cialis

cialis alternative

get cialis online

cheapest viagra online

get cialis

levitra from canadian pharmacy

cialis pharmacy

cheap cialis soft

order cheap propecia

order generic levitra