First, the purpose of the experiment:
The use of C language compiled recursive descent parsing procedures, and simple language parsing.
The preparation of a recursive descent parser, syntax checking and realize the structure of the word lexical analysis program provided by sequence analysis.
Second, the experimental principle
Each nonterminal symbol corresponds to a subroutine.
This subroutine is determined according to a next input symbol (SELECT sets) which are processed in a production, then the right end of the production according to:
- Each encounter a terminator, it is determined whether the current read word matches the terminator, if matched, then the next word is read to continue the analysis; do not match, an error process is performed
- Each encounter a nonterminal, the appropriate subroutine is called
Third, the experiment asked for clarification
Enter the word string to "#" end if the grammar is correct sentences, the output success information, print "success", otherwise output "error", and pointed out grammatical errors of the type and location.
E.g:
Input begin a: = 9; x: = 2 * 3; b: = a + x end #
Output success
Input x: = a + b * c end #
Output 'end' error
Fourth, the experimental procedures
1. Grammar of the language to be analyzed (refer to P90)
2. Grammar to be expressed, at least comprising
- Statement
-condition
-expression
3. eliminate left recursion
4. Extract the left common factor
5. SELECT set computing
6. LL (1) grammar is determined
7. recursive descent parser
achieve:
#include<stdio.h> #include<string.h> #include<stdlib.h> char wsym[80],ssym[80]; char ch; int row,syn,sum,m,i,p; char *word[6]={"begin","if","then","while","do","end"}; int flag=0; void E(); void T(); void E1(); void T1(); void F(); void getsyn(){ for(i=0;i<8;i++) ssym [i] = NULL; ch=wsym[p++]; while(ch==' '){ ch=wsym[p]; p++; } if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){ m=0; while((ch>='0'&&ch<='9')||(ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){ ssym[m++]=ch; ch=wsym[p++]; } ssym[m++]='\0'; p--; syn=10; for(i=0;i<6;i++){ if(strcmp(word[i],ssym)==0){ syn=i+1; break; } } }else if(ch>='0'&&ch<='9'){ sum=0; while(ch>='0'&&ch<='9'){ sum=sum*10+(ch-'0'); ch=wsym[p++]; } p--; syn=11; }else switch(ch){ case '+': ssym[0]=ch;syn=13; break; case '-': ssym[0]=ch;syn=14; break; case '*': ssym[0]=ch;syn=15; break; case '/': ssym[0]=ch;syn=16; break; case ':': i=0; ssym[i++]=ch; ch=wsym[p++]; if(ch=='='){ ssym[i++]=ch; syn=18; }else{ syn=17; p--; } break; case '<': i=0; ssym[i++]=ch; ch=wsym[p++]; if(ch=='='){ ssym[i++]=ch; syn=21; }else if(ch=='>'){ ssym[i++]=ch; syn=22; }else{ syn=20; p--; } break; case '>': i=0; ssym[i++]=ch; ch=wsym[p++]; if(ch=='='){ ssym[i++]=ch; syn=24; }else{ syn=23; p--; } break; case '=': ssym[0]=ch;syn=25; break; case ';': ssym[0]=ch;syn=26; break; case '(': ssym[0]=ch;syn=27; break; case ')': ssym[0]=ch;syn=28; break; case '#': ssym[0]=ch;syn=0; break; case '\n': syn=100; break; default: syn=-1; break; } } void E(){ if(syn==1){ getsyn (); T(); while(syn==26){ getsyn (); T(); } if(syn==6){ getsyn (); if(syn==0 && flag==0){ printf("success!\n"); } } else{ printf ( " syntax error, missing End \ the n-! " ); } }else{ printf ( " syntax error, missing the begin \ the n-! " ); flag=1; } return ; } void T(){ if(syn==10){ getsyn (); if(syn==18){ getsyn (); E1 (); }else{ printf ( " expression syntax error with error:!% S \ the n- " , ssym); flag=1; } }else{ if(flag!=1){ printf ( " expression syntax error, the error is:% S \ the n- " , ssym); flag=1; } } } void E1(){ T1 (); while(syn==13||syn==14){ getsyn (); T1 (); } return ; } void T1(){ F(); while(syn==15||syn==16){ getsyn (); F(); } return ; } void F(){ if(syn==10||syn==11){ getsyn (); }else if(syn==27){ getsyn (); E1 (); if(syn==28) getsyn (); else{ printf ( " ! no ')', a syntax error \ the n- " ); flag=1; } } else{ printf ( " expression syntax error with error:!% S \ the n- " , ssym); flag=1; } return ; } int main () { p=0; printf ( " Please enter paragraph statement: " ); do { ch=getchar(); wsym[p++]=ch; }while(ch!='#'); p=0; getsyn (); E(); printf ( " Analysis of the end! " ); getchar(); }
operation result: