12 23 Recursive syntax analysis

Source: Internet
Author: User

#include <stdio.h>
Char Curr;
Char ll1[1000];
int n=-1;

void Top ();
void Top1 ();
void Top2 ();
void Top ();
void Top1 ();
void error ();
void Scaner ();

void Main ()
{char ch;
int i=0;
printf ("Please enter the Curr syntax to parse: (end with #) \ n");
do{
scanf ("%c", &ch);
Ll1[i] = ch;
i++;
}while (ch! = ' # ');
Scaner ();
TOP1 ();
if (Curr = = ' # ')
printf ("Success! \ n ");
else{
printf ("Grammatical Error!! \ n ");
Error ();
}

}

void Scaner () {///for reading the next character of the source function

n++;
if (ll1[n] = = ") {
n++;
}else{
Curr = Ll1[n];
}
}

void Top () {
TOP2 ();
Top ();
}

void Top1 () {
Top ();
Top1 ();
}

void Top2 () {
if (Curr = = ' (') {
Scaner ();
TOP1 ();
if (Curr = = ') ') {
Scaner ();
}
Else
{
Error ();
}
Else
{
Judging whether it's a function or a number
while (1) {
if (Curr >= ' a ' && curr<= ' z ' | | curr >= ' a ' && curr <= ' z ' | | curr >= ' 0 ' && curr &L t;= ' 9 ') {
Scaner ();
}
Else
{
Break
}
}
}
}

void Top () {
if (Curr = = ' * ') {
Scaner ();
TOP2 ();
Top ();
}else if (Curr = = '/') {
Scaner ();
TOP2 ();
Top ();
}else if (ll1[n+1] = = ' # ' | | LL1[N+1] = = ' + ' | | LL1[N+1] = = '/' | | LL1[N+1] = = ' * ' | | LL1[N+1] = = '-') {
if (ll1[n]! = ' (' && ll1[n]! = ') ')
Error ();
}
}

void Top1 () {
if (Curr = = ' + ') {
Scaner ();
Top ();
Top1 ();
}
else if (Curr = = '-') {
Scaner ();
Top ();
Top1 ();
}else if (ll1[n+1] = = ' # ' | | LL1[N+1] = = ' + ' | | LL1[N+1] = = '/' | | LL1[N+1] = = ' * ' | | LL1[N+1] = = '-') {
if (ll1[n]! = ' (' && ll1[n]! = ') ')
Error ();
}
}

void error () {
printf ("syntax is wrong after%d%c!") \ n ", N,curr);
}

12 23 Recursive syntax analysis

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.