/* * CDECL.Y - Translate 'C' declarations to english * Phil Budne, May 12, 1984 */ /* * The purpose of this program is to translate large obtuse * C language declarations into english. It was inspired by * the article 'TYPE SYNTAX IN THE LANGUAGE "C"; an object * lesson in syntactic innovation' by Bruce Anderson. * * While the article and the need for such a program such as this * might tend to deride the capabilities of 'C', it must be * duly noted that this program would have taken more than * the two hours it did to write it without the avaiablilty of * tools such as 'C' and 'YACC' */ %{ # include # include /* # define YYDEBUG 1 */ %} %union { char *sval; } %token ID %token '(' ')' '[' ']' '*' ';' %% start: ID dclr ';' { printf("%s\n",$1); } dclr: '*' dclr { printf("ref "); } | dclr '(' ')' { printf("proc() "); } | dclr '[' ']' { printf("array of "); } | ID { printf("%s: ",$1); } | '(' dclr ')' %% char strbuf[80]; char *strptr; extern char *malloc(); main() { puts("Input a C declarator: "); gets(strbuf); strptr = &strbuf; yyparse(); } # define NEXT c= *strptr++;if(c==0)return(0) yylex() { register char c; NEXT; while( c == ' ' || c == '\t' ) NEXT; if( isalpha(c) ) { register char *idp, *save; idp = save = malloc(20); *idp++ = c; c = *strptr++; while( c != 0 && isalnum(c) || c == '_' ) { *idp++ = c; /* store character */ c = *strptr++; /* advance pointer */ } *idp = 0; /* terminate token */ strptr--; /* backup over terminator */ yylval.sval = save; /* return address of token */ return( ID ); /* say what kind of token */ } switch( c ) { case '*': case '(': case ')': case '[': case ']': case ';': return( c ); } return( -1 ); } yyermsg(s) char *s; { printf("?%s\n",s); }