![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Sep 2005
Location: Požega, Croatia
Posts: 93
Rep Power: 4
![]() |
Searching in file... paradoxal problem.
I have one question...
I open .cpp source file, copy its contents in vector, and I wan't to search for functions in it. One way is to search for lines that look like this : "type nameoffunction (parameters, moreofthem)". This is where my problem begins... I don't know what is return type of function, its name, parameters... The question is : How can I search for unknown ? ... or, how can I determine if some line in vector is function ? ![]() |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
theoritical it's quite easy.. just keep a list somewhere of existing types. when a line begins with the name of that type, you search after that for some word. after that, check there is a ( or an =. If there is an (, you know it's a function.
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 137
Rep Power: 0
![]() |
As C and C++ are not picky about white space you might actually get a source that resembles this:
int
my_function_name
(
char *
some_parameter
,
int another_parameter)If you want to parse an input file I would suggest a tool like Lex (although, you will have to be familiar with C syntax to use it). Lex uses regular expressions to parse a file into tokens. So you might say: name [a-zA-z][0-9A-Za-z_]*
type void|int|char|long /*etc....*/
ws [ \t]+
lparen "("
rparen ")"
function {type}{ws}{name}{ws}{lparen}{optional parameters here}{rparen}Alternatively, you can define your own regular expressions. Either way you are in for a bit of work. Unfortunately, you can not always rely on the format of a file.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Sep 2005
Location: Požega, Croatia
Posts: 93
Rep Power: 4
![]() |
@Polyphemus
And what if someone who uses my lib makes his custom type? Here is my function that deals with I/O: void insertInfoHeader (char inputFile[],
char outputFile[],
Flags &infoHeaderFlags)
{
// input & output files
ifstream file_input;
ofstream file_output;
// will be used in copy(...) for writing file
ostream_iterator<string> os (file_output, "\n");
// vector for saving contents of file_input...
vector <string> file_buffer;
// and its iterator
vector <string>::const_iterator file_bufferBegin;
// used in getline loop as temporary storage
string currInputLine;
try
{
file_input.open (inputFile, ios::in);
file_output.open (outputFile, ios::trunc);
if (!(file_input))
reportError (FileErr ("Input"), OPEN_FAIL);
if (!(file_output))
reportError (FileErr ("Output"), OPEN_FAIL);
// Fill the buffer vector with lines from input file.
// thanks to ILoveVectors for this suggestion.
while ( getline( file_input, currInputLine, '\n' ) )
{
file_buffer.push_back (currInputLine);
}
// let's modify buffer vector...
// iterate through it
for ( file_bufferBegin = file_buffer.begin();
file_bufferBegin != file_buffer.end();
++file_bufferBegin; )
{
cout << endl
<< file_buffer.back ();
file_buffer.pop_back ();
}
// and finally, write changes into os.
// os is iterator for output file.
copy (file_buffer.begin(), file_buffer.end(), os);
}
catch (exception &exc)
{
cout << "\nException catched!\n"
<< __DATE__ << "\t" << __TIME__ <<" ."
<< "\nreport : " << exc.what ();
}
// close opened files
file_output.close();
file_input.close ();
} // insertInfoHeader()EDIT : The code is little messy... Probably becouse i used tabs instead of spaces.... |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Much content in a source file might become interpretable only in the light of other files (typedefs in a header, for example). It is not a trivial task. I wouldn't recommend blithely ignoring L7's post. Some things one doesn't care to consider remain facts, nonetheless.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
![]() |
|
|
|
|
|
|
#7 |
|
Programmer
Join Date: Sep 2005
Location: Požega, Croatia
Posts: 93
Rep Power: 4
![]() |
Polyphemus,
I think you didn't understand what I mean't... The program will be distributed as compiled .exe. When someone wants to use my program, he specifies input file. Program must open that file, search for functions, and generate function decription from it. eg. //===================================== // function : Type name // parameters : parameter1... etc... User may have class named Ass, and function Ass dropSomething (how_many, how_big) As I don't have telepathic abilities (except when I'm drunk), I can't know that some sick user will have function that returns Ass... ![]() Last edited by BinaryStorm; Sep 3rd, 2005 at 12:34 PM. |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Sep 2005
Location: Požega, Croatia
Posts: 93
Rep Power: 4
![]() |
L7Sqr,
I searched for Lex and got this : Lex.SourceForge.net Downloaded code, but has some problems compiling on Win. If anyone has some solution to this, please help... i will never manage to write this. :p |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 137
Rep Power: 0
![]() |
To address the idea of user created functions, which is complicated by (as DaWei pointed out) typedefs, classes, structs and other things you will want to probably define a grammar of sorts. And, don't you know, there is a tool for that. The tool is Yacc and it is designed to work with Lex.
At this point, however, you are building the beginings of a small compiler (Yet Another Compiler Compiler). As C makes it difficult (or lengthy, anyway) to manage these 'objects' you will be creating, you may want to investigate an alternative. A language such as OCaml, with Lex & Yacc equivalents built right in, is an option. It is not going to be a day in the park to learn, but you will benefit from it. If you decide to do this entirely in C++, without the help of external tools, the minimal requirement will be to decide what you will accept as proper grammer. Design regular expressions to test for these grammar and implement them somehow. You will need at least two passes throught the file. One as a 'preprocessing' pass, collecting information regarding types and small syntax requirements. The second will produce your 'tree' with checks into your aforementioned type definitions and full syntax review. You will still have the trouble of opening and reading header files for their definitions. What about support for things such as the standard library? Or other files that may or may not be pre-compiled? If you decide to exclude these items, what is the criteria for including a function definition in the tree or not? What you describe is not a simple task - per se. It will not be accomplished in a day by slick use of a vector. Just my opinion, of course. If you do decide to undertake the task, please post with any problems, as there are many herre who will be able to help.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
|
#10 | |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 137
Rep Power: 0
![]() |
here is a link to a place to download Flex (A win32 version). Flex can be run in Lex compatible mode or by itself.
From the sourceforge page for Lex: Quote:
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|