2019-11-27 Job

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

  1 #include "string.h"
  2 #include <stdio.h>
  3 #include<conio.h>
  4 
  5 void lrparser();
  6 void yucu();
  7 void statement();
  8 void expression();
  9 void term();
 10 void factor();
 11 int kk=0;
 12 char prog[80],token[8];
 13 int syn,p,m,n,sum=0;
 14 char ch;
 15 char *rwtab[6]= {"begin","if","then","while","do","end"};
 16 
 17 void scaner() {
 18     m=0;
 19     for(n=0; n<8; n++) token[n]=NULL;
 20     ch=prog[p++];
 21     while(ch==' ') ch=prog[p++];
 22     if((ch>='a' && ch<='z') ||(ch>='A' && ch<='Z')) {
 23         while((ch>='a' && ch<='z') ||(ch>='A' && ch<='Z')||(ch>='0' && ch<='9')) {
 24             token [m ++] = CH;
 25              CH = PROG [P ++ ];
 26 is          }
 27          token [m ++] = ' \ 0 ' ;
 28          SYN = 10 ;
 29          P = p- . 1 ;       // back one character 
30          for (n- = 0 ; n-< . 6 ; n-++ ) {
 31 is              IF (strcmp (token, rwtab [n-]) == 0 ) {
 32                  SYN = n-+ . 1 ;
 33 is                  BREAK ;
 34 is              }
 35         }
 36     } else if(ch>='0' && ch<='9') {
 37         sum=0;
 38         while(ch>='0' && ch<='9') {
 39             sum=sum*10+ch-'0';
 40             ch=prog[p++];
 41         }
 42         p=p-1;
         syn =4311;
 44     } else {
 45         switch(ch) {
 46             case '<':
 47                 m=0;
 48                 token[m++]=ch;
 49                 ch=prog[p];
 50                 if(ch=='>') {
 51                     syn=21;
 52                     token[m++]=ch;
 53                 } else if(ch=='=') {
 54                     syn=22;
 55                     token[m++]=ch;
 56                 } else {
 57                     syn=20;
 58                     p=p-1;
 59                 }
 60                 p=p+1;
 61                 token[m]='\0';
 62                 break;
 63             case '>':
 64                 m=0;
 65                 token[m++]=ch;
 66                 ch=prog[p++];
 67                 if(ch=='=') {
 68                     syn=24;
 69                     token[m++]=ch;
 70                 } else {
 71                     syn=23;
 72                     p=p-1;
 73                 }
 74                 break;
 75             case ':':
 76                 m=0;
 77                 token[m++]=ch;
 78                 ch=prog[p++];
 79                 if(ch=='=') {
 80                     syn=18;
 81                     token[m++]=ch;
 82                 } else {
 83                     syn=17;
 84                     p=p-1;
 85                 }
 86                 break;
 87             case '+':
 88                 syn=13;
 89                 token[0]=ch;
 90                 break;
 91             case '-':
 92                 syn=14;
 93                 token[0]=ch;
 94                 break;
 95             case '*':
 96                 syn=15;
 97                 token[0]=ch;
 98                 break;
 99             case '/':
100                 syn=16;
101                 token[0]=ch;
102                 break;
103             case ';':
104                 syn=26;
105                 token[0]=ch;
106                 break;
107             case '(':
108                 syn=27;
109                 token[0]=ch;
110                 break;
111             case ')':
112                 syn=28;
113                 token[0]=ch;
114                 break;
115             case '=':
116                 syn=25;
117                 token[0]=ch;
118                 break;
119             case '#':
120                 syn=0;
121                 token[0]=ch;
122                 break;
123             default:
124                 syn=-1;
125         }
126     }
127 }
128 
129 void lrparser() {
130     if (syn==1) { //begin
131         scaner();
132         yucu();
133         if (syn==6) { //end
134             scaner();
135             if (syn==0 && kk==0) printf("success \n");
136         } else {
137             if(kk!=1) printf("error,lose 'end' ! \n");
138             kk=1;
139         }
140     } else {
141         printf("error,lose 'begin' ! \n");
142         kk=1;
143     }
144     return;
145 }
146  
147 void yucu() {
148     statement();
149     while(syn==26) { 
150         scaner();
151         statement();
152     }
153     return;
154 }
155  
156 void statement() {
157     if (syn==10) { //An identifier 
158          Scaner ();
 159          IF (SYN == 18 is ) { // is: = 
160.              Scaner ();
 161              expression The ();
 162          } the else {
 163              the printf ( " ! Error " );
 164 is              KK = . 1 ;
 165          }
 166      } the else {
 167 is          the printf ( " error! " );
 168          KK = . 1 ;
 169     }
170     return;
171 }
172  
173  
174 void expression() {
175     term();
176     while(syn==13 || syn==14) {
177         scaner();
178         term();
179     }
180     return;
181 }
182  
183  
184 void term() {
185     factor();
186     while(syn==15 || syn==16) {
187         Scaner ();
 188          factor ();
 189      }
 190      return ;
 191  }
 192   
193   
194  void factor () {
 195      IF (SYN == 10 || == SYN . 11 ) Scaner (); // an identifier or an integer constant when the next read word symbol 
196      the else  IF (SYN == 27 ) {
 197          Scaner ();
 198          expression the ();
 199          IF (SYN == 28 ) Scaner ();
 200 is          the else {
 201              the printf ( "')' Error \ n- " );
 202              KK = . 1 ;
 203          }
 204      } the else {
 205          the printf ( " Expression Error \ n- " );
 206          KK = . 1 ;
 207      }
 208      return ;
 209  }
 210   
211   
212  int main ( ) {
 213      P = 0 ; int I;
 214      the printf ( " ************************************************************ parser ************* ** \ the n- " );
215      the printf ( " Please enter the source: \ n- " );
 216      do {
 217          Scanf ( " % C " , & CH);
 218          PROG [P ++] = CH;
 219      } the while (! CH = ' # ' );
 220      = P 0 ;
 221      Scaner ();
 222      lrparser ();
 223      the printf ( " parsing end \ n-! " );
 224      getch ();
 225 }

Test Results:

 

 

 

 

Guess you like

Origin www.cnblogs.com/chuichuichui1998/p/11959259.html