Job 12- second experiment recursive descent parser

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

 

Codes are as follows:

#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;E ();
void



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';
        son-
        p;=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){
                the printf ( 
            getsyn ();" ! Success \ n- " ); 
            }         
        } 
        the else { 
            the printf ( " syntax error, missing End \ n-! " ); 
        } 
    } The else { 
        the printf ( " ! Errors syntax, missing the begin \ n- " ); 
        In Flag = . 1 ; 
    } 
    return ; 
    
} 

void T () {
     IF (SYN == 10 ) { 
        getsyn (); 
        IF (SYN == 18 is ) { 
            E1 (); 
        } the else {
            the printf ( " Expression of a syntax error Error:!% S \ n- " , ssym); 
            In Flag = . 1 ; 
        } 
    } the else {
         IF (! = In Flag . 1 ) { 
            the printf ( " expression syntax error, the error is:% S \ n- " , ssym); 
            In Flag = . 1 ; 
        } 
        
    } 
} 

void E1 () { 
    Tl (); 
    the while (SYN == 13 is || SYN == 14 ) { 
        getsyn ();  
        Tl ( );
    } 
    return ; 
} 

voidTl () { 
    F. (); 
    The while (SYN == 15 || SYN == 16 ) { 
        getsyn (); 
        F. (); 
    } 
    Return ; 
} 
void F. () {
     IF (SYN == 10 || SYN == . 11 ) { 
        getsyn (); 
    } the else  IF (SYN == 27 ) { 
        getsyn (); 
        E1 (); 
        IF (SYN == 28 ) 
            getsyn (); 
        the else { 
            the printf ( " no ''), a syntax error! \ the n- " ); 
            Flag= . 1 ; 
        } 
    } 
    the else { 
        the printf ( " Expression of a syntax error Error:!% S \ n- " , ssym); 
        In Flag = . 1 ; 
    } 
    return ; 
} 


int main () { 
    P = 0 ; 
    the printf ( " Enter paragraph statement: " );
     do { 
        CH = getchar (); 
        wsym [P ++] = CH; 
    } the while (CH =! ' # '); 
    P = 0; 
    Getsyn (); 
    ; E () 
    (printf " analysis is finished! " ); 
    Getchar ();     
}

 

Screenshot operating results as follows:

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/heiyedeshihouh/p/11955615.html