cdcl (1.1) - Java applet to unscramble C declarations

This is a java applet which can unscramble C declarations. The code to parse C declaration is translated from the C code described in K&R, 2nd edition, page 123.

Here are some C declarations from K&R, page 122. Try without the applet first and see if you can unscramble them.

Examples:

    int *f()
    int (*f)()
    char **argv
    int (*daytab)[13]
    void *comp()
    void (*comp)()
    char (*(*x())[])()
    char (*(*x[3])())[5]


Note: It can not handle declarations with function argument types or qualifiers like const, volatile etc. For Example: it can not handle declaration like: char * const (a[10]) (int **b) Therefore, you have to rewrite the declaration to char *(a[10]) ()

Rules for unscrambling

  1. Go to the leftmost identifier and say:
            identifier is
    
  2. Look at left and right side of the identifier and apply the rules of precedence:
          ( )  => [ ] => *
    
    Note: ( ) has the highest precedence and * has the lowest precedence. ( ) and [ ] are used as postfix operators and * is used as prefix operator.

    if you see ( ), say:

        function returning
    
    if you see [ ], say:
        array of
    
    if you see *, say:
          pointer to
    

Example

Here is an example of the above rules. Let unscramble it:
    char (*(*x[3])())[5]
  1. Go to the leftmost identifier x and we say:
        x is
    
  2. We look at the left and right side of x and see * is at the left side and [] is at the right side. As [] binds tighter than *, we say:
        array [0..2] of pointer to
    
    Now, we see *x[3] is surrounded by () and * is at the left and () is at the right side. As () has higher precedence than *, we say:
        function returning pointer to
    
    Now, we look around again and see, outside the parens, char datatype is at the left and [] is at the right side. So, we say:
        array [0..4] of char
    
    And we'r done. The result is:

    x is array [0..2] of pointer to function returning pointer to array [0..4] of char

Copyright

This applet is in the public domain. Do anything you like with it. It would be nice but not required if you give me the credit for it.If you are using this applet, please let me know. Bug reports, suggestions are always welcome!

Here is is the: source
The compiled class/source as gzipped tar archive is: here.


Gamelan thinks, the applet is cool


Muhammad A Muquit
Research Computing Services (RCS)
The Fox Chase Cancer Cancer
email: MA_Muquit@fccc.edu

Initial release: Aug-09-96