So, i have this program for correcting syntax errors written in C, from the K&R book.
Here is the code below.
My question is, how can i modify this code so that the program does this:
1. I enter the program in console mode - after that i write in the name of the C project which i want to check for syntactic errors.
2. Then, the program puts characters in an array and checks for syntactic errors (the part of the code i dont have to change).
Im using a Dev-C++ compiler and sadly a Windows Vista OS.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
/*
Write a program to check a C program for rudimentary syntax errors
like unbalanced parentheses, brackets and braces. Don't forget about quotes,
both single and double, escape sequences, and comments. (This program is hard
if you do it in full generality.)
*/
enum { normalno, navodnici1, navodnici2, komentar };
char array[100];
int a=0;
int x=0;
int state=normalno;
int pom=0;
int okrugle;
int uglate;
int viticaste;
okrugle=uglate=viticaste=0;
int single_quote, double_quote, comment;
single_quote = double_quote = comment=0;
int pom5=0;
enum {........, non_comment};
int stanje;
stanje=non_comment;
while((a=getchar())!=EOF)
{
array[x]=a;
x++;
}
array[x]='\n';
x++;
array[x]='\0';
pom=x;
for(x=0; x<pom; x++)
{
if(array[x]=='/' && array[x+1]=='*')
{
state=komentar;
comment++;
}
else if(array[x]=='*' && array[x+1]=='/')
{
state=normalno;
comment--;
}
else if(state==navodnici1 && array[x]=='"' && state!=komentar)
{
state=normalno;
}
else if(array[x]=='"' && state!=komentar)
{
state=navodnici1;
}
else if(state==navodnici2 && array[x]=='\'' && state!=komentar)
{
state=normalno;
}
else if(array[x]=='\'' && state!=komentar)
{
state=navodnici2;
}
if(array[x]=='"' && stanje==comment)
{
double_quote--;
stanje=non_comment;
}
if(array[x]=='"' && stanje==non_comment)
{
double_quote++;
stanje=comment;
}
if(state==normalno)
{
if(array[x]=='(')
okrugle++;
if(array[x]==')')
okrugle--;
if(array[x]=='[')
uglate++;
if(array[x]==']')
uglate--;
if(array[x]=='{')
viticaste++;
if(array[x]=='}')
viticaste--;
}
}
printf("%s", array);
if(okrugle==0 && uglate==0 && viticaste==0)
{
printf("Nema sintaktickih gresaka.\n");
}
if(okrugle>0)
printf("Okrugle zagrade se ne poklapaju.\n");
if(uglate>0)
printf("Uglate zagrade se ne poklapaju.\n");
if(viticaste>0)
printf("Viticaste zagrade se ne poklapaju.\n");
if(comment>0)
printf("Komentari se ne poklapaju.\n");
if(double_quote>0)
printf("Dvostruki navodni znakove se ne poklapaju");
system("PAUSE");
return 0;
}