查找特定令牌

时间:2013-04-30 05:06:28

标签: compiler-construction lex

我正在使用lex来查找令牌。我想获取令牌类之后的Identifier令牌值,我该怎么做。

例如我的函数应该在类令牌之后返回标识符。有没有办法这样做?

lex生成的词汇标记如下: -

     LexToken(SEMICOLON,';',15,125)
     LexToken(RETURN,'return',16,127)
     LexToken(SEMICOLON,';',16,134)
     LexToken(RCURLY,'}',17,136)
     LexToken(CLASS,'class',20,140)
     LexToken(IDENTIFIER,'animal',20,146)

在将此令牌流作为输入函数后,它应该返回'animal',因为它是“class”令牌后面的令牌'Identifier'的值。

1 个答案:

答案 0 :(得分:1)

while True:
    tok = lexer.token()
    if not tok: break      # No more input
    print tok.type, tok.value, tok.line, tok.lexpos 
if flag==1:
   flag=0
   print tok.value       # here u get the value.
if tok.type=='CLASS':
    flag=1