Recursive descent analysis-Experimental report

Source: Internet
Author: User

First, Experimental Purpose

Compile a recursive descent analysis program.

E→te '

E ' → (+t|-t) e ' |ε

T→ft '

T ' → (*f|/f) t ' |ε

f→ (E) |i

Second, experimental content and requirements

Input: an arithmetic expression;

Judge: Each encounter a terminator, then determine whether the current read word symbol is matched with the Terminator, if matched, continue to read the next word symbol, if not match, then error processing.

Each non-Terminator is encountered, the corresponding parser subroutine is called

Test methods, steps and Results

1. SOURCE program Name: recursive descent analysis. C

executable name: recursive descent parsing. exe

#include <stdio.h>
#include <string.h>
void Scaner ();
Error ();
E ();
E1 ();
T ();
T1 ();
F ();
Char character[80];//holds all the input characters
Char token[8];//Word itself string
Char ch;//single character
int p,m=0,i,sum=0,syn;//sum: integer constant row: Record the line number of the newly read-in Word lines, syn: Word species code
Char *keywords[6]={' begin ', ' if ', ' then ', ' while ', ' Do ', ' end '};
void Main () {
P=0;
printf ("Please enter a string:");
Do
{
Ch=getchar ();
Character[i++]=ch;
}while (ch!= ' # ');//Enter with the # key to end
P=0;
Do
{
Scaner ();//Recognize words
Switch (SYN)
{
Case 11:printf ("(%d,%d) \ n", syn,sum);
Case-1: printf ("(%s,#) \ n", token);
Default:printf ("(%d,%s) \ n", Syn,token);
}
}while (syn!=0);
P=0;
Scaner ();
E ();
if (syn==0)
printf ("success!\n");
Else
printf ("failure!\n");
}
Lexical analysis Program
void Scaner ()
{
for (i=0;i<8;i++)
Token[i]=null;
Ch=character[p++];
while (ch== ")
{
CH=CHARACTER[P];
p++;
}
if ((ch>= ' a ' &&ch<= ' z ') | | (ch>= ' A ' &&ch<= ' Z ')) Identifier or variable name
{
m=0;
while ((ch>= ' 0 ' &&ch<= ' 9 ') | | (ch>= ' A ' &&ch<= ' z ') | | (ch>= ' A ' &&ch<= ' Z '))
{
Token[m++]=ch;
Ch=character[p++];
}
token[m++]= ' + ';
p--;
syn=10;
for (i=0;i<6;i++)//Compare the identified characters with the defined identifiers,
if (strcmp (Token,keywords[i]) ==0)
{
syn=i+1;
Break
}
}
else if ((ch>= ' 0 ' &&ch<= ' 9 '))//Digital
{
sum=0;
while ((ch>= ' 0 ' &&ch<= ' 9 '))
{
sum=sum*10+ (ch-' 0 ');
Ch=character[p++];

}
syn=11;
p--;
}
else switch (CH)//Other characters
{
Case ' < ': m=0;token[m++]=ch;
Ch=character[p++];
if (ch== ' = ')
{
syn=21;
Token[m++]=ch;
}
else if (ch== ' > ')
{
syn=22;
Token[m++]=ch;
}
Else
{
syn=23;
p--;
}
Break
Case ' > ': m=0;token[m++]=ch;
Ch=character[p++];
if (ch== ' = ')
{
syn=24;
Token[m++]=ch;
}
Else
{
syn=20;
p--;
}
Break
Case ': ': m=0;token[m++]=ch;
Ch=character[p++];
if (ch== ' = ')
{
syn=18;
Token[m++]=ch;
}
Else
{
syn=17;
p--;
}
Break
Case ' + ': syn=13;token[0]=ch;break;
Case '-': syn=14;token[0]=ch;break;
Case ' * ': syn=15;token[0]=ch;break;
Case '/': syn=16;token[0]=ch;break;
Case ' = ': syn=25;token[0]=ch;break;
Case '; ': syn=26;token[0]=ch;break;
Case ' (': syn=27;token[0]=ch;break;
Case ') ': syn=28;token[0]=ch;break;
Case ' # ': syn=0;token[0]=ch;break;
Default:syn=-1;break;

}
}
Error ()
{
printf ("(%d,%s) failure! \ n ", syn, token);
}
E ()
{
T ();
E1 ();
}
E1 ()
{
if (syn==13| | SYN==14)
{
Scaner ();
T ();
E1 ();
}

}
T ()
{
F ();
T1 ();
}
T1 ()
{
if (syn==15| | SYN==16)
{
Scaner ();
F ();
T1 ();
}else if (syn!=28 && syn!=0 && syn!=13 && syn!=14)
Error ();

}
F ()
{
if (syn==27)
{
Scaner ();
E ();
if (syn==28)
Scaner ();
else error ();
}
else if (syn==10| | SYN==11)
Scaner ();
}

Recursive descent analysis-Experimental report

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.