We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

This resource has not currently been approved, and is not currently linked to from our directory of resources. It is being displayed here for preview by the author and moderators only.
Rated
Read 244 times

Contents

Related Categories

The use of the code analysis library OpenC++: modifications, improvements, error corrections - 2. Addition of a new lexeme.

AndreyKarpov

2. Addition of a new lexeme.

If you want to add a keyword which should be processed, you need to create a new lexeme ("token"). Let's look at the example of adding a new keyword "__w64". At first create an identifier of the new lexeme (see token-name.h file), for example in this way:

enum {	
  Identifier = 258,
  Constant = 262,
  ...
  W64 = 346, // New token name
  ...
};

Modernize the table "table" in lex.cc file:

static rw_table table[] = {
	...
    { "__w64",       W64 },
	...
};

The next step is to create a class for the new lexeme, which we'll call LeafW64.

namespace Opencxx
{
class LeafW64 : public LeafReserved {
public:
  LeafW64(Token& t) : LeafReserved(t) {}
  LeafW64(char* str, ptrdiff_t len) :
     LeafReserved(str, len) {}
  ptrdiff_t What() { return W64; }
};
}

To create an object we'll need to modify optIntegralTypeOrClassSpec() function:

...
case UNSIGNED :
  flag = 'U';
  kw = new (GC) LeafUNSIGNED(tk);
  break;
case W64 : // NEW!
  flag = 'W';
  kw = new (GC) LeafW64(tk);
  break;
...

Pay attention that as far as we've decided to refer "__w64" to data types, we'll need the 'W' symbol for coding this type. You may learn more about type coding mechanism in Encoding.cc file.

Introducing a new type we must remember that we need to modernize such functions as Parser::isTypeSpecifier() for example.

And the last important point is modification of Encoding::MakePtree function:

Ptree* Encoding::MakePtree(unsigned char*& encoded, Ptree* decl)
{
	...
        case 'W' :
            typespec = PtreeUtil::Snoc(typespec, w64_t);
            break;
	...
}

Of course, it is only an example, and adding other lexemes may take much more efforts. A good way to add a new lexeme correctly is to take one close to it in sense and then find and examine all the places in OpenC++ library where it is used.

Comments