Parsing Variables in VB6

I'm trying to locate and type my remaining variables after getting several projects going that share variables. We're talking several hundred variables. Need to generate lists of variables. Does anyone know of a parser that would make my job easier?

Greg Wright

Comments

  • : I'm trying to locate and type my remaining variables after getting several projects going that share variables. We're talking several hundred variables. Need to generate lists of variables. Does anyone know of a parser that would make my job easier?
    :
    : Greg Wright
    :

    What exactly are you trying to do? Are the variables declared in other VB source code files, and you are trying to re-use the same variable names and types?
    There are lots of ways of generating variable declarations, [italic]once you have a list of variable names an types[/italic].

    I know this problem is some weeks old and you may have solved it by now but let me know if you still need help.


  • [b][red]This message was edited by gdwright at 2002-11-14 16:57:34[/red][/b][hr]
    Thanks for your reply. The reason I want to parse the program is to generate a list of variables. Then I can write some simple code to change them from IniRec to strIniRec, I to intI, etc. This is note easy because of the number of variables involved. I found some analysis programs on the web but they ignored untyped local vars. Didn't know about typing standards when I started coding.
  • : [b][red]This message was edited by gdwright at 2002-11-14 16:57:34[/red][/b][hr]
    : Thanks for your reply. The reason I want to parse the program is to generate a list of variables. Then I can write some simple code to change them from IniRec to strIniRec, I to intI, etc. This is note easy because of the number of variables involved. I found some analysis programs on the web but they ignored untyped local vars. Didn't know about typing standards when I started coding.
    :
    I appreciate this is pretty tricky, there being so many ways a variable can be declared. More of a problem, you'd need to change the variable name everywhere it is used in the code.
    There's some powerful text-processing filters from UNIX-land (that you can get for windows) that might be up to the job, but they aren't easy to use, and i don't know how to.

    A feasible approach might be:
    Write a prog that lets you select a file, then reads the file in a line at a time (Line Input # statement). Check if the line ends in a line-continuation char ('_') and if so add the next line too.
    If the line starts with Dim, Private or whatever, parse the string in code to identify the variable names. Parsing could go like this:
    -> strip the declaration keyword from the start of the line
    -> split the remainder around the commas (Split() function)
    -> take the first word in the split.

    Array declarations may be difficult to differentiate from Subs, Procedures and class methods. There are no doubt many other Hidden Dangers, so a trial-and-error approach may be required. I'd recommend that early attempts should err on the side of finding too many false declarations rather than too few genuine ones.

    Another approach is Visual Basic Extensibility. This lets you write an add-in that can inspect the project as an object model, so you find all the properties of module-scope members. This would be more difficult but potentially more powerful, so it depends on how much work it would actually save you. I don't think the model knows about local vars though - which is probably why those analysers didn't find them - so you'd still have to parse the declaration lines inside each function.

    It's likely that your results will at best be partially successfull - and you'll never be sure until you test each programme. [b]Watch out[/b] for code modules that don't have Option Explicit as the first line or you're heading for problems...

    Now, once you've got your list, how do you intend to actually make the substitutions? Search & Replace in VB? A text editor? Do you intend to use the variable-name list to drive an automated process?

    [b]Finally, are you sure you need to do this at all (if it ain't broke...)? [/b] Coding style standards are cool but they don't make a working program work any better. If you rename all your vars, at best you'll still have exactly the same program. Anything less than perfection and you've damaged it.

  • Wow! Who are you? Your response made me feel like I just received a tennis lesson from Venus Williams or some other expert. (I'm a tennis fan) You make perfect sense. Your last comment about "am I sure if I need to do this" has me really thinking. But before I address that, here is the method I was expecting to use.

    I originally wanted to change the name of my global type variables only. So I coded a routine that searched line by line for a variable, and then performed a replace with the new name. This worked well except for sequences of chars that were contained within longer vars. I then started using the syntax from the IDE to parse using spaces, parenthesis, commas, etc. This also met with limited success and it was better than the replace method. Then I started thinking that surely someone had figured this out ... and did this posting. If someone has, they are carefully guarding it.

    Now I'm wondering if I may have a potential product that would make mass changes to a project. What do you think? Is there a need?

    As far as do I need to do this, maybe, but not right now. If I break my modules into dlls, I have to add the object reference to all vars. If I can do this, then I should be able to just add a prefix to the vars at the same time. I decieded, and you helped, to hold off for the time being on typing my vars, gradually develop my parser, and make sure the code that I already have is even more perfect. This will work for me.

    Thanks
    Greg Wright

  • : Wow! Who are you? Your response made me feel like I just received a tennis lesson from Venus Williams or some other expert. (I'm a tennis fan) You make perfect sense. Your last comment about "am I sure if I need to do this" has me really thinking. But before I address that, here is the method I was expecting to use.
    :
    : I originally wanted to change the name of my global type variables only. So I coded a routine that searched line by line for a variable, and then performed a replace with the new name. This worked well except for sequences of chars that were contained within longer vars. I then started using the syntax from the IDE to parse using spaces, parenthesis, commas, etc. This also met with limited success and it was better than the replace method. Then I started thinking that surely someone had figured this out ... and did this posting. If someone has, they are carefully guarding it.
    :
    : Now I'm wondering if I may have a potential product that would make mass changes to a project. What do you think? Is there a need?
    :
    : As far as do I need to do this, maybe, but not right now. If I break my modules into dlls, I have to add the object reference to all vars. If I can do this, then I should be able to just add a prefix to the vars at the same time. I decieded, and you helped, to hold off for the time being on typing my vars, gradually develop my parser, and make sure the code that I already have is even more perfect. This will work for me.
    :
    : Thanks
    : Greg Wright
    :
    :
    Hi Greg

    sorry if i went on a bit there, i tend to get carried away...
    I'd agree, probably every programmer comes up against this at some time. Some months ago i started to write a VB add-in that would search & replace using wildcards in the 'replace' part, thinking it would take me a day or so... i dicovered that i would need an almost complete parser - and that's just for the search part. The replace part would have meant inventng a small programming language. I guess this all comes under Text Processing, which is a big field in itself. Guess where that project went.

    However, are you aware of the [b]Find Whole Word Only[/b] option in VBs search & replace? This solves the problem where a var name appears as part of a larger term, or where the var appears right next to a bracket or whatever. If you haven't noticed it yet (and i didn't until i'd been using vb for ages!) then you'll find it's really useful.

    I'm also wondering wether you are hoping that adding a prefix to the var names will actually constrain their types. In early versions of basic, you could for example make a variable behave like an integer just by giving it a name that started with a small 'i'. Most modern languages (VB included) - don't do that kind of thing. The only way to fix the [italic]type[/italic] of a var is to specify it in a declaration. The only constraint on a vars name is that it should be meaningfull to the programmer, which is why many programmers want their integers to start with 'i'. I think you are aware of this already but i thought i'd check anyway!

    rik

    p.s. i'm not much of a tennis fan (we don't have many in scotland) but if Venus ever needs any vb lessons, i could sure work out an arrangement.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories