Translate the declaration of C language into popular language--cdecl

Source: Internet
Author: User
Tags stdin strcmp volatile

"c Expert Programming" in a program, you can translate the declaration into a popular language, the main use of stacks, very difficult, I am not able to write, according to the book knocked again. Normal method:

#include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> #define Maxtokens

#define Maxtokenlen the enum Type_tag {identifier,qualifier,type};
	struct token {char type;
Char String[maxtokenlen];

};
int top =-1;
struct token stack[maxtokens];

struct token now;
	#define POP stack[top--] #define PUSH (s) stack[++top]=s enum Type_tag classify_string (void) {char *s = now.string;
		if (!strcmp (S, "const")) {strcmp (S, "read-only");
	return QUALIFIER;
	} if (!strcmp (S, "volatile")) return QUALIFIER;
	if (!strcmp (S, "Void")) return TYPE;
	if (!strcmp (S, "char")) return TYPE;
	if (!strcmp (S, "signed")) return TYPE;
	if (!strcmp (S, "unsigned")) return TYPE;
	if (!strcmp (s, "int")) return TYPE;
	if (!strcmp (S, "long") return TYPE;
	if (!strcmp (S, "float")) return TYPE;
	if (!strcmp (S, "double")) return TYPE;
	if (!strcmp (s, "struct")) return TYPE;
	if (!strcmp (S, "union")) return TYPE;
	if (!strcmp (S, "enum")) return TYPE; Return IDENTIFIER;
	} void GetToken (void)//reads the next tag to now {char *p = now.string;
		while ((*p = GetChar ()) = = ");//skip whitespace character if (Isalnum (*p)) {while isalnum (*++p = GetChar ());
		UNGETC (*p,stdin)//will not match the character returned to the input stream *p = ' ";
		Now.type = Classify_string ();
	Return
		} if (*p = = ' * ') {strcpy (now.string, "Pointer to");
		Now.type = ' * ';
	Return
	} now.string[1] = '; '//single character mark Now.type = *p;
Return
	} void Read_to_first_identifer () {GetToken ();
		while (Now.type!= IDENTIFIER)//Find identifier {push (now);
	GetToken ();
	printf ("%s is", now.string);
	GetToken ();
Return
		} void Deal_with_arrays () {while (Now.type = = =] {printf ("array");
			GetToken ()//number or '] ' if (isdigit (now.string[0)) {printf ("0..%d", Atoi (now.string)-1);
	GetToken ();//read '] '} gettoken ();//read '] ' after another mark printf ("of");
	} void Deal_with_function_args () {while (Now.type!= ') ') GetToken ();
	GetToken ();
printf ("function returing");}
		void Deal_with_pointers () {while (Stack[top].type = = ' * ')printf ("%s", pop.string);}
	void Deal_with_declacrator ()//function {switch (now.type) {case ' [':d eal_with_arrays () ') that may exist after processing an identifier;
	Case ' (':d Eal_with_function_args ();
	} deal_with_pointers ();
			while (top >= 0)//processes the symbol {if (Stack[top].type = = ' (') {pop] that is pressed into the stack before reading the identifier;
		Symbol Deal_with_declacrator () after GetToken ();//read ') ';
	else printf ("%s", pop.string);
	int main () {Read_to_first_identifer ()};
	Deal_with_declacrator ();
	printf ("\ n");
return 0; }


finite automata: finite automaton representations are a computational model that is abstracted for the purpose of studying the computational process of finite memory and some language classes. Finite state automata have a finite number of states, each state can migrate to 0 or more states, and input strings determine which state to perform. A finite state automaton can be represented as a forward graph.

#include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> #define Maxtokens

#define Maxtokenlen the enum Type_tag {IDENTIFIER, QUALIFIER, type};
	struct token {char type;
Char String[maxtokenlen];

};
int top =-1;
struct token stack[maxtokens];

struct token now;
	#define POP stack[top--] #define PUSH (s) stack[++top]=s enum Type_tag classify_string (void) {char *s = now.string;
		if (!strcmp (S, "const")) {strcmp (S, "read-only");
	return QUALIFIER;
	} if (!strcmp (S, "volatile")) return QUALIFIER;
	if (!strcmp (S, "Void")) return TYPE;
	if (!strcmp (S, "char")) return TYPE;
	if (!strcmp (S, "signed")) return TYPE;
	if (!strcmp (S, "unsigned")) return TYPE;
	if (!strcmp (s, "int")) return TYPE;
	if (!strcmp (S, "long") return TYPE;
	if (!strcmp (S, "float")) return TYPE;
	if (!strcmp (S, "double")) return TYPE;
	if (!strcmp (s, "struct")) return TYPE;
	if (!strcmp (S, "union")) return TYPE;
	if (!strcmp (S, "enum")) return TYPE; Return IdentiFier;
	} void GetToken (void)//reads the next tag to now {char *p = now.string;
		while ((*p = GetChar ()) = = ");//skip whitespace character if (Isalnum (*p)) {while isalnum (*++p = GetChar ());
		Ungetc (*p, stdin);//return the inconsistent characters to the input stream *p = ';
		Now.type = Classify_string ();
	Return
		} if (*p = = ' * ') {strcpy (now.string, "Pointer to");
		Now.type = ' * ';
	Return
	} now.string[1] = '; '//single character mark Now.type = *p;
Return
} void Initialize (), Get_array (), Get_params (), Get_lparen (), Get_ptr_part (), Get_type ();
	void (*nextstate) (void) = initialize;//set up a function pointer that states int main () {while (Nextstate!= NULL) (*nextstate) ();
return 0;
	} void Initialize ()//takes the leftmost identifier {GetToken ();
		while (Now.type!= IDENTIFIER) {push (now);
	GetToken ();
	printf ("%s is", now.string);
	GetToken ();
Nextstate = Get_array;
	void Get_array ()//view the next symbol of the identifier, if it is the square bracket {nextstate = Get_params;
		while (Now.type = = ' [') {printf ("array"); GetToken ()//A number or '] ' if (isdigit (now.string[0)) {printf ("0..%d", Atoi (now.string)-1);
		GetToken ()//read '] '} gettoken ();
		printf ("of");
	Nextstate = Get_lparen;
	} void Get_params ()//If it is a left bracket representing {nextstate = Get_lparen; The content representing the IF (Now.type = = ' (') {while (Now.type!= ') ')//to the right parenthesis returns ...
		function of GetToken ();
		GetToken ();
	printf ("function returning");
	} void Get_lparen ()//if the symbol on the left is an opening parenthesis {nextstate = Get_ptr_part;
			if (top >= 0) {if (Stack[top].type = = ' (') {pop;
		GetToken (),//In ') ' after reading nextstate = Get_array;
	} void Get_ptr_part ()//if the symbol on the left is one of the following: const,volatile,* {nextstate = Get_type;
		if (Stack[top].type = = ' * ') {printf ("pointer to");
		Pop
	Nextstate = Get_lparen;
		else if (Stack[top].type = = QUALIFIER) {printf ("%s", pop.string);
	Nextstate = Get_lparen;
	} void Get_type ()//The remaining symbol forms the base type of the declaration {nextstate = NULL;
	while (top >= 0) {printf ("%s", pop.string);
printf ("\ n");
 }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.