How to find all variables in C # source code?
Here's my idle grammar that doesn't work, because ZeroOrMore (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) + ";"
For example, my line input is:
int k1 = 89, k2 = 6; public static int alpha = 54 ;
In the output file:
{'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'}
Maybe there is another way to implement this grammar, or how to make this grammar?