暂不支持多重继承
plyjava.py:
[Python] 纯文本查看 复制代码
tokens = (
'PACKAGE',
'PACKAGENAME',
'COMMA',
'ID',
'PUBLIC',
'CLASS',
'LBRACE',
'INTERFACE',
'EXTENDS',
'IMPLEMENTS',
'PACKAGEPATH',
'IMPORT'
)
states = (
('multicomment','exclusive'),
('package','exclusive'),
)
reserved = {
'public' : 'PUBLIC',
'class' : 'CLASS',
'extends' : 'EXTENDS',
'implements' : 'IMPLEMENTS',
'interface' : 'INTERFACE',
'import' : 'IMPORT'
}
def t_LBRACE(t):
r'\{'
return t
def t_COMMENT(t):
r'\/\/.*'
def t_PACKAGE(t):
r'\bpackage\b'
t.lexer.begin('package')
return t
def t_package_PACKAGENAME(t):
r'([a-zA-Z_$][a-zA-Z_$0-9]*)(\.[a-zA-Z_$][a-zA-Z_$0-9]*)*'
return t
t_package_ignore = " \t"
def t_package_COMMA(t):
r';'
t.lexer.begin('INITIAL')
return t
def t_package_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(ply_java_filelen)
def t_LCOMMENT(t):
r'\/\*'
t.lexer.begin('multicomment')
def t_multicomment_newline(t):
r'[\r\n]+'
def t_multicomment_RCOMMENT(t):
r'\*\/'
t.lexer.begin('INITIAL')
def t_multicomment_MCOMMENT(t):
r'((?!\*\/).)+'
def t_multicomment_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(ply_java_filelen)
t_multicomment_ignore = ""
def t_PACKAGEPATH(t):
r'([a-zA-Z_$][a-zA-Z_$0-9]*)(\.[a-zA-Z_$][a-zA-Z_$0-9]*)+'
return t
def t_ID(t):
r'[a-zA-Z_$][a-zA-Z_$0-9]*'
t.type = reserved.get(t.value,'ID') # Check for reserved words
return t
def t_COMMA(t):
r';'
return t
def t_newline(t):
r'[\r\n]+'
def t_error(t):
#print("Illegal character '%s'" % t.value[0])
t.lexer.skip(ply_java_filelen)
# Ignored characters
t_ignore = " \t"
def p_error(t):
#print("Syntax error at '%s'" % t.value)
pass
def p_prog_java(t):
'''prog : pkgline importlines decors PUBLIC decors CLASS ID extend PACKAGEPATH LBRACE
| pkgline importlines decors PUBLIC decors INTERFACE ID extend PACKAGEPATH LBRACE
| pkgline importlines decors PUBLIC decors CLASS ID extend ID LBRACE
| pkgline importlines decors PUBLIC decors INTERFACE ID extend ID LBRACE
| pkgline importlines decors PUBLIC decors CLASS ID LBRACE
| pkgline importlines decors PUBLIC decors INTERFACE ID LBRACE'''
global ply_java_classname
ply_java_classname = t[7]
def p_importlines_java(t):
'''importlines : importline
| '''
def p_importline_java(t):
'''importline : importline import
| import'''
def p_import_java(t):
'import : IMPORT PACKAGEPATH COMMA'
global ply_java_depends
ply_java_depends.append(t[2])
def p_pkgline_java(t):
"pkgline : PACKAGE pkgname COMMA"
global ply_java_pkgname
ply_java_pkgname = t[2]
def p_extend_java(t):
'''extend : EXTENDS
| IMPLEMENTS'''
def p_pkgname_java(t):
'pkgname : PACKAGENAME'
t[0] = t[1]
def p_decors_java(t):
'''decors : decor
| '''
def p_decor_java(t):
'''decor : decor ID
| ID'''
ply_java_filelen = 0
ply_java_pkgname = ''
ply_java_classname = ''
ply_java_depends = []
import ply.lex as lex
lexer = lex.lex()
import ply.yacc as yacc
parser = yacc.yacc()
def analyzeJava(fname):
global ply_java_pkgname,ply_java_classname,ply_java_depends,ply_java_filelen
ply_java_pkgname = ""
ply_java_classname = ""
ply_java_depends.clear()
ply_java_filelen = 0
try:
f = open(fname,"r")
f.seek(0,2)
ply_java_filelen = f.tell()
f.seek(0,0)
buffer = f.read(ply_java_filelen)
f.close()
except Exception as e:
print(str(e))
parser.parse(buffer)
return {'pkgname':ply_java_pkgname,'classname':ply_java_classname,'depends':ply_java_depends}
client.py:
[Asm] 纯文本查看 复制代码 import plyjava
print(plyjava.analyzeJava("Socket.java")) |