Cpp syntax detector

rhoit 0 Tallied Votes 365 Views Share

i don't know how it happen but the exe formed after compilation was 1.23 MB which has never happen before.

/*
  Title: Detect 5R
  Version: Test Mode
  Started Date  : 19 Jan 2009
  Last Update   : 30 Jan 2009
  Revised: 4
*/

#include <iostream>
#include <windows.h>
#include <conio.h>

using namespace std;

HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
FILE *fp;

#define TxtAttrib(x) SetConsoleTextAttribute(hConsole, x)
#define DefTxt SetConsoleTextAttribute(hConsole,15)

#define Marker(x) {\
                    TxtAttrib(10);\
                    cout<<(char)x;\
                    DefTxt;\
                  }                   

#define buffer_pp {\
                    buffer=fgetc(fp);\
                    if(buffer==EOF) return EOF;\
                  }

/*
  Color Attribute = foreground(%d) + background(%d) * 16
  0 = Black         1 = Blue          2 = Green         3 = Aqua
  4 = Red           5 = Purple        6 = Yellow        7 = Gray
  8 = Dark Gray     9 = Brit Blue     A = Brit Green    B = Brit Aqua
  C = Brit Red      D = Brit Purple   E = Brit Yellow   F = Brit White
*/

#define Color_Preprocess 2
#define Color_Comment 6
#define Color_Char 4
#define Color_String 12
#define Color_Numeric 5
#define Color_Datatypes 10
#define Color_Conditional 14
#define Color_Function 11
#define Color_Loop 15+9*16

int run_check1=0, run_check2=0, keyword_filter=0, keyword_found=0, function_found=0, function_flag=0;

int read_file();
int check1();
int check2(int);

int main(int argc, char *argv[])
{
    if(argc>1) fp=fopen(argv[1], "r"); else fp=fopen("Detect5R2.cpp","r");
    if(!fp){ cout<<"WARNING: A file doesn't exist.\n"; exit(0); }
    
    system("color 0f");
    read_file();
   
    printf("\n\"#\" check1: %4d check2: %4d\
 keyword filter: %4d keyword found: %4d\
 function found: %4d\
 \n", run_check1, run_check2, keyword_filter, keyword_found, function_found);
    
    system("pause");
    return 0;
}

void readtill(int *buffer, int code)
{
     cout<<(char)*buffer;
     for(*buffer=fgetc(fp); *buffer!=EOF && *buffer!=code; *buffer=fgetc(fp))
         cout<<(char)*buffer;     
}

int i, buffer; char word[20];
int compare(char word[])
{ 
    keyword_filter++;
    switch(i)
    {
             case 2:   if(strcmp(word, "if")==0) return 'c';
                  else if(strcmp(word, "do")==0) return 'l';
                  break;
             
             case 3:   if(strcmp(word, "for")==0) return 'l';
                  else if(strcmp(word, "int")==0) return 'd';     
                  break;
                  
             case 4:   if(strcmp(word, "case")==0) return 'c';
                  else if(strcmp(word, "char")==0) return 'd';      
                  else if(strcmp(word, "else")==0) return 'c';
                  else if(strcmp(word, "void")==0) return 'd'; 
                  else if(strcmp(word, "goto")==0) return 'k';
                  break;
             
             case 5:   if(strcmp(word, "while")==0) return 'l';
                  else if(strcmp(word, "using")==0) return 'k';
                  else if(strcmp(word, "break")==0) return 'k';
                  else if(strcmp(word, "const")==0) return 'k';
                  break;
                  
             case 6:   if(strcmp(word, "switch")==0) return 'l';
                  break;
             
             case 8:   if(strcmp(word, "continue")==0) return 'k';
    }
    //Marker(175); 
    return 0;
}

int check2(int flag_keyword)
{    
    run_check2++;  
    i=0;
    
    for(;;)
    {
         if((buffer>='A' && buffer<='Z') || (buffer>='0' && buffer<='9') || (buffer=='_') ) flag_keyword=0;
         else if(!(buffer>='a' && buffer<='z')) break;                 
         word[i++]=buffer;
         buffer_pp;
    }
    word[i]='\0';
    
    if(flag_keyword==1) switch(compare(word))
    {
         case 'l': TxtAttrib(Color_Loop); i=-'l'; break;
         case 'c': TxtAttrib(Color_Conditional); i=-'c'; break;
         case 'd': TxtAttrib(Color_Datatypes); i=-'d'; break;
         case 'k': TxtAttrib(13); i=-'k'; break;
         case 0: goto function;
    }
    else
    {    
function:
         //Marker(173);
         if(buffer=='(')
              {
                   function_found++; function_flag+=2;
                   TxtAttrib(Color_Function);
                   cout<<word<<'(';
                   DefTxt;
              until:
                   buffer_pp;              
                   if(check1()==')' && function_flag>0)
                   {
                          function_flag--;
                          TxtAttrib(Color_Function);
                          cout<<"\b)";
                          DefTxt;
                   }
                   else
                   {
                       if(!((buffer>='0' && buffer<='9') ||
                             buffer==',' ||
                             buffer=='*' ||
                             buffer=='[' || buffer==']'
                            )
                         ) //Quick Patch
                         cout<<(char)buffer;
                       goto until;
                   }
                   buffer_pp;
                   return 1;
               }
     }

    cout<<word;
    
    //if(i>1) Marker(174);
    
    if(i<0) { keyword_found++; DefTxt;} //change colour only when needed
    return 0;    
}
int stat;
int check1()
{   
    run_check1++;
    if(buffer>='0' && buffer<='9')
    {
         TxtAttrib(Color_Numeric);
         cout<<(char)buffer;
         DefTxt;
         return buffer;
    }
    
    if((buffer>='A' && buffer<='Z') || buffer=='_') if(check2(0)==EOF) return EOF;
    if(buffer>='a' && buffer<='z') if(stat=check2(1)==EOF) return EOF;  

    switch(buffer)
    {
         case '/': //what about the line extention using escape sequence
              buffer_pp;
              if(buffer=='/' || buffer == '*')
              {
                   TxtAttrib(Color_Comment);
                   cout<<'/';
                   if(buffer=='/') readtill(&buffer, '\n');
                   else
                   {
rep:
                       readtill(&buffer, '*');
                       cout<<(char)buffer;
                       buffer_pp;
                       if(buffer!='/') goto rep;
                   }
                   cout<<(char)buffer;
                   DefTxt;
              }                       
              break;
         
         case '#':
              TxtAttrib(Color_Preprocess);
              cout<<'#';
              for(buffer=fgetc(fp); buffer!=EOF && buffer!='\n'; buffer=fgetc(fp))
              {
                   cout<<(char)buffer;
                   if(buffer=='\\')
                   {
                        buffer=fgetc(fp); //bypassing the '\n' as per Dev-C++ 4.9.9.2 IDE
                        cout<<(char)buffer;
                        continue;
                   }
              }
              cout<<(char)buffer;
              DefTxt;              
              break;
                       
         case '"':
              TxtAttrib(Color_String);
              cout<<'"';
              for(buffer=fgetc(fp); buffer!=EOF && buffer!='\n' && buffer!='"'; buffer=fgetc(fp))
              {
                   cout<<(char)buffer;
                   if(buffer=='\\')
                   {
                        buffer=fgetc(fp); //bypassing the '\n' as per Dev-C++ 4.9.9.2 IDE
                        cout<<(char)buffer;
                        continue;
                   }
              }
              cout<<(char)buffer;
              DefTxt;
              break;
         
         case 39: //char '
              TxtAttrib(Color_Char);
              do{ cout<<(char)buffer; buffer_pp; }while(buffer!=39);
              cout<<(char)39;
              DefTxt;
              break;
         
         case ' ': 
spacekill:
              for(;;) //space kill
              {
                   if(buffer==' ') cout<<' ';//(char)250;
                   else return check1();
                   buffer_pp;
              }
              break;
         
         case '\n': //cout<<(char)20;               
         case ';':
              cout<<(char)buffer;
              buffer_pp;
              goto spacekill;
         
         case '(': case ')':
              if(stat==0) cout<<(char)buffer;
              break;
         
         default: cout<<(char)buffer;         
    }
    return buffer;
} 

int read_file()
{
    for(;;)
    {
         buffer_pp; if(check1()==EOF) {cout<<"EOF"; return EOF;}
    }         
}
William Hemsworth 1,339 Posting Virtuoso

Nice snippet, works perfectly. The only thing I could complain about (other than the formatting) is the fact that you included conio.h.

rhoit 0 Junior Poster in Training

Wrote for linux! for C
https://github.com/rhoit/cyntax

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.