python表达式词法分析包_python实现词法分析

#请先安装Ply

# -*- coding: utf-8 -*-

#--------------------------------------------------------------------------

#Author:Jmdebugger

#email: pengkailb@gmail.com

#date: 2013-9-17

#--------------------------------------------------------------------------

import ply.lex as lex

tokens = [

"TOKEN_IDENT",

"TOKEN_INT",

"TOKEN_FLOAT",

"TOKEN_STRING",

"TOKEN_OP",

"TOKEN_DELIM_COMMA", #,

"TOKEN_DELIM_OPEN_PAREN", #(

"TOKEN_DELIM_CLOSE_PAREN", #)

"TOKEN_DELIM_OPEN_BRACKET", #[

"TOKEN_DELIM_CLOSE_BRACKET",#]

"TOKEN_DELIM_OPEN_BRACE", #{

"TOKEN_DELIM_CLOSE_BRACE", #}

"TOKEN_DELIM_SEMICOLON" #;

]

reserved = {

'if' : 'TOKEN_RSRVD_IF',

'else' : 'TOKEN_RSRVD_ELSE',

'true' : 'TOKEN_RSRVD_TRUE',

'false' : 'TOKEN_RSRVD_FALSE',

'while' : 'TOKEN_RSRVD_WHILE',

'break' : 'TOKEN_RSRVD_BREAK',

'continue': 'TOKEN_RSRVD_CONTINUE',

'goto' : 'TOKEN_RSRVD_GOTO',

'func' : 'TOKEN_RSRVD_FUNC',

'var' : 'TOKEN_RSRVD_VAR',

'for' : 'TOKEN_RSRVD_FOR',

'return' : 'TOKEN_RSRVD_RETURN'

}

tokens += reserved .values()

t_ignore = r' \t\r'

def t_COMMENT(t):

r'(/\*(.|\n)*?\*/)|(\/\/.*)'

pass

def t_newline(t):

r'\n+'

t.lexer.lineno += len(t.value)

def t_error(t):

print "LaunchScript error: "+repr(t.value)

def t_TOKEN_IDENT(t):

r'[a-zA-Z_][a-zA-Z_0-9]*' #标识符

t.type = reserved.get(t.value , 'TOKEN_IDENT')

return t

def t_TOKEN_INT(t):

r'(0x[a-fA-F0-9]+)|([0-9]+)'

return t

t_TOKEN_FLOAT = r'[0-9]*\.[0-9]+'

t_TOKEN_STRING = r'(\"([^\\\r]|(\\.))*?\")' #|(\"([^\\\n]|(\\.))*?\")' only for windows

t_TOKEN_DELIM_COMMA = r'\,'

t_TOKEN_DELIM_OPEN_PAREN = r'\('

t_TOKEN_DELIM_CLOSE_PAREN = r'\)'

t_TOKEN_DELIM_OPEN_BRACKET = r'\['

t_TOKEN_DELIM_CLOSE_BRACKET = r'\]'

t_TOKEN_DELIM_OPEN_BRACE = r'\{'

t_TOKEN_DELIM_CLOSE_BRACE = r'\}'

t_TOKEN_DELIM_SEMICOLON = r'\;'

def t_TOKEN_OP(t):

r'(\\>\=)|([\+\-\*\/\%\&\|\^\=\!\>\\

return t

if __name__ == "__main__":

lexer = lex.lex()

f = open("./test.txt" , 'rb')

data = f.read()

f.close()

lexer.input(data)

while True:

tok = lexer.token()

if not tok: break # No more input

print tok.value+"\t---->\t"+tok.type

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值