Identifier

The program determines if a given identifier is valid or not in a variant of Pascal language, called Silly Pascal. A valid identifier must begin with a letter and must contain only letter or digits. Moreover, it must have at least one character and no more than six characters.


Source in Java

<source lang="java" line> public class Identifier {

 public boolean validateIdentifier(String s) {
   char achar;
   boolean valid_id;
   valid_id = true;
   achar = s.charAt(0);
   valid_id = valid_s(achar);
   if (s.length() > 1) {
     achar = s.charAt(1);
     int i = 1;
     while (i < s.length() - 1) {
        achar = s.charAt(i);
        if (! valid_f(achar))
           valid_id = false;
        i++;
     }
   }
   if (valid_id && (s.length() >= 1) && (s.length() <= 6))
      return true;
   else
      return false;
 }
 public boolean valid_s(char ch) {
   if (((ch >= 'A') &&
        (ch <= 'Z')) ||
       ((ch >= 'a') &&
        (ch <= 'z')))
     return true;
   else
     return false;
 }
 public boolean valid_f(char ch) {
   if (((ch >= 'A') &&
        (ch <= 'Z')) ||
       ((ch >= 'a') &&
        (ch <= 'z')) ||
       ((ch >= '0') &&
        (ch <= '9')))
     return true;
   else
     return false;
 }
 public static void main(String[] args) {
   if (args.length == 0) {
     System.out.println("Usage: identifier.Identifier [string]");
   } 
   else {
     Identifier id = new Identifier();
     if (id.validateIdentifier(args[0])) {
        System.out.println("Valid");
     } 
     else {
        System.out.println("Invalid");
     }
   }
 }

} </source>


Usage:

Command line Result
java Identifier "abcd12" Valid
java Identifier "cont*1" Invalid
java Identifier "2soma" Invalid
java Identifier "a123456" Invalid


Source in C

<source lang="c" line>

  1. include <stdio.h>
  2. include <string.h>

int valid_s(char ch) {

   if (((ch >= 'A') &&
        (ch <= 'Z')) ||
       ((ch >= 'a') &&
        (ch <= 'z')))
     return 1;
   else
     return 0;

}

int valid_f(char ch) {

   if (((ch >= 'A') &&
        (ch <= 'Z')) ||
       ((ch >= 'a') &&
        (ch <= 'z')) ||
       ((ch >= '0') &&
        (ch <= '9')))
     return 1;
   else
     return 0;

}

int valid_id(char *s) {

   char achar;
   int valid_id;
   int slen = strlen(s);
   achar = s[0];
   valid_id = valid_s(achar);
   if (slen > 1) {
     achar = s[1];
     int i = 1;
     while (i < slen - 1) {
        achar = s[i];
        if (! valid_f(achar))
           valid_id = 0;
        i++;
     }
   }
   if (valid_id && (slen >= 1) && (slen < 6))
      return 1;
   else
      return 0;

}

int main(int argc, char **argv) {

   if (argc == 1) {
     printf("Usage: %s [string]\n", argv[0]);
   } else {
     if (valid_id(argv[1])) {
        printf("%s: Valid\n", argv[1]);
     } else {
        printf("%s: Invalid\n", argv[1]);
     }
   }

} </source>


Control flow graph

<source lang="java"> public boolean validateIdentifier(String s) { char achar ; /* 01 */ boolean valid_id = false; /* 01 */ if (s.length() > 0) { /* 02 */ achar = s.charAt(0); /* 02 */ valid_id = valid_s(achar); /* 02 */ if (s.length() > 1) { /* 03 */ achar = s.charAt(1); /* 03 */ int i = 1; /* 04 */ while (i < s.length() − 1) { /* 05 */ achar = s.charAt(i); /* 05 */ if (! valid_f(achar)) /* 06 */ valid_id = false; /* 07 */ i ++; } } } /* 08 */ /* 09 */ /* 10 */ if (valid_id && (s.length() >= 1) && (s.length() < 6)) /* 11 */ return true ; else /* 12 */ return false ; } </source>

Identifier - Java - CFG.png

Definition graph

Identifier - Java - DG.png

Definition-use graph

Identifier - Java - DUG.png