Compiling principle Experiment 1 The structure of lexical analysis program

Source: Internet
Author: User
Tags int size strlen
"Experimental Purpose"Practice the method of constructing lexical analysis program, deepen the comprehension of classroom teaching and improve the practical ability of lexical analysis method. "Experimental Requirements"Constructing lexical analysis programs by using languages such as C + + "Specific Requirements"The lexical analyzer is able to recognize the state transition diagram of real numbers recognition
where, D ={0,1,2,3,4,5,6,7,8,9}. F stands for ±. "Source program"
/* Experiment Name: Experiment 1 The structure of the lexical analysis program number: Name: niu91 (859222829) class: */#include <stdio.h> #include <malloc.h> #include <str
Ing.h> #define N 65535/* symbol table f1[] positive and negative group d[] integer array and ' E ' and '. '/static char f1[2]={' + ', '-'};
static char d[10]={' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '};
	/* The Function list */* Determines whether the character returns a value of 1 in the symbol table: Sign 2:e 3:.

4: Number 0: Lookup failed */int isbelong (char);

/* Match String */void matching (char*);

/* Determine if the return value is an integer: 1: Yes 0: no */int isinteger (int,char*,int);

/* Determine if decimal */int isdecimal (int,char*,int);

/* Determine if index */int isexponent (int,char*,int);
	void Main () {char *s= (char*) malloc (n*sizeof (char));
	The int flag=0;//glyph for 0 matches the failure to 1 to match success int n;
	printf ("Please enter the number of tests:");
	scanf ("%d", &n);
	GetChar ();//Eat the carriage return symbol, otherwise the carriage return will be stored in S.
		while (n) {printf ("Please enter test data:");
		Gets (s);
		S[strlen (s) +1]= ' + ';//Add symbol end sign matching (s);
	--n;
	} free (s);//Release The dynamic space pointed to by the pointer s} int isbelong (char ch) {int i;
	if (ch== ' + ' | | ch== '-') {return 1;
	}else if (ch== ' E ') {return 2;
	}else if (ch== '. ')
	{return 3; }else{for (i=0;i<10;i++) {if (Ch==d[i]) return 4;
	} return 0;
	}} void matching (char* s) {int Size=strlen (s)-1;
	int flag=0;
		First determine if the first character is a F1 or a number if (Isbelong (s[0)) ==1)//f1 {printf ("%c", S[0]); Matching (s+1);///Recursive}else if (Isbelong (S[0]) ==4) {//d//real number has a character greater than one zero at the beginning, as illegal if (size>=2 && s[0]== ' 0 ' &&am P
		s[1]!= '. ') {printf ("%s" is not a real number.)
		\ n ", s); }else if (Isinteger (1,s,size)) {printf ("%s" is an integer.)
		\ n ", s); }else if (Isdecimal (0,s,size)) {printf ("%s" is a decimal number.)
		\ n ", s); }else if (isexponent (0,s,size)) {printf ("%s" is an exponent.)
		\ n ", s); }else {printf ("%s" is not a real number.)
		\ n ", s); }}else{printf ("%s" is not a real number.)
	\ n ", s);
	}} int Isinteger (int start,char* s,int size) {int i;
	int flag=0;
		for (i=start;i<=size;i++) {if (Isbelong (S[i]) ==4) {flag=1;
		}else {return 0;
}} return flag;
	} int isdecimal (int start,char* s,int size) {int i;

	int flag=0; for (i=start;i<=size-2;i++) {if (Isinteger (start,s,i) && Isbelong (s[i+1]) ==3 && isintEger (i+2,s,size)) {flag=1;
}} return flag;
	} int isexponent (int start,char* s,int size) {int i;

	int flag=0; for (i=start+2;i<=size-3;i++) {if (Isdecimal (start,s,i) && Isbelong (s[i+1]) ==2 &&isbelong (s[i+2])
		==1 && Isinteger (i+3,s,size)) {flag=1;
}} return flag; }

Experimental Results

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.