Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 22nd, 2006, 3:03 AM   #1
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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?!"     ;
In this last case I want to process that line to:
#include <stdio.h>

int main()
{
	char * booo = "what?!";
Which would be saved to a new buffer.

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
nnxion is offline   Reply With Quote
Old Feb 22nd, 2006, 9:05 AM   #2
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
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.
jim mcnamara is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:52 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC