How do i make a simple parser that parse strings
ex.
"string1 string2 string3"
and store in a vector?
how can it be done using BOOST.Spirit?
---
leaf 10 2370
need BOOST.Spirit?just using strtok
modify the MSDN simple
char string1[] = "string1 string2 string3";
char seps[] = " ";
char *token;
vector<string >(temp)
void main( void )
{
printf( "%s\n\nTokens:\n", string1 );
/* Establish string and get the first token: */
token = strtok( string1, seps );
while( token != NULL )
{
/* While there are tokens in "string1" */
printf( " %s\n", token );
temp.push_back(token);
/* Get next token: */
token = strtok( NULL, seps );
}
}
tried it and it worked.
but my actual implementation is something like parsing:
"public: __thiscall unsigned int FunctionName( type, type, type ... )"
---
leaf
the array seps to separate the string can stroe a few characters .so ur
string can aslo be parsing.
In article <11**********************@p10g2000cwp.googlegroups .com>,
"Hippo" <hi*******@hotmail.com> wrote: need BOOST.Spirit?just using strtok modify the MSDN simple char string1[] = "string1 string2 string3"; char seps[] = " "; char *token; vector<string >(temp) void main( void ) { printf( "%s\n\nTokens:\n", string1 ); /* Establish string and get the first token: */ token = strtok( string1, seps ); while( token != NULL ) { /* While there are tokens in "string1" */ printf( " %s\n", token ); temp.push_back(token); /* Get next token: */ token = strtok( NULL, seps ); } }
That looks like too much work to me, all to use a rather dangerous
function.
How about this instead:
void fn( const string& in, vector<string>& vec )
{
istringstream iss( in );
copy( istream_iterator<string>( iss ), istream_iterator<string>(),
back_inserter( vec ) );
}
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
What bothers me is the problem of ensuring that the strings that are
push'ed inside the vector are correct.
consider this string "public: __thiscall unsigned int
Class::FunctionName( type, type, type ... )"
Using Hippo's code the problem was solved but one still remained the
"Class::Function" whereas they should be seperated.
BTW, what i'm doing is is pushing the elements of a function prototype.
( extracted from a DLL )
Problem is when Return Types are "unsigned int" etc.
Another problem arise when the prototype is rather incomplete (
elements in the vector position is very important )
Regards,
leaf
In article <11*********************@v46g2000cwv.googlegroups. com>,
"leaf" <ad********@gmail.com> wrote: What bothers me is the problem of ensuring that the strings that are push'ed inside the vector are correct. consider this string "public: __thiscall unsigned int Class::FunctionName( type, type, type ... )" Using Hippo's code the problem was solved but one still remained the "Class::Function" whereas they should be seperated.
BTW, what i'm doing is is pushing the elements of a function prototype. ( extracted from a DLL )
Problem is when Return Types are "unsigned int" etc.
Another problem arise when the prototype is rather incomplete ( elements in the vector position is very important )
Are you trying to parse c++ code in general? I suggest you buy or get a
tool then...
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Not really, i mean i need to parse the DLL from my own application. The
information (stored in vectors) should be inside the application
because later it will be used as a reference table...
leaf wrote: How do i make a simple parser that parse strings ex. "string1 string2 string3" and store in a vector?
What kind of delimiters do you want, characters or strings? What should
be done with adjacent delimiters, should the merge (strtok() style, or
insert a 0-length string?
These are important questions before the nuts and bolts of a solution
can be derived.
Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
I see, all i need to to is to parse the 'function prototype string'
store it in a vector,
to be specific :
std::vector<stFunction> fun; // FunctionStruct
....
struct stFunction{
// simple variable spec
enum eVarType{
VAR_VOID, VAR_BOOL, VAR_INT, VAR,
};
// possible calling conventions
enum eCallType{
CALL_CDECL, CALL_STDCALL, CALL_THISCALL,
};
typedef std::vector<eVarType> ParamVec;
void* m_Proc;
eVarType m_ReturnType;
ParamVec m_ParamTypes;
eCallType m_CallType;
std::string m_Name;
};
There, from the string say:
"public: void __thiscall MyClass::MyFunc(unsigned int, unsigned int)"
stFunction structure will be filled...
---
Young Leaf
Ayon kay Default User: leaf wrote:
How do i make a simple parser that parse strings ex. "string1 string2 string3" and store in a vector?
What kind of delimiters do you want, characters or strings? What should be done with adjacent delimiters, should the merge (strtok() style, or insert a 0-length string?
These are important questions before the nuts and bolts of a solution can be derived.
Brian
-- Please quote enough of the previous message for context. To do so from Google, click "show options" and use the Reply shown in the expanded header.
In article <11**********************@u72g2000cwu.googlegroups .com>,
"leaf" <ad********@gmail.com> wrote: I see, all i need to to is to parse the 'function prototype string' store it in a vector, to be specific : std::vector<stFunction> fun; // FunctionStruct ... struct stFunction{ // simple variable spec enum eVarType{ VAR_VOID, VAR_BOOL, VAR_INT, VAR, }; // possible calling conventions enum eCallType{ CALL_CDECL, CALL_STDCALL, CALL_THISCALL, }; typedef std::vector<eVarType> ParamVec;
void* m_Proc; eVarType m_ReturnType; ParamVec m_ParamTypes; eCallType m_CallType; std::string m_Name; };
There, from the string say: "public: void __thiscall MyClass::MyFunc(unsigned int, unsigned int)" stFunction structure will be filled...
Start with a test:
void fn() {
stFunction out = parse( "int main()" );
assert( out.eVarType == VAR_INT );
// etc... for the other asserts.
}
int main() {
fn();
cout << "working\n";
}
Take the above and start writing code until it will compile, then write
more code until none of the asserts fire. Then write a slightly more
complicated test and repeat.
For an example, do a google group search for ["a Recursive Descent
Parser via TDD" phlip]
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Paul Czubilinski |
last post by:
Hello,
I would like to make a simple parser which should work as follows:
this is my text {link:href=mypage,link_text} some other text
{link:href=otherpage,other_description}
/|\
|
|
by: Trimbitas Sorin |
last post by:
Hello
I have a simple syntax question :
What does the following line mean:
1: %checkType; ?? I know that @test="" is an array and $test="" is a
simple variable.
Thank you
With best regards...
|
by: Jason Swett |
last post by:
I want to do graphics with C++. Surprisingly, so far nobody has been
able to tell me anything helpful. How do I do it? Any input would be
greatly appreciated.
Jason
|
by: Alan |
last post by:
hi all,
I want to define a constant length string, say 4
then in a function at some time, I want to set the string to a constant
value, say a
below is my code but it fails
what is the correct...
|
by: msnews.microsoft.com |
last post by:
Hello All,
In the "Find Dialog" (Ctrl-F) of the IDE there is an option called "Mark
All".
When I click "Mark All", this marks the occurences of my search text.
How will I clear this mark?
...
|
by: Scott Frankel |
last post by:
Still too new to SQL to have run across this yet ...
How does one return the latest row from a table, given multiple entries
of varying data?
i.e.: given a table that looks like this:
color...
|
by: jacob navia |
last post by:
Summary:
I have changed (as proposed by Chuck) the code to use isalpha()
instead of (c>='a' && c <= 'z') etc.
I agree that EBCDIC exists :-)
I eliminated the goto statement, obviously it is...
|
by: Chrism2671 |
last post by:
I'm new to XSLT/XML and I have a very simple, quick question. i've
been trying to convert simple xml files into CSV files and have made a
simple XSLT template using the w3 tutorials, but it doesn't...
|
by: Oriane |
last post by:
Hi,
With Asp.net 2.0, when a internet user logs in with a "login authentication
form", is the password encrypted when it is sent to the server ? Is is
hashed ?
Best regards
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |