游戏开发论坛

 找回密码
 立即注册
搜索
12
返回列表 发新帖
楼主: instemast

求 Lex 和 Yacc 可用的下载

[复制链接]

121

主题

2029

帖子

2034

积分

金牌会员

Rank: 6Rank: 6

积分
2034
QQ
发表于 2008-7-29 09:33:00 | 显示全部楼层

Re:求 Lex 和 Yacc 可用的下载

我没那么麻烦,直接拆了Cygwin的binary在用,也挺好的。

362

主题

3023

帖子

3553

积分

论坛元老

Rank: 8Rank: 8

积分
3553
 楼主| 发表于 2008-7-29 20:27:00 | 显示全部楼层

Re: Re:求 Lex 和 Yacc 可用的下载

instemast: Re:求 Lex 和 Yacc 可用的下载

呵呵,是啊。我看了下正则表达式和NFA,的确比我这样手工编码DFA简单多了。
前者是 using ,后者是 making...


呵呵,我有个macro,比如:
GET_SET (Int32, Property)

#define GET_SET(type,x)private: type m_##x;public: type Get##x()const{ return m_##x; }void Set##x(type val){ m_##x = val; }

不过最近感觉这个比较难看,所以干脆不用了,只在临时赶时间的时候用。

362

主题

3023

帖子

3553

积分

论坛元老

Rank: 8Rank: 8

积分
3553
 楼主| 发表于 2008-7-30 02:24:00 | 显示全部楼层

Re:求 Lex 和 Yacc 可用的下载

__forceinline Bool IsNum(WChar ch){ return ch>='0' && ch <='9'; }
__forceinline Bool IsEng(WChar ch){ return (ch>='A'&&ch <='Z') || (ch>='a'&&ch <='z') || ch=='_'; }
__forceinline Bool IsSpace(WChar ch){ return ch==10 || ch==13 || ch==' ' || ch==9; }
__forceinline Bool IsCrlf(WChar ch){ return ch==10 || ch==13; }

void CLexer::NextToken(TOKEN *pOut)
{
        INST_ASSERT(m_pSrc);
        INST_ASSERT(pOut);

        pOut->len = 0;

        static WChar ch = 0;

        goto _start;

_start:
        if( 0 == *m_p )                {                                                goto _eof;                } // 仅在初态中判断 eof 即可
        if( IsSpace(*m_p) )        {                                m_p++;        goto _start;        }
        if( '/' == *m_p )        { pOut->len++;        m_p++;        goto _div;                } // 目前来看,需要保存这个 '/'
        if( IsNum(*m_p) )        { pOut->len++;        m_p++;        goto _error;        } // 不支持数字,出错,保存并输出该数字
        if( IsEng(*m_p) )        { pOut->len++;        m_p++;        goto _eng;                }
        if( ';' == *m_p )        { pOut->len++;        m_p++;        goto _semi;                }
        if( '{' == *m_p )        { pOut->len++;        m_p++;        goto _left;                }
        if( '}' == *m_p )        { pOut->len++;        m_p++;        goto _right;        }
        else                                { pOut->len++;        m_p++;        goto _error;        } // 不支持当前的字符,出错,保存并输出该字符

_div:
        if( '/' == *m_p )        { pOut->len--;        m_p++;        goto _comment;        } // 原来是注释啊,那么,清除刚刚的 '/'.( assert: pOut->len == 1 )
        else                                {                                                goto _error;        } // 不支持除号,出错,并输出刚刚的 '/'(不是当前而是刚刚的字符非法,故指针不动)

_comment:
if( 0 == *m_p )                {                                                goto _eof;                }
        if( IsCrlf(*m_p) )        {                                m_p++;        goto _start;        }
        else                                {                                m_p++;        goto _comment;        }

_eng:
        if( IsNum(*m_p) )        { pOut->len++;        m_p++;        goto _eng;                }
        if( IsEng(*m_p) )        { pOut->len++;        m_p++;        goto _eng;                }
        else                                {                                                goto _eng_fnl;        }

_eng_fnl:
        pOut->type = TT_IDENT;
        INST_ASSERT( pOut->len > 0 );
        pOut->ptr = m_p - pOut->len;        // 根据(该token的结尾+1)指针,和读取的长度,反过来计算出该token的开头指针.
        return;        // 稍候调用MapToKeyword即可...

_semi:
        pOut->type = TT_SEMI;
        INST_ASSERT( 1 == pOut->len );
        pOut->ptr = m_p - 1;        // 根据(该token的结尾+1)指针,和读取的长度,反过来计算出该token的开头指针.
        return;

_left:
        pOut->type = TT_BRACE_L;
        INST_ASSERT( 1 == pOut->len );
        pOut->ptr = m_p - 1;        // 根据(该token的结尾+1)指针,和读取的长度,反过来计算出该token的开头指针.
        return;

_right:
        pOut->type = TT_BRACE_R;
        INST_ASSERT( 1 == pOut->len );
        pOut->ptr = m_p - 1;        // 根据(该token的结尾+1)指针,和读取的长度,反过来计算出该token的开头指针.
        return;

_eof:
        pOut->type = TT_EOF;
        INST_ASSERT( 0 == pOut->len );
        pOut->ptr = null;
        return;

_error:
        pOut->type = TT_ERROR;
        pOut->ptr = m_p - pOut->len;        // 根据(该token的结尾+1)指针,和读取的长度,反过来计算出该token的开头指针.
        return;
}

121

主题

2029

帖子

2034

积分

金牌会员

Rank: 6Rank: 6

积分
2034
QQ
发表于 2008-7-30 08:24:00 | 显示全部楼层

Re:求 Lex 和 Yacc 可用的下载

叫什么GetSet还不如叫ACCESSOR
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-21 20:48

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表