简体   繁体   中英

local variables converted into global variables?

I am passed in a C file. The C file contains functions which contain local variables. Given any C file, i have to be able to find the local variables within a specific method (which the user specifies) and make them global. (specifications for another application which can only handle global variables)...

What is the easiest way to do this? Would it be through shell scripting or should it directly in C. Should i use regex? Am i thinking about doing this the wrong way?

Any help would be appreciated

You are looking for a specific context of variables, in your case the context is local variables declared inside of a function. A regular expression can be used to pull out a function definition, but some kind of token parser would be much better.

Using a simplified grammar for C it can be found that you can get the function strings themselves and from there you can find the local variables themselves.

But, if the C code passed in is in a specific format, IE local variables are at the top of the function definition then a simple regular expression can be used.

C99 will allow variables to be declared throughout the function, within blocks etc., so the task of identifying them is complicated. It might be possible to leverage a lexer/parser like yacc and flex to tokenise a C source file, then pull the variable declarations out of the generated structures. Making this general purpose is likely to be a lot of work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM