A fast lexical analyzer with IDE

Environment: VC6 SP3, Win98-WinXP, STLPort 4.0/4.5.1 recommended (free)

'cxtPackage' is a library allowing custom lexical analyzers supporting the following features:

  • includes a fast generic tokenizer
  • uses a fast caching recursive-descent analyzer
  • the ruleset for the analyzer permits assigning precedence priorities to expressions
  • the parse tree is built according to the precedence prioritites
  • a small IDE for developing and testing the ruleset is included

This consists of the three projects 'cxTokenizer', 'cxAnalyzer' and 'cxtPackage' and provides an interface to the analyzer and tokenizer.
The setup of the analyzer is done with a plain-text initialization string, a rule definition for a simple expression evaluator could for example look like this:

std::tstringstream init(
    "200:+\n"       "201:-\n"
    "202:*\n"       "203:/\n"
    "204:^\n"       "205:;\n"
    "206:(\n"       "207:)\n"
    "' Whitespace tokens:\n"
    "0: \n"         "0:\t\n"
    "0:\\n\n"       "0:\\r\n"

The grammar-IDE
The grammar IDE included in the complete download provides an environment to develop and test analyzer rulesets. It has some syntax-highlighting features, shows errors by marking the lines in the editor and has an integrated test environment to live-check the results of the ruleset. I have no documentation yet and the ide is still early beta and it has some cosmetic bugs (for example, it is possible to insert via clipboard RTF-formatted text into the editor :-), but most of it is already usable.

How to use

I will submit a tutorial on how to use the library soon, but for now the sample projects included in the complete download must serve that purpose. Both 'emptyTestApp' and 'simpleCalc' are documented, so you should be able to see how it works. If you have questions you can also mail me: alexander-berthold@web.de.

'emptyTestApp' is an almost skeletal MFC dialog app and has a small piece of sample code in its 'OnOK' handler. 'simpleCalc' is a simple math expression evaluator.

Small code snippet

I confess I used hungarian notation, but some pieces of the code required me to do so because of a pretty confusing amount of classes when dealing with hierarchical linked lists :). Please don't flame me :)

// Source file 'EmptyTestAppDlg.cpp', line 200+:

// flush the internal token stream

// attach the input stream to the tokenizer/analyzer

// read until the next delimeter - no delimeters are
// set, i.e. read until the end of the input stream

// check for the '.expr' rules defined above
cxaTokenStream::const_iterator endpos;
cxaParseBranch *papbResult = NULL;
cxaStatusCookie ascCondition;

// test for all rules belonging to the same 
// group of rules with ID 300
papbResult =pkg.papbCheckForRule( 300,

  AfxMessageBox("Syntax error.");

  'papbResult' contains now the parse tree. You can for
  example dump its contents to the debug output using
  papbResult->vDump(), or you can load the ruleset in
  the IDE and explore it there.


Download complete project including grammarIDE - 213 Kb
Download source only - 162 Kb


  • which grammar?

    Posted by Legacy on 07/24/2003 12:00am

    Originally posted by: Max

    Could you provide any comments about which grammar types your lexer can parse? (LL, LR(1), ...) I can get the point (in fact have no time to look through the implementation to find this out).

  • Great, but how can i make my own...

    Posted by Legacy on 01/09/2002 12:00am

    Originally posted by: Roman

    great!, but can i get some information on how to say, make my own language that your package can parse and help me interpret?

    i looked at complex-sample and i was lost, most of the C++ statements said "syntax error" or such, and i dont know how to make my script language in it

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Anthony Christie, the Chief Marketing Officer for Level Communications, is responsible for customer experience, worldwide marketing and product management. In this informative asset, he shares his insights into why a private network connection to cloud-bases applications is the right decision for your enterprise. Download now to find out more.

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date