(mm) Java applet to unscramble C declaration
Home | Software | Count
  ansi xterm
  web counter
  bangla font
MS Windows
c declaration unscrambler
Muhammad A Muquit

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.


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

Also look at the JavaScript version of cdcl

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


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

Nice and simple, isn't it?


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!


Java Source
File: cdcl.java
Size: 9359 bytes
MD5 Checksum: 07149ff78e9f4a9186062bca76f8d2b2
Last updated: ?

Compiled class/source
File: cdcl1.1.tar.gz
Size: 7907 bytes
MD5 Checksum: b775d5d6e076acac3b2c2f9653b5b76e
Last updated: ?

Initial release: Aug-09-1996

URL of this page: http://www.muquit.com/muquit/software/cdcl/cdcl.html

back Page updated: Tue Feb 25 19:50:46 2014 GMT   Copyright © 2014 muquit@muquit.com.