[antlr-interest] C++, Rick, Not optimal Lexer's code.

Ruslan Zasukhin sunshine at public.kherson.ua
Mon Oct 18 23:47:08 PDT 2004


Hi All,
Hi Rick,

I think I see not optimal code in the nexttoken.
It looks in this way:


antlr::RefToken VSQL_Lexer::nextToken()
{
    antlr::RefToken theRetToken;
    for (;;) {
        antlr::RefToken theRetToken;
        int _ttype = antlr::Token::INVALID_TYPE;
        resetText();
        try {   // for lexical and char stream error handling

            switch ( LA(1)) {   <<<<<<<<<<<<<<<<<<< this is good.

            case static_cast<unsigned char>('('):
            {
                mLPAREN(true);
                theRetToken=_returnToken;
                break;
            }
            default:

                else if ((LA(1) == static_cast<unsigned char>('-')) &&
(LA(2) == static_cast<unsigned char>('>'))) {
                    mPTR(true);
                    theRetToken=_returnToken;
                }
                else if ((LA(1) == static_cast<unsigned char>('<')) &&
(LA(2) == static_cast<unsigned char>('>'))) {
                    mNE(true);
                    theRetToken=_returnToken;
                }
                else if ((LA(1) == static_cast<unsigned char>('>')) &&
(LA(2) == static_cast<unsigned char>('='))) {
                    mGE(true);
                    theRetToken=_returnToken;
                }
                else if ((LA(1) == static_cast<unsigned char>('<')) &&
(LA(2) == static_cast<unsigned char>('='))) {
                    mLE(true);
                    theRetToken=_returnToken;
                }
            }

-----------------------------------------------------------------

I think that in default clause we also must use switch

            default:

                switch( LA(1) ){

                case static_cast<unsigned char>('-')):{
                        if (LA(2) == static_cast<unsigned char>('>'))) {
                            mPTR(true);
                            theRetToken=_returnToken;
                        }
                }
                case static_cast<unsigned char>('<')) {
                        if(LA(2) == static_cast<unsigned char>('>'))) {
                           mNE(true);
                            theRetToken=_returnToken;
                        }
                }
 

In this way code become more natural and clean.
But the main is that we now call LA(1) only ONCE instead of 30-40 times (in
my case).

What you think Rick?
What others think ?


-- 
Best regards,
Ruslan Zasukhin      [ I feel the need...the need for speed ]
-------------------------------------------------------------
e-mail: ruslan at paradigmasoft.com
web: http://www.paradigmasoft.com

To subscribe to the Valentina mail list go to:
http://lists.macserve.net/mailman/listinfo/valentina
-------------------------------------------------------------

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20041019/f579d7e7/attachment.html


More information about the antlr-interest mailing list