使用pyparsing定义变量语法C

2024-09-30 14:21:57 发布

您现在位置:Python中文网/ 问答频道 /正文

如何找到源代码中的所有变量?
这是我的非工作语法,因为zeroor或more(VarDef4)而不起作用

Protected = Literal("protected")  
Private = Literal("private")  
Public = Literal("public")  
Modification = Public^Private^Protected  

Static = Literal("static")  
Auto = Literal("Auto")   
MemoryType =(Static^Auto)  
Bool = Literal("bool"); Byte = Literal("byte"); Char = Literal("char"); Decimal = Literal("decimal"); Double = Literal("double"); Enum = Literal("enum")
Float = Literal("float"); Int = Literal("int"); Long = Literal ("long"); Sbyte = Literal("sbyte"); Short = Literal("short")
Struct = Literal("struct"); String = Literal("string"); Uint = Literal("uint"); Ulong = Literal("ulong"); Ushort = Literal("ushort")  

VariableType = (Bool)^(Byte)^(Char)^(Decimal)^(Double)^(Enum)^(Float)^(Int)^(Long)^(Sbyte)^(Short)^(Struct)^(String)^(Uint)^(Ulong)^(Ushort)    

Variable = Word(alphanums + '_' + '$'+ '.' ) 
VarDef2 = '=' + OneOrMore(Word(alphanums + '_' + '$' + '"' + '-'+ "'" ) )  
VarDef3 = ZeroOrMore("," + Variable + Optional(VarDef2))  
VarDef4 = VarDef2^VarDef3 VarDef =  Optional(Modification) + Optional(MemoryType) + (VariableType) + Variable  + ZeroOrMore(VarDef4) + ";"

例如,给定的字符串输入:

^{pr2}$

在输出文件中:

{'Row': 1, 'Construct': "['int', 'k1', '89]", "['int', 'k2', '6']", 'Variable' : k1, k2, 'VariableType': int, 'Modification': not defined, 'MemoryType': not defined, 'Line': 'int k1 = 89, k2 = 6'}  
{'Row': 2, 'Construct': "['public', 'static', 'int', 'alpha']", 'Variable': alpha, 'VariableType': int,'Modification': public, 'MemoryType': static, 'Line': 'public static int alpha = 54'} 

也许有另一种方法来实现这个语法,或者如何制作这个语法?在


Tags: auto语法statick2k1publicvariableoptional