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]
- Go to the leftmost identifier and say:
identifier is- 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 returningif you see [ ], say:
array ofif you see *, say:pointer to
Here is an example of the above rules. Let unscramble it:char (*(*x[3])())[5]
- Go to the leftmost identifier x and we say:
x is- 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 toNow, 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 toNow, 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 charAnd we'r done. The result is:x is array [0..2] of pointer to function returning pointer to array [0..4] of char
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.
Muhammad A Muquit
Research Computing Services (RCS)
The Fox Chase Cancer Cancer
email:
MA_Muquit@fccc.edu
Initial release: Aug-09-96