Twelfth job: Experiment two 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

 

code show as below:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char prog[80]="begin a:=9;x:=2*3;b:=a+x end #",token[50];
char ch;
int row,syn,p,m,i,sum;
char *rwtab[6]={"begin","if","then","while","do","end"};
int flag=0;
void E();
void T();
void E1();
void T1();
void F();

Scaner void () {
// initialize an array DC
for (I = 0; I <. 8; I ++)
token [I] = NULL;
CH PROG = [P ++];
the while (CH == '') {
// spaces encountered pointer plus. 1
CH = PROG [P];
P ++;
}
// identifier
if ((ch> = 'a ' && ch <= 'z') || (ch> = 'A' && ch <= 'Z')) {
m = 0;
the while ((CH> = '0' && CH <= '. 9') || (CH> = 'A' && CH <= 'Z') || (CH> = 'A' && CH <= ' the Z ')) {
token [m ++] = CH;
CH = PROG [P ++];
}
token [m ++] =' \ 0 ';
the P--;
SYN = 10;
// the recognized characters and the defined label comparison operator,
for (I = 0; I <. 6; I ++) {
IF (strcmp (rwtab [I], token) == 0) {
SYN = I +. 1;
BREAK;
}
}
}else if(ch>='0'&&ch<='9'){
sum=0;
while(ch>='0'&&ch<='9'){
sum=sum*10+(ch-'0');
ch=prog[p++];
}
p--;
syn=11;
}else switch(ch){
case '+':
token[0]=ch;syn=13;
break;
case '-':
token[0]=ch;syn=14;
break;
case '*':
token[0]=ch;syn=15;
break;
case '/':
token[0]=ch;syn=16;
break;
case ':':
i=0;
token[i++]=ch;
ch=prog[p++];
if(ch=='='){
token[i++]=ch;
syn=18;
}else{
syn=17;
p--;
}
break;
case '<':
i=0;
token[i++]=ch;
ch=prog[p++];
if(ch=='='){
token[i++]=ch;
syn=21;
}else if(ch=='>'){
token[i++]=ch;
syn=22;
}else{
syn=20;
p--;
}
break;
case '>':
i=0;
token[i++]=ch;
ch=prog[p++];
if(ch=='='){
token[i++]=ch;
syn=24;
}else{
syn=23;
p--;
}
break;
case '=':
token[0]=ch;syn=25;
break;
case ';':
token[0]=ch;syn=26;
break;
case '(':
token[0]=ch;syn=27;
break;
case ')':
token[0]=ch;syn=28;
break;
case '#':
token[0]=ch;syn=0;
break;
case '\n':
syn=100;
break;
default:
syn = -1;
break;
}
}

void E(){
if(syn==1){
scaner();
T();
while(syn==26){
scaner();
T();
}
if(syn==6){
scaner();
if(syn==0 && flag==0){
printf("success!\n");
}
}
else{
printf("error,缺少end!\n");
}
}else{
printf("error,缺少begin!\n");
flag=1;
}
return ;
}

void E1(){
T1();
while(syn==13||syn==14){
scaner();
T1();
}
return ;
}

T void () {
IF (SYN == 10) {
Scaner ();
IF (SYN 18 is ==) {
Scaner ();
E1 ();
} {the else
the printf ( "syntax error is an error:!% s \ n ", token);
In Flag =. 1;
}
} the else {
! IF (In Flag =. 1) {
the printf (" syntax error, the error is:% S \ n-", token);
In Flag =. 1;
}
}
}

void T1(){
F();
while(syn==15||syn==16){
scaner();
F();
}
return ;
}

F. void () {
IF (SYN SYN || == == 10. 11) {
Scaner ();
} the else IF (SYN == 27) {
Scaner ();
E1 ();
IF (SYN == 28)
Scaner ( );
the else {
the printf ( "no ''), a syntax error \ n-!");
In Flag =. 1;
}
}
the else {
the printf ( "syntax error error is:% S \ n-!", token);
In Flag = . 1;
}
return;
}

int main () {
P = 0;
the printf ( "Please input syntax statement:");
do {
CH = getchar ();
PROG [P ++] = CH;
} the while (CH = '#'!);
P = 0;
scaner ();
E ();
printf ( "analysis of the end!");
getchar ();
}

 

Guess you like

Origin www.cnblogs.com/keshangming/p/11940855.html