473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Simple parser question...

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

Mar 10 '06 #1
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 );
}
}

Mar 10 '06 #2
tried it and it worked.

but my actual implementation is something like parsing:
"public: __thiscall unsigned int FunctionName( type, type, type ... )"

---
leaf

Mar 10 '06 #3
the array seps to separate the string can stroe a few characters .so ur
string can aslo be parsing.

Mar 10 '06 #4
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.
Mar 10 '06 #5
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

Mar 10 '06 #6
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.
Mar 10 '06 #7
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...

Mar 10 '06 #8
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.
Mar 10 '06 #9
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.


Mar 10 '06 #10
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.
Mar 10 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
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} /|\ |
2
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...
13
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
51
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...
3
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? ...
7
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...
26
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...
3
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...
5
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
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
Oralloy
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,...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.