设为首页 加入收藏

TOP

自己动手写C语言编译器(3)(二)
2014-11-23 22:37:13 来源: 作者: 【 】 浏览:8
Tags:自己 手写 语言 编译器
ctype[ch] = 0;

}

void commentChar(int ch)

{

if (ch >= 0 && ch < _COUNT_OF(ctype))

ctype[ch] = CT_COMMENT;

}

void quoteChar(int ch)

{

if (ch >= 0 && ch < _COUNT_OF(ctype))

ctype[ch] = CT_QUOTE;

}

void parseNumbers()

{

for (int i = '0'; i <= '9'; i++)

ctype[i] |= CT_DIGIT;

ctype['.'] |= CT_DIGIT;

ctype['-'] |= CT_DIGIT;

}

/**

* Determines whether or not ends of line are treated as tokens.

* If the flag argument is true, this tokenizer treats end of lines

* as tokens; the nextToken method returns

* TT_EOL and also sets the ttype field to

* this value when an end of line is read.

*

* A line is a sequence of characters ending with either a

* carriage-return character ('\r') or a newline

* character ('\n'). In addition, a carriage-return

* character followed immediately by a newline character is treated

* as a single end-of-line token.

*

* If the flag is false, end-of-line characters are

* treated as white space and serve only to separate tokens.

*

* @param flag true indicates that end-of-line characters

* are separate tokens; false indicates that

* end-of-line characters are white space.

* @see java.io.StreamTokenizer#nextToken()

* @see java.io.StreamTokenizer#ttype

* @see java.io.StreamTokenizer#TT_EOL

*/

void eolIsSignificant(bool flag)

{

eolIsSignificantP = flag;

}

void slashStarComments(bool flag)

{

slashStarCommentsP = flag;

}

void slashSlashComments(bool flag)

{

slashSlashCommentsP = flag;

}

void lowerCaseMode(bool fl)

{

forceLower = fl;

}

/** Read the next character */

private:

int read()

{

return input.get();

}

int nextToken() {

if (pushedBack) {

pushedBack = false;

return ttype;

}

unsigned char* ct = ctype;

int c = peekc;

if (c < 0)

c = NEED_CHAR;

if (c == SKIP_LF) {

c = read();

if (c < 0)

return ttype = TT_EOF;

if (c == '\n')

c = NEED_CHAR;

}

if (c == NEED_CHAR) {

c = read();

if (c < 0)

return ttype = TT_EOF;

}

ttype = c; /* Just to be safe */

/* Set peekc so that the next invocation of nextToken will read

* another character unless peekc is reset in this invocation

*/

peekc = NEED_CHAR;

int ctype = c < 256 ct[c] : CT_ALPHA;

while ((ctype & CT_WHITESPACE) != 0) {

首页 上一页 1 2 3 4 5 下一页 尾页 2/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇位运算及其应用实例(2) 下一篇自己动手写C语言编译器(5)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: