home *** CD-ROM | disk | FTP | other *** search
- # Decipher C Declarator
- # declarator syntax (The C Programming Language, 2cd Edition, page 215)
- #
- # Copyright (C) 1990 Terry D. Boldt, All Rights Reserved
- #
- # declarator:
- # pointer direct-declarator
- #
- # direct-declarator:
- # identifier
- # ( declarator )
- # direct-declarator [ constant-expression ]
- # direct-declarator ( parameter-type-list )
- # direct-declarator ( identifier-list )
- #
- # pointer:
- # * type-qualifier-list
- # * type-qualifier-list pointer
- #
- # type-qualifier-list:
- # type-qualifier
- # type-qualifier-list type-qualifier
- #
- BEGIN {
- # define error reporting file name
- stderr = "stderr";
- #
- # parenthesis are necessary in following regular expressions
- # so that they may be safely invoked in other regular expressions
- #
- # define regular expression for names
- identifier = /([A-Za-z_][A-Za-z0-9_]*)/;
- # define regular expression for function parenthesis
- parens = /(\(\))/;
- # define regular expression for array brackets
- # with optional array size
- brackets = /(\[{_d}*\])/;
- # define regular expression for plain variable names
- var_identifier = /^{identifier}/;
- # define regular expression for function declarations
- function_identifier = /^{identifier}?{parens}+/;
- # define regular expression for array declarations
- array_identifier = /^{identifier}?{brackets}+/;
- # define regular expression for pointer declarations
- ptr_identifier = /^\*+{identifier}?/;
- # define regular expression for leading parenthesis
- # in parenthesized exp.
- # use look-ahead to ensure that ')' NOT immediately following
- ldg_paren = /^\(@[!\)]/;
- # define pointers
- ldg_pointer = /^\*+/;
-
- Quit = /^{_w}*[Qq](uit)?{_w}*$/;
-
- fprintf(stderr,"Decipher C Declarator.\n");
- fprintf(stderr,"Copyright (C) 1990 Terry D. Boldt, All Rights Reserved.\n");
- fprintf(stderr,"\nEnter C Declaration: (or quit)\n");
- }
-
- Quit { exit 1; }
-
- # examine each input line
- {
- # use for optionally tracing execution of user defined functions or switch
- # statements
- # TRACE = /_u$|^s/;
- token = $1;
- f = 2;
- type = identifier_token = decl_list = "";
- storage_class;
- data_type; # find data type
- decl; # find declarations
- if ( token != ";" && token != "" ) {
- fprintf(stderr,"Error, Unrecognized Symbol : %s\n",token);
- exit 1;
- }
- print identifier_token,":",decl_list,type; # print declations
- fprintf(stderr,"\nEnter C Declaration: (or quit)\n");
- }
-
- # function to recognize declarations
- function decl() {
- local ns;
-
- if ( token ~~ ldg_pointer ) {
- ns = MLENGTH;
- s_token(MLENGTH);
- } else ns = 0;
- type_qualifier;
- dir_decl;
- while ( ns-- ) decl_list = decl_list " pointer to";
- }
-
- # function to recognize direct declarations
- function dir_decl() {
- local tmp_str;
- local loop = TRUE;
-
- while ( loop ) {
- switch ( token ) {
- case function_identifier: # find function identifier tokens
- if ( match(token,var_identifier) ) {
- identifier_token = substr(token,1,RLENGTH);
- s_token(RLENGTH);
- }
- if ( match(token,/^{parens}+/) ) {
- tmp_str = substr(token,1,RLENGTH);
- s_token(RLENGTH);
- }
- gsub(parens," function returning",tmp_str); # substitute for parens
- decl_list = decl_list tmp_str;
- break;
- case array_identifier: # find array identifier tokens
- if ( match(token,var_identifier) ) {
- identifier_token = substr(token,1,RLENGTH);
- s_token(RLENGTH);
- }
- if ( match(token,/^{brackets}+/) ) {
- tmp_str = substr(token,1,RLENGTH);
- s_token(RLENGTH);
- }
- gsub(brackets," array $$0 of",tmp_str); # substitute for arrays
- gsub(/[\[\]]/,"",tmp_str); # remove '[' & ']'
- decl_list = decl_list tmp_str;
- break;
- case ldg_paren: # find tokens starting with '('
- s_token(1);
- decl;
- if ( token !~ /^\)/ ) { fprintf(stderr,"Error: Missing ')'\nToken: %s.\n",token); exit 2; }
- s_token(1);
- break;
- case var_identifier: # find identifier tokens
- identifier_token = substr(token,1,CLENGTH);
- s_token(CLENGTH);
- break;
- default:
- loop = FALSE;
- break;
- }
- }
- }
-
- # function recognize storage class
- function storage_class() {
- local loop = TRUE;
-
- while ( loop ) {
- switch ( token ) {
- case "auto":
- type = type " automatic";
- s_token(0);
- break;
- case "extern":
- type = type " external";
- s_token(0);
- break;
- case "register":
- type = type " register";
- s_token(0);
- break;
- case "typedef":
- type = type " type definition: ";
- s_token(0);
- break;
- case "static":
- type = type "static";
- s_token(0);
- break;
- default:
- loop = FALSE;
- break;
- }
- }
- }
-
- function type_qualifier() {
- local loop = TRUE;
-
- while ( loop ) {
- switch ( token ) {
- case "const":
- decl_list = decl_list " constant";
- s_token(0);
- break;
- case "volatile":
- decl_list = decl_list " volatile";
- s_token(0);
- break;
- default:
- loop = FALSE;
- break;
- }
- }
- }
-
- function data_type() {
- local loop = TRUE;
-
- while ( loop ) {
- switch ( token ) {
- case "char":
- type = type " character";
- s_token(0);
- break;
- case "double":
- type = type " double";
- s_token(0);
- break;
- case "float":
- type = type " float";
- s_token(0);
- break;
- case "int":
- type = type " integer";
- s_token(0);
- break;
- case "long":
- type = type " long";
- s_token(0);
- break;
- case "short":
- type = type " short";
- s_token(0);
- break;
- case "signed":
- type = type " signed";
- s_token(0);
- break;
- case "unsigned":
- type = type " unsigned";
- s_token(0);
- break;
- case "void":
- type = type " void";
- s_token(0);
- break;
- case "const":
- type = type " constant";
- s_token(0);
- break;
- case "volatile":
- type = type " volatile";
- s_token(0);
- break;
- default:
- loop = FALSE;
- break;
- }
- }
- }
-
- # function to shave characters from front of token string
- function s_token(cnt) {
-
- if ( cnt == 0 || cnt == length(token) ) token = $(f++);
- else token = substr(token,cnt+1);
- }
-