[ create a new paste ] login | about

Link: http://codepad.org/hXmKTsIE    [ raw code | fork ]

ninwa - C, pasted on Sep 8:
/* The C Programming Language,
   my solution for exercise 1-24

   A very rough C syntax error-finder. Simply ensures
   that there are matching open /closing characters for:

		single-quote: '
		double-quote: "
		parens: ()
		brackets: []
		curly-braces: {}

	The program will ignore \' and \" as to not get false results. The program
	will also ignore any bracket type characters within strings or single-quotes
	as to not give fals results.
*/

#include <stdio.h>

#define	SINGLE	0
#define	DOUBLE	1
#define	PAREN	2
#define	BRACKET	3
#define	BRACE	4

#define MAXFILENAME 128

int main()
{
	FILE* file; /* our file pointer */

	int i; /* used in iterating our file. */

	char buf, lastChar; 
	char filename[MAXFILENAME];

	int openers[5] = {0}; /* initalize to */
	int closers[5] = {0}; /*   zero       */

	lastChar = '\0';

	/* Get filename from user input. */
	i = 0;
	while ((buf = getchar()) != '\n'){
		filename[i] = buf;
		++i;
	}
	filename[i] = '\0';

	/* Attempt to open file. */
	file = fopen(filename, "r");
	
	if( file == NULL ){
		printf("Error opening file: %s", filename);
		return 1;
	}

	while ((buf = fgetc(file)) != EOF)
	{
		switch(buf)
		{
		case '\'':
			if (lastChar != '\\')
				openers[SINGLE]++;
			break;
		case '\"':
			if (lastChar != '\\')
				openers[DOUBLE]++;
			break;
		}

		if( openers[SINGLE] % 2 == 0 && 
			openers[DOUBLE] % 2 == 0 ){
			switch(buf)
			{
			case '(':
				openers[PAREN]++;
				break;
			case ')':
				closers[PAREN]++;
				break;
			case '[':
				openers[BRACKET]++;
				break;
			case ']':
				closers[BRACKET]++;
				break;
			case '{':
				openers[BRACE]++;
				break;
			case '}':
				closers[BRACE]++;
				break;
			default:
				break;
			}
		}

		lastChar = buf;
	}

	if( openers[SINGLE] % 2 != 0 )
		printf("Warning: There are an uneven number of single-quotes.\n");
	if( openers[DOUBLE] % 2 != 0 )
		printf("Warning: There are an uneven number of double-quotes.\n");
	if( openers[PAREN] != closers[PAREN] )
		printf("Warning: There is an unmatched number of parens.\n");
	if( openers[BRACKET] != closers[BRACKET] )
		printf("Warning: There is an unmatched number of brackets.\n");
	if( openers[BRACE] != closers[BRACE] )
		printf("Warning: There is an unmatched number of curly-braces.\n");

	fclose(file);
	return 0;
}


Create a new paste based on this one


Comments: