Thread: identifying string input

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    41

    identifying string input

    How can i count the number of symbols found in C-code in filex.c ??

    eg:
    Code:
    while          1
    (                 3
    )                 3
    }                1
    {                1
    ;                 2
    !=               1
    ++              1
    identifiers   8  (c, getchar, EOF, sentence, i, etc)
    =                3  
    [                 2
    ]                 2
    char const  1  ('\0')
    Code:
    //filex.c
    
    	while(( c = getchar()) != EOF){
    		   sentence[i++] = c;
    	}
    	sentence[i] = '\0';
    With the code bellow, i have split the c-code into tokens which will be


    Code:
    while((
    c
    =
    getchar())
    !=
    EOF){
    sentence[i++]
    =
    c;
    }
    sentence[i]
    =
    '\0';
    From here how can i go further to identify what is the content of the token ??

    Code:
    		fscanf(cfPtr, "%s", xyz);
    		while(!feof(cfPtr)){
    	        processinfo(xyz);
    			fscanf(cfPtr, "%s", xyz);
    
    			}
    		fclose(cfPtr);
    		}

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    127
    What you want to do is called lexical analysis. To do it properly you should be able to recognize the difference between identifiers, keywords, operators, separators, comments and constants. Because most of these rarely change, you can create lookup tables for most of them. The only difficult part about this is that some tokens are build using other tokens, yet do not perform the same function. So your lexical analyzer must support the "maximal munch" feature that C compilers use. That basically means that you should accept the longest valid token before moving on to the next one. All of this is easiest with single character input and careful calls to ungetc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Identifying string input
    By winsonlee in forum C Programming
    Replies: 1
    Last Post: 04-23-2004, 09:02 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Basic C Programming Help Needed
    By Smurphygirlnz in forum C Programming
    Replies: 8
    Last Post: 09-26-2002, 07:12 PM