Home > Datatypes, Uncategorized, empirical > Parsing without a symbol table

Parsing without a symbol table

December 19th, 2008 Derek-Jones

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).

Comments are closed.
FireStats icon Powered by FireStatsdiscount viagra

discount canadian cialis

buy cialis once daily

buying generic cialis mexico rx

buy propecia uk

cheap levitra tablets

buy canada levitra

buy daily cialis

canadian drug viagra soft

canadian pharmacy cialis

hydrochlorothiazide cialis

buying levitra online

buy cialis in usa

lowest price propecia best

cheap viagra generic

mail online order propecia

female viagra

canada viagra pharmacies scam

cialis canadian

cialis testimonial

buy mg propecia

low price propecia

buy propecia where

buy propecia online prescription

cheapest prices on propecia

cheap viagra on line

brand viagra professional

generic levitra india

buy cialis professional

cialis blood thinner

buy vardenafil levitra

buy cialis online without prescription

get viagra

buy propecia generic

cialis headaches

cialis 30 mg

cialis vs levitra

buy levitra uk

buying viagra with no prescription

buy online prescription propecia

daily cialis cost

online ordering propecia

canadian pharmacy cialis pfizer

buying real viagra without prescription

online viagra gel to buy

best levitra prices

buy levitra online

generic cialis next day delivery

levitra cheapest

buy propecia canada

cialis daily

i need to buy propecia

buy now online propecia

generic propecia online pharmacy

buy cheap propecia online

cialis once daily

cialis 5 mg buy

discount real viagra

cialis okay for women

online pharmacy cost levitra

cialis in canada

canada pharmacy propecia

buy cialis online uk

cialis philippines

cialis discount

cialis fast delivery

how much cialis

buy cialis cannada

cheap prescription propecia

low cost canadian viagra

cialis professional 100 mg

canadian pharmacy

cialis soft canada

best online levitra

buy propecia in canada

buying online propecia

levitra vs viagra

cialis from india

canada online pharmacy levitra

5 mg cialis canada

cheapest priced propecia

buy viagra

cialis alternative

cheapest viagra online

buy now propecia

generic cialis next day delivery

levitra sex pill

cialis en mexico

canadian healthcare cialis

buy cialis online canada

indian viagra

overnight delivery viagra

buying viagra without prescription

generic propecia uk

buy cheap online propecia

cheap levitra online us

buy cialis without a prescription

canadian pharmacies cialis

cheap online levitra

buy propecia prescriptions online

generic viagra from china

cialis and ketoconazole

generic cialis soft tabs

buy propecia online without prescription

buy viagra in canada no prescription

cialis for sale

cialis dosage mg

cialis refractory

levitra 10 mg

cialis okay for women

buy prescription propecia without

best price cialis

buy discount viagra

levitra now online

brand viagra professional

buy generic propecia online

buy propecia online usa

how can i get some cialis

cheap viagra on line

levitra tadalafil

buy pfizer viagra in canada

get levitra

cheap online propecia

best price generic propecia

buy viagra

cialis arterial fibrillation

mexico levitra

buy real cialis

best cialis price

original brand cialis

discount propecia rx

best deal for propecia

cheap order prescription propecia

cialis canada online pharmacy

cialis cost canada

generic cialis soft tabs

cialis from india

buy cialis once daily

no prescription viagra

cialis brand

buy cialis usa

cheap viagra generic

generic cialis canadian

cheapest propecia prescription

buy fast propecia

canadian healthcare viagra sales

cheap canadian viagra

discount levitra online viagra

levitra in mexico

cialis 20 mg

generic cialis canadian

cialis brand

cheap cialis fast delivery time

cialis angioplasty

canada viagra

discount propecia propecia

cialis en mexico

canada cheap propecia

cialis usa

canada propecia prescription

how to buy cialis in canada

cialis on sale

canada viagra pharmacies scam

buy levitra online from canada

5 mg original brand cialis

buy levitra vardenafil

cost of viagra in germany

beta blockers and viagra

levitra low price

cialis india pharmacy

buy cheapest propecia

cheap propecia online prescription

generic propecia india

best cialis price

buy generic propecia online

buy generic propecia

buying cialis without a prescription

cialis canada

cialis 50 mg

cialis delivered overnight

cialis and diarrhea

cialis dosage mg

levitra ed

buy propecia in the uk

how to get cialis in canada

mexico viagra

cialis samples

canadian pharmacy cialis

canadian healthcare

buy cialis canada

best levitra price

cheap viagra online

cialis no prescription

lowest price levitra

canadian viagra and healthcare

buy levitra overnight

generic levitra vardenafil

cialis professional

discount online propecia

generic cialis sale

cialis vs levitra

50 mg cialis dose

cialis 30 mg

brand cialis

discount generic propecia

cheap cialis from india

cheapest prices for viagra

cialis on line pricing in canada

online prescription propecia

cheap propecia uk

buy viagra without prescription

buy canada in propecia

cialis at real low prices

cheap viagra from uk

best price levitra online

canadian viagra 50mg

cialis alternitives

one day cialis

order cheap propecia

cialis 5 mg italia

combine cialis and levitra

china viagra

5 mg original brand cialis

buying viagra without prescription

brand viagra

overnight canadian viagra

cheapest cialis

order prescription propecia

cost of viagra

cialis generic 100 mg

cialis online canada

next day delivery cialis

5 mg cialis

buy levitra now

buy propecia cheap

cialis canadian

buy prescription propecia

cialis for woman

cialis india

cialis transdermal

buy xenical propecia

natural levitra

buy propecia generic

cialis in mexico

generic levitra purchase

cialis levitra

discount brand name cialis

buy cheapest propecia

order cheap levitra

cheap viagra or cialis