![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Designing business rules for a beautifier.
I was working on a C beautifier a while back, but I was lacking time to get working on it much, now I have some spare time, but am kind of stuck because I can't define what the business rules are.
Let me explain what I mean. I read a line, which could be a whole program like: #include <stdio.h>int main(){char * booo = "what?!";printf("booo says: %s", booo);}But can also be just #include <stdio.h>int main (){char*booo="what?!" ;#include <stdio.h>
int main()
{
char * booo = "what?!";The beautifier will just remove or add spaces, newlines and tabs. All I really want to know now is when to place or remove those. For example, I could place an newline after a semicolon but it shouldn't do so in a for(;;) loop. There are so many diverse situations. When processing do I read keywords or characters? Do I memorize which keywords or characters I have seen? Would I do that with a huge switch or else if statement or are there better ways? I'm not very experienced with such things and would like to know how to solve such a thing. Ultimately I need a processline function that would turn the following: #include <stdio.h>
char * processline();
int main()
{
char * buffer = "#include<stdio.h>int main(){int i;char*booo=\"what?!\";for(i=0;i<3;i++)printf(\"booo says: %s\",booo);}";
char * newbuf = processline(buffer);
printf("%s\n", newbuf);
return 0;
}
char * processline(char * line)
{
/* do processing here */
return line;
}Into: #include <stdio.h>
int main()
{
int i;
char * booo = \"what?!\";
for(i = 0; i < 3; i++)
printf(\"booo says: %s\", booo);
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
You're on Windows. Are there lex & yacc (bison & flex) programs around for you to use? You need a lexer. Instead of UML, or an on-paper design, you're going to need to parse the code text block exactly the way C would see it. Then apply formatting rules which you can put up as business rules - but you need to know lexemes first.
I'm basing this suggestion on your description of munged source above. Failing that - consider getting GNU source for one or all of these: cb, bcpp, indent. These don't go to quite the extremes that I think you are beginning with. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|