I've had this for a while, but it's still buggy. I remember that I thought it was neat at the time but I can't for the life of me remember what gave me the idea. Now that I think of it, I may rewrite it and solve the problems as well as update the processing for C++ declarations as well.
Sorry, I don't have any of my best C++ programs that I can post without embarassing myself on this computer. Old stuff from programming classes
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
static int cDecl ( void );
static void readTok ( void );
static void firstIdent ( void );
static void decl ( void );
static void pointers ( void );
static void arrays ( void );
static void args ( void );
struct Token { char tok; char buffer[200]; };
struct Token stack[200];
struct Token curr;
int top = -1;
int main( void )
{
printf ( "--Universal Declaration Translator--"
"\n written by Julienne Walker\n"
"\nInstructions: Enter a C style\n"
"declaration at the command line,\n"
"excluding argument names for \n"
"functions and ending with a semicolon.\n"
"\nTo exit the program, type x and hit\n"
"enter.\n"
"\nExamples:\n"
"const char* (*a)(int, char);\n"
"int func(double *, double);\n"
"char *a[50];\n"
"int i;\n\n"
"$: " );
while ( 1 ) {
firstIdent();
decl();
printf ( "\n" );
printf ( "$: " );
}
return EXIT_SUCCESS;
}
int cDecl ( void )
{
char *word = curr.buffer;
if ( !strcmp ( word, "const" ) ) {
strcpy ( word, "constant" );
return 1;
}
else if ( !strcmp ( word, "signed" ) ) return 2;
else if ( !strcmp ( word, "unsigned" ) ) return 2;
else if ( !strcmp ( word, "char" ) ) return 2;
else if ( !strcmp ( word, "short" ) ) return 2;
else if ( !strcmp ( word, "int" ) ) return 2;
else if ( !strcmp ( word, "long" ) ) return 2;
else if ( !strcmp ( word, "float" ) ) return 2;
else if ( !strcmp ( word, "double" ) ) return 2;
else if ( !strcmp ( word, "void" ) ) return 2;
else if ( !strcmp ( word, "struct" ) ) return 2;
else if ( !strcmp ( word, "enum" ) ) return 2;
else return 3;
}
void readTok ( void )
{
char *temp = curr.buffer;
while ( ( *temp = getchar() ) == ' ' ) ;
if ( *temp == 'x' ) {
printf ( "Good day\n");
exit( 0 );
}
if ( isalnum ( *temp ) ) {
while ( isalnum ( *++temp = getchar() ) ) ;
ungetc ( *temp, stdin );
*temp = '\0';
curr.tok = cDecl();
return;
} else if ( *temp == '*' ) {
strcpy ( curr.buffer, "pointer to" );
curr.tok = '*';
return;
}
curr.buffer[1] = '\0';
curr.tok = *temp;
return;
}
void firstIdent ( void )
{
readTok();
while ( curr.tok != 3 ) {
stack[++top] = curr;
readTok();
}
printf ( "%s is a ", curr.buffer );
readTok();
}
void decl ( void )
{
if ( curr.tok == '[' ) arrays();
else if ( curr.tok == '(' ) args();
pointers();
while ( top >= 0 ) {
if ( stack[top].tok == '(' ) {
stack[top--]; readTok(); decl();
} else printf ( "%s ", stack[top--].buffer );
}
}
void pointers ( void )
{ while ( stack[top].tok == '*' ) printf ( "%s ", stack[top--].buffer ); }
void arrays ( void )
{
while ( curr.tok == '[' ) {
printf ( "array " );
readTok();
if ( isdigit ( curr.buffer[0] ) ) {
printf ( "0 - %d ", atoi ( curr.buffer ) - 1 );
readTok();
}
readTok();
printf ( "of " );
}
}
void args ( void )
{
printf ( "function that takes a " );
while ( curr.tok != ')' ) {
readTok();
printf ( "%s ", curr.buffer );
readTok();
if ( curr.tok == '*' ) printf ( "pointer " );
if ( curr.tok != ')' ) {
readTok();
if ( curr.tok == ',' ) printf ( "and " );
}
}
readTok();
printf ( "as arguments and returns " );
}
-Prelude