精华内容
下载资源
问答
  • 递归向下法语法分析

    2011-12-08 09:53:53
    递归向下的方法,模拟语法分析的过程,分析过程中输出产生式。
  • 递归向下子程序分析法基本思想: 对文法中的每个非终结符(语法成分)编写一个子程序,而子程序的代码结构由相应非终结符的产生式右部所决定: (1)产生式右部的终结符与输入符号相匹配 (2)非终结符与相应的子...

    递归向下子程序分析法基本思想:
    对文法中的每个非终结符(语法成分)编写一个子程序,而子程序的代码结构由相应非终结符的产生式右部所决定:
    (1)产生式右部的终结符与输入符号相匹配

    (2)非终结符与相应的子程序调用对应

    本程序采用文法:
    E→E+T|E-T|T|-E
    T→TF|T/F|F
    F→(E) | i
    消除左递归:
    E→TE’| -EE’
    E’→+TE’| -TE’|ε
    T→FT’
    T’→
    FT’| /FT’|ε
    F→( E ) | i

    # -*- coding: utf-8 -*-
    """
    Created on Tue Apr 21 18:33:13 2020
    
    @author: 侯明会
    """
    #文法G为:  E→E+T|E-T|T|-E
    #          T→T*F|T/F|F
    #          F→(E) | i
    #消除左递归:E→TE'| -EE'
    #           E'→+TE'| -TE'|ε
    #          T→FT' 
    #          T'→*FT'| /FT'|ε
    #          F→( E ) | i
    
    def error():
        print("failed to match!")
        global flag
        flag=False
    def match(t):#匹配终结符(t:终结符)
        global pos 
        if s[pos] == t:  #判断当前终结符是否匹配  
             pos+=1       #指针位置后移
        else:
             error()
    def E():#E→TE'| -EE'
        if s[pos]=='#':
            return
        elif s[pos] == '-':
            match('-')
            if flag==True:
                print("E->-EE'")
                E()
                E1()
            else:
                return
        else:
            print("E->TE'")
            T()
            E1()
    def E1():#E'→+TE'| -TE'|ε
        if s[pos]=='+':
            match('+')
            if flag==True:
                print("E'->+TE'")
                T()
                E1()
            else:
                return
        elif s[pos]=='-':
            match('-')
            if flag==True:
                print("E'->-TE'")
                T()
                E1()
            else:
                return
    def T():#T→FT' 
        print("T->FT'")
        F()
        T1()
    def T1():#T'→*FT'| /FT'|ε
        if s[pos]=='*':
            match('*')
            if flag==True:
                print("T'→*FT'")
                F()
                T1()
            else:
                return
        elif s[pos]=='/':
            match('/')
            if flag==True:
                print("T'→/FT'")
                F()
                T1()
            else:
                return
    def F():#F→( E ) | i
         if s[pos] == 'i':
             match('i')
             if flag==True:
                 print("F->i")
             else:
                return
         elif s[pos] == '(':
             match('(')
             if flag==True:
                print("F->(E)")
                E()
                if s[pos] == ')':
                    match(')')
                    if flag==True:
                        pass
                    else:
                        return
                else:
                    error()
             else:
                return
         else:
            error()   
    
    if __name__=='__main__':
        pos=0
        s=input('请输入以#结尾的字符串:')
        flag=True
        E()
        if s[pos]=='#' and flag==True:
            print("语句合法!")
        else:
            print("语句不合法!")
        
        
    

    实验结果
    在这里插入图片描述
    在这里插入图片描述

    展开全文
  • 这个程序有些局限性,即不能分析任意语法。分析的语法如下: 代码结构如下: 代码如下: Main.java:package cn.porkbar.recursion;import java.io.IOException; import java.util.Scanner;public class Main { ...

    这个程序有些局限性,即不能分析任意语法。


    分析的语法如下:
    这里写图片描述


    代码结构如下:
    这里写图片描述


    代码如下:
    Main.java:

    package cn.porkbar.recursion;
    
    import java.io.IOException;
    import java.util.Scanner;
    
    public class Main
    {
        public static void main(String[] args)
        {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要分析的文件路径,输入-1退出:");
            String op;
            RecursionDown parser;
            while (sc.hasNext())
            {
                op = sc.next();
                if (op.equals("-1"))
                {
                    System.out.println("谢谢使用!");
                    break;
                }
                else
                {
                    try
                    {
                        parser = new RecursionDown(op);
                        parser.Parser();
                    }
                    catch (IOException e)
                    {
                        System.out.println("无法打开文件!");
                    }
    
                }
                System.out.println("*****************************\n请输入要分析的文件路径,输入-1退出:");
            }
        }
    }
    

    RecursionDown.java:

    /*
        规则:
        ⑴<程序>::=begin<语句串>end
        ⑵<语句串>::=<语句>{;<语句>}
        ⑶<语句>::=<赋值语句>
        ⑷<赋值语句>::=ID:=<表达式>
        ⑸<表达式>::=<项>{+<项> | -<项>}
        ⑹<项>::=<因子>{*<因子> | /<因子>
        ⑺<因子>::=ID | NUM | (<表达式>)
    
        syn映射表:
        -1:未知的符号
        0:结束符$
        10:自定义变量名
        1、2...9:begin和end关键字(3-9为保留串,后续可继续扩充)
        11:数字
        12、13、14、15:加减乘除符号
        16:';'字符
        17:':='字符
        18、19:左右括号
    */
    package cn.porkbar.recursion;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import cn.porkbar.util.Util;
    
    public class RecursionDown
    {
        private File file;      //要读取的文件
        static private String[] rwtab = {"begin","end"};        //关键字映射表
        private char[] pro = new char[1024*1024];       //可以读取1M以内的文件
        private int pos;        //扫描的位置
        private int syn;        //扫描结果的特征值,对应的映射表在上面注释
        private StringBuffer token;     //扫描出来的字符串
        private int num;        //扫描出来的数字
        public RecursionDown(String road) throws IOException
        {
            file = new File(road);
            FileInputStream in = new FileInputStream(file);
            {
                int pos = 0;
                int t;
                while ((t = in.read()) != -1)
                    pro[pos++] = (char)t;
    
                //Debug
    //          System.out.println(new String(pro,0,pos));
            }
            in.close();
            pos = 0;
    
            System.out.println("待编译的本文为:");
            System.out.println(String.valueOf(pro).trim());
        }
        private boolean Scan()
        {
            char ch;
            do      //忽略空格、换行符、制表符
            {
                ch = pro[pos++];            
            }while (ch == ' ' || ch == '\t' || ch == '\n');
    
            if (Util.isAlpha(ch))
            {
                token = new StringBuffer("");
                token.append(ch);
                while(Util.isAlpha(pro[pos]) || Util.isDigit(pro[pos]))
                {
                    ch = pro[pos++];
                    token = token.append(ch);
                }
    
                //Debug
    //          System.out.println(token.toString());
    
                syn = 10;
                for (int i = 0 ; i < rwtab.length ; i++)
                {
                    if (token.toString().equals(rwtab[i]))
                    {
                        syn = i+1;
                        break;
                    }
                }
                return true;
            }
            else if (Util.isDigit(ch))
            {
                num = 0;
                num = ch-'0';
                while (Util.isDigit(pro[pos]))
                    num = num*10 + pro[pos++] - '0';
                syn = 11;
                if (Util.isAlpha(pro[pos]))     //后面如果跟上字母则出错
                    return false;
                return true;
            }
            else
            {
                switch(ch)
                {
                    case '+':
                        syn = 12;
                        break;
                    case '-':
                        syn = 13;
                        break;
                    case '*':
                        syn = 14;
                        break;
                    case '/':
                        syn = 15;
                        break;
                    case ';':
                        syn = 16;
                        break;
                    case ':':
                        if (pro[pos] == '=')
                            pos++;
                        else
                        {
                            syn = -1;
                            return false;
                        }
                        syn = 17;
                        break;
                    case '(':
                        syn = 18;
                        break;
                    case ')':
                        syn = 19;
                        break;
                    case '$':
                        syn = 0;
                        break;
                    default:
                        syn = -1;
                        return false;
                }
                return true;
            }
        }
        private boolean SentenceAnalysis()
        {
            if (!Statement())
                return false;
            while (syn == 16)       //这个地方应该用循环,表示可以多个语句
            {
                Scan();
                if (!Statement())
                    return false;
            }
            return true;
        }
        private boolean Factor()
        {
            if (syn == 10 || syn == 11)
            {
                Scan();
                return true;
            }
            if (syn == 18)
            {
                Scan();
                if (!Expression())
                    return false;
                if (syn == 19)
                {
                    Scan();
                    return true;
                }
                else
                {
                    System.out.println("缺少')'!");
                    return false;
                }
            }
            else
            {
                System.out.println("因子编译出错!");
                return false;
            }
        }
        private boolean Term()
        {
            if (!Factor())
                return false;
            if (syn == 14 || syn == 15)
            {
                Scan();
                if (!Factor())
                    return false;
            }
            return true;
            /*else if (syn == 16)       //若项后面没有*或/号,也可能为空,不用出错处理,给上一级扫描即可
            {
                Scan();
                return true;
            }
            else
            {
                System.out.println("项编译出错!");
                return false;
            }*/
        }
        private boolean Expression()
        {
            if (!Term())
                return false;
            if (syn == 12 || syn == 13)
            {
                Scan();
                if (!Term())
                    return false;
            }
            return true;
            /*else if (syn == 2)        //没有+-号丢给上一级处理
            {
                Scan();
                return true;
            }
            else
            {
                System.out.println("表达式编译出错!");
                return false;
            }*/
        }
        private boolean Statement()
        {
            if (syn == 10)      //自定义变量名
            {
                Scan();
                if (syn == 17)
                {
                    Scan();
                    if (!Expression())
                        return false;
                    return true;
                }
                else
                {
                    System.out.println("不合法的赋值语句!");
                    return false;
                }
            }
            else
            {
                System.out.println("不合法的变量名!");
                return false;
            }
        }
        public void Parser()
        {
            Scan();
            if (syn == 1)
            {
                Scan();
                if (!SentenceAnalysis())
                {
                    return;
                }
                if (syn == 2)
                {
                    Scan();
                    if (syn == 0)
                    {
                        System.out.println("语法分析成功!");
                        return;
                    }
                    else
                    {
                        System.out.println("没有找到合法的结束符!");
                    }
                }
                else
                    System.out.println("代码结束符end读取出错!");
            }
            else
            {
                System.out.println("代码开始符begin读取出错!");
            }
        }
    }
    

    Util.java:

    package cn.porkbar.util;
    
    import java.util.ArrayList;
    
    public class Util
    {
        static public boolean isAlpha(char ch)      //判断是否是字母
        {
            if ((ch >= 'a' && ch <= 'z') || (ch >='A' && ch <= 'Z'))
                return true;
            return false;
        }
        static public boolean isDigit(char ch)
        {
            if (ch >= '0' && ch <= '9')
                return true;
            return false;
        }
        static public ArrayList<String> getItems(String str)
        {
            char[] chs = str.toCharArray();
            ArrayList<String> items = new ArrayList<String>();
            for (char ch : chs)
            {
                if (ch == '\'')
                {
                    String t = items.get(items.size()-1) + "'";
                    items.remove(items.size()-1);
                    items.add(t);
                }
                else
                {
                    String t = Character.toString(ch);
                    items.add(t);
                }
            }
            return items;
        }
    }
    

    然后我们运行一下看一下效果:
    这里写图片描述

    有兴趣的可以试试分析一下其他的语法,代码有bug欢迎指出~

    展开全文
  • 递归下降分析法(编译原理)

    万次阅读 2015-03-23 19:15:24
    递归向下分析法C语言实现

    递归下降分析法的实现方案


    递归下降分析法的原理是利用函数之间的递归调用模拟语法树自上而下的构造过程,具体实现方法概述如下:

    1)每个非终结符对应一个解析函数;

    2)产生式右侧为该产生式左侧非终结符所对应解析函数的“函数体”;

    3)产生式右侧终结符对应从输入串中“消耗”该终结符的操作;

    4)产生式中的‘|’对应函数体中的“if-else”语句;

    5)对于扩展的BNF文法,产生式中的‘{}’对应函数体中的“while”语句。

    说明:

    此部分中的数据来源为前一篇(词法分析)的结果

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAX_ID_LENGTH	256
    #define MAX_TABLE_SIZE 2048
    #define SUCCESS         1
    
    typedef enum token
    {
    
        T_0=0,          T_1=1,          T_2=2,          T_INT=3,            T_IF=4,         T_ELSE=5,
        T_WHILE=6,      T_FOR=7,        T_READ=8,       T_WRITE=9,          T_BEGIN=10,     T_FOUNCTION=11,
        T_END=12,       T_THEN=13,      T_ADD=14,       T_SUB=15,           T_MUL=16,       T_DIV=17,
        T_L=18,         T_LE=19,        T_G=20,         T_GE=21,            T_NE=22,        T_E=23,
        T_ASSIGN=24,    T_LPAR=25,      T_RPAR=26,      T_COM=27,           T_SEM=28,       T_RETURN=29,
        T_ERROR=30,     T_SYMBOL=31,    T_CONSTANT=32
    } Token;
    
    typedef struct LEX
    {
        Token read;
        struct LEX *next;
        size_t line;
    } MYLEX;
    
    
    MYLEX lex_head,error_temp;
    MYLEX* current;
    Token error_TOKEN=T_0;
    /*初始化信息*/
    
    /*函数申明*/
    int item(void);
    int Block(void);
    int Factor(void);
    int Variable(void);
    int Function(void);
    int Constant(void);
    int Parameter (void );
    int Identifier (void);
    int Main_Program(void);
    int Function_call(void);
    void emergency_exit(void);
    int Read_statement(void);
    int Write_statement(void);
    int Execute_statement(void);
    MYLEX* Advance(MYLEX* read);
    MYLEX* Read_file(MYLEX* read);
    int Relational_operators(void);
    int Assignment_statement(void);
    int Variable_declaration(void);
    int Function_declaration(void);
    int match(Token read,Token aim);
    int Conditional_statement(void);
    int Arithmetic_expression(void);
    int Declarative_statement(void);
    int Conditional_expression(void);
    int Execute_statement_table(void);
    int error(MYLEX *current,Token aim);
    int Declarative_statement_table(void);
    
    
    /*退出*/
    void emergency_exit(void)
    {
        MYLEX* EXIT_TEEMP=lex_head.next;
        current=EXIT_TEEMP;
        while(!current)
        {
            current=EXIT_TEEMP->next;
            free(EXIT_TEEMP);
        }
        exit(0);
    }
    
    /*错误处理*/
    int error(MYLEX *current,Token aim)
    {
        FILE *error_file=fopen("error.txt","a");
        if(!error_file)
        {
            perror("can`t open file \n");
        }
        if(error_temp.line<current->line)
        {
            error_temp=*current;
            error_TOKEN=aim;
            fprintf(error_file,"error in line:%d\tTOKEN:%d\texpeced Token is :%d\n",
                    error_temp.line,error_temp.read,error_TOKEN);
        }
        fclose(error_file);
        return SUCCESS;
    }
    //************************************
    // Method:    Read_file
    // FullName:  Read_file
    // Access:    public
    // Returns:   int
    // Qualifier:读文件
    // Parameter: void
    //************************************
    MYLEX* Read_file(MYLEX* current)
    {
        current->line=0;
        current->next=NULL;
        current->read=T_0;
    
        MYLEX* Temp;
        Temp=current;
        FILE* READ_FILE=fopen("GRAMER.txt","r");
        if(!READ_FILE)
        {
            printf("can`t load file GRAMER.txt, a file like GRAMER.txt is expectd here \n");
            exit(0);
        }
        while(!feof(READ_FILE))
        {
            Temp->next=(MYLEX*)malloc(sizeof(MYLEX));
            Temp=Temp->next;
            if(!Temp)
            {
                printf("can`t malloc a space for this program!\n");
                emergency_exit();
            }
            Temp->line=0;
            Temp->next=NULL;
            Temp->read=T_0;
            fscanf(READ_FILE,"%d\t%d\n",&(Temp->line),&(Temp->read));
        }
        fclose(READ_FILE);
        return current->next;
    }
    
    //************************************
    // Method:    Advance
    // FullName:  Advance
    // Access:    public
    // Returns:   Token
    // Qualifier:	前进移向下一个TOKEN
    // Parameter: void
    //************************************
    MYLEX* Advance(MYLEX* current)
    {
        FILE*EAT_FILE=fopen("symtab2.txt","a");
        FILE* LEFT_FILE=fopen("detail.txt","a");
        if((!EAT_FILE)||(!LEFT_FILE))
        {
            perror("can`t open file");
            emergency_exit();
        }
        MYLEX* Temp=current->next;
        printf("you have eat the one is :%3d\n\n follow is \n",current->read);
        fprintf(EAT_FILE,
                "\nyou have eat the one is line:%3d\tToken:%3d\n",
                current->line,current->read);
        fprintf(LEFT_FILE,
                "\n\ncurrent eat the one is line:%3d\tToken:%3d\nfollow is\n",
                current->line,current->read);
        for(Temp; Temp!=NULL; Temp=Temp->next)
        {
            printf("line:%3d \t token=%3d\n",
                   Temp->line,Temp->read);
            fprintf(LEFT_FILE,"line:%3d \t token=%3d\n",
                    Temp->line,Temp->read);
        }
        fclose(EAT_FILE);
        fclose(LEFT_FILE);
        if(current==NULL)
        {
            printf("all work is to be done!,there is an error in your project!\n ");
            emergency_exit();
        }
        return current->next;
    }
    
    //************************************
    // Method:    main
    // FullName:  main
    // Access:    public
    // Returns:   int
    // Qualifier:	主程序
    // Parameter: int agrc
    // Parameter: char * agrv[]
    //************************************
    int main(int agrc, char*agrv[])
    {
        error_temp.line=0;
        error_temp.next=NULL;
        error_temp.read=T_0;
    
        printf("\t编译原理实验二\n\tXXXXXXXXX\n\t\t—NEWPLAN\n");
        current=Read_file(&lex_head);
        system("pause");
    
        if(Main_Program())
        {
            printf("\n\ncongratulation\tsuccessfully!\n\n");
            system("del error.txt");
        }
        else
        {
            printf(" sorry you have failed!\n");
            printf("error in line:%d\tTOKEN:%d\texpeced Token is :%d\n",
                   error_temp.line,error_temp.read,error_TOKEN);
        }
        system("pause");
        emergency_exit();/*释放空间,函数返回*/
        return 0;
    }
    
    //************************************
    // Method:    match
    // FullName:  match
    // Access:    public
    // Returns:   int
    // Qualifier:	匹配
    // Parameter: Token read
    // Parameter: Token aim
    //************************************
    int match(Token read ,Token aim)
    {
        return (read==aim)? (current=Advance(current),SUCCESS): (error(current,aim),!SUCCESS);
    }
    
    //************************************
    // Method:    Main_Program
    // FullName:  Main_Program
    // Access:    public
    // Returns:   int
    // Qualifier:	开始主程序
    // Parameter: void
    //************************************
    int Main_Program(void)
    {
        return Block()? SUCCESS: !SUCCESS;
    }
    
    //************************************
    // Method:    Block
    // FullName:  Block
    // Access:    public
    // Returns:   int
    // Qualifier:/*分程序*/
    // Parameter: Token read
    //************************************
    int Block(void)
    {
        MYLEX* Temp=current;
        return (match(current->read,T_BEGIN)&&Declarative_statement_table()&&match(current->read,T_SEM)&&
                Execute_statement_table()&&match(current->read,T_END))?SUCCESS:(current=Temp,!SUCCESS);
    }
    
    
    //************************************
    // Method:    Declarative_statement_table
    // FullName:  Declarative_statement_table
    // Access:    public
    // Returns:   int
    // Qualifier:/*说明语句表*/
    // Parameter: Token read
    //************************************
    int Declarative_statement_table()
    {
        MYLEX* Temp=current;
        if(!Declarative_statement())
        {
            current=Temp;
            return !SUCCESS;
        }
        /*采用扩展的BNF方法消除左递归*/
        for(;;)
        {
            Temp=current;
            if(!match(current->read,T_SEM))
                break;
            if(!Declarative_statement())
            {
                current=Temp;
                break;
            }
        }
        return SUCCESS;
    }
    
    
    //************************************
    // Method:    Declarative_statement
    // FullName:  Declarative_statement
    // Access:    public
    // Returns:   int
    // Qualifier:/*说明语句*/
    // Parameter: Token read
    //************************************
    int Declarative_statement(void)
    {
        MYLEX* Temp=current;
        return Variable_declaration()? SUCCESS :
               (current=Temp,Function_declaration()? SUCCESS:!SUCCESS);
    }
    
    
    //************************************
    // Method:    Execute_statement_table
    // FullName:  Execute_statement_table
    // Access:    public
    // Returns:   int
    // Qualifier:/*执行语句表*/
    // Parameter: Token read
    //************************************
    int Execute_statement_table(void)
    {
        MYLEX* Temp=current;
        if(!Execute_statement())
        {
            current=Temp;
            return !SUCCESS;
        }
        for(;;)
        {
            Temp=current;
            if(!match(current->read,T_SEM))
                break;
            if(!Execute_statement())
            {
                current=Temp;
                break;
            }
        }
        return SUCCESS;
    }
    
    
    //************************************
    // Method:    Execute_statement
    // FullName:  Execute_statement
    // Access:    public
    // Returns:   int
    // Qualifier:/*执行语句*/
    // Parameter: Token read
    //************************************
    int Execute_statement(void)
    {
        MYLEX* Temp=current;
        if(Read_statement())
            return SUCCESS;
        current=Temp;
        if(Write_statement())
            return SUCCESS;
        current=Temp;
        if(Conditional_statement())
            return SUCCESS;
        current=Temp;
        if(Assignment_statement())
            return SUCCESS;
        current=Temp;
        return !SUCCESS;
    }
    
    
    //************************************
    // Method:    Variable_declaration
    // FullName:  Variable_declaration
    // Access:    public
    // Returns:   int
    // Qualifier:/*变量说明*/
    // Parameter: Token read
    //************************************
    int Variable_declaration(void)
    {
        MYLEX* Temp=current;
        return (match(current->read , T_INT)&&Variable())?SUCCESS:(current=Temp,!SUCCESS);
    }
    
    
    //************************************
    // Method:    Function_declaration
    // FullName:  Function_declaration
    // Access:    public
    // Returns:   int
    // Qualifier:/*函数说明*/
    // Parameter: Token read
    //************************************
    int Function_declaration(void)
    {
        MYLEX* Temp=current;
        return (match(current->read, T_INT)&&match(current->read, T_FOUNCTION)&&Identifier()&&
                match(current->read, T_LPAR)&&Parameter()&&match(current->read, T_RPAR)&&
                match(current->read,T_SEM)&&Function())? SUCCESS:(current=Temp,!SUCCESS);
    }
    
    
    //************************************
    // Method:    Variable
    // FullName:  Variable
    // Access:    public
    // Returns:   int
    // Qualifier:/*变量*/
    // Parameter: Token read
    //************************************
    int Variable(void)
    {
        return match(current->read,T_SYMBOL)?SUCCESS:!SUCCESS;
    }
    
    //************************************
    // Method:    Identifier
    // FullName:  Identifier
    // Access:    public
    // Returns:   int
    // Qualifier:/*标识符*/
    // Parameter: Token read
    //************************************
    int Identifier  (void)
    {
        return match(current->read,T_SYMBOL)?SUCCESS:!SUCCESS;
    }
    
    
    //************************************
    // Method:    Parameter
    // FullName:  Parameter
    // Access:    public
    // Returns:   int
    // Qualifier:/*参数*/
    // Parameter: Token read
    //************************************
    int Parameter  (void)
    {
        MYLEX* Temp=current;
        return Variable()?SUCCESS:(current=Temp,!SUCCESS);
    }
    
    
    //************************************
    // Method:    Function
    // FullName:  Function
    // Access:    public
    // Returns:   int
    // Qualifier:/*函数体*/
    // Parameter: Token read
    //************************************
    int Function(void)
    {
        MYLEX* Temp=current;
        return (match(current->read, T_BEGIN)&&Declarative_statement_table()&&match(current->read, T_SEM)&&
                Execute_statement_table()&&match(current->read,T_END))?SUCCESS:(current=Temp,!SUCCESS);
    }
    
    
    //************************************
    // Method:    Read_statement
    // FullName:  Read_statement
    // Access:    public
    // Returns:   int
    // Qualifier:/*读语句*/
    // Parameter: Token read
    //************************************
    int Read_statement(void)
    {
        MYLEX* Temp=current;
        return (match(current->read,T_READ)&&match(current->read,T_LPAR)&&Variable()&&
                match(current->read,T_RPAR))?SUCCESS:(current=Temp,!SUCCESS);
    }
    
    
    //************************************
    // Method:    Write_statement
    // FullName:  Write_statement
    // Access:    public
    // Returns:   int
    // Qualifier:/*写语句*/
    // Parameter: Token read
    //************************************
    int Write_statement(void)
    {
        MYLEX* Temp=current;
        return (match(current->read,T_WRITE)&&match(current->read,T_LPAR)&&Variable()&&
                match(current->read,T_RPAR))?SUCCESS:(current=Temp,!SUCCESS);
    }
    
    
    //************************************
    // Method:    Assignment_statement
    // FullName:  Assignment_statement
    // Access:    public
    // Returns:   int
    // Qualifier:/*赋值语句*/
    // Parameter: Token read
    //************************************
    int Assignment_statement(void)
    {
        MYLEX* Temp=current;
        return (Variable()&&match(current->read,T_ASSIGN)&&
                Arithmetic_expression())? SUCCESS:(current=Temp,!SUCCESS);
    }
    
    //************************************
    // Method:    Conditional_statement
    // FullName:  Conditional_statement
    // Access:    public
    // Returns:   int
    // Qualifier:/*条件语句*/
    // Parameter: Token read
    //************************************
    int Conditional_statement(void)
    {
        MYLEX* Temp=current;
        return (match(current->read,T_IF)&&Conditional_expression()&&
                match(current->read,T_THEN)&&Execute_statement()&&
                match(current->read,T_ELSE)&&Execute_statement()
               )?SUCCESS:(current=Temp,!SUCCESS);
    }
    
    //************************************
    // Method:    Arithmetic_expression
    // FullName:  Arithmetic_expression
    // Access:    public
    // Returns:   int
    // Qualifier:/*算术表达式*/
    // Parameter: Token read
    //************************************
    int Arithmetic_expression(void)
    {
        MYLEX* Temp=current;
        if(!item())
        {
            current=Temp;
            return !SUCCESS;
        }
        for(;;)
        {
            Temp=current;
            if(!match(current->read,T_SUB))
                break;
            if(!item())
            {
                current=Temp;
                break;
            }
        }
        return SUCCESS;
    }
    
    //************************************
    // Method:    item
    // FullName:  item
    // Access:    public
    // Returns:   int
    // Qualifier:/*项*/
    // Parameter: Token read
    //************************************
    int item(void)
    {
        MYLEX* Temp=current;
        if(!Factor())
        {
            current=Temp;
            return !SUCCESS;
        }
        for(;;)
        {
            Temp=current;
            if(!match(current->read,T_MUL))
                break;
            if(!Factor())
            {
                current=Temp;
                break;
            }
        }
        return SUCCESS;
    }
    
    //************************************
    // Method:    Constant
    // FullName:  Constant
    // Access:    public
    // Returns:   int
    // Qualifier:/*常数*/
    // Parameter: Token read
    //************************************
    int Constant(void)
    {
        return match(current->read,T_CONSTANT)?SUCCESS: !SUCCESS;
    }
    
    //************************************
    // Method:    Conditional_expression
    // FullName:  Conditional_expression
    // Access:    public
    // Returns:   int
    // Qualifier:/*条件表达式*/
    // Parameter: Token read
    //************************************
    int Conditional_expression(void)
    {
        MYLEX* Temp=current;
        return (Arithmetic_expression()&&Relational_operators()&&
                Arithmetic_expression())? SUCCESS:(current=Temp,!SUCCESS);
    }
    
    //************************************
    // Method:    Relational_operators
    // FullName:  Relational_operators
    // Access:    public
    // Returns:   int
    // Qualifier:/*关系运算符*/
    // Parameter: Token read
    //************************************
    int Relational_operators(void)
    {
        return ( match(current->read,T_L)||match(current->read,T_LE)||match(current->read,T_G)||
                 match(current->read,T_GE)||match(current->read,T_E)||match(current->read,T_NE)
               )?SUCCESS:!SUCCESS;
    
    }
    
    
    //************************************
    // Method:    Factor
    // FullName:  Factor
    // Access:    public
    // Returns:   int
    // Qualifier:/*因子*/
    // Parameter: Token read
    //************************************
    int Factor(void)
    {
        MYLEX* Temp=current;
        if(Function_call())
            return SUCCESS;
        current=Temp;
        if(Variable())
            return SUCCESS;
        current=Temp;
        if(Constant())
            return SUCCESS;
        current=Temp;
        return !SUCCESS;
    }
    
    //************************************
    // Method:    Function_call
    // FullName:  Function_call
    // Access:    public
    // Returns:   int
    // Qualifier:/*函数调用*/
    // Parameter: Token read
    //************************************
    int Function_call(void)
    {
        MYLEX* Temp=current;
        return (Identifier()&&match(current->read,T_LPAR)&&Arithmetic_expression()&&
                match(current->read,T_RPAR))?SUCCESS:(current=Temp,!SUCCESS);
    }
    

    源码版本2(更容易理解)

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAX_ID_LENGTH	256
    #define MAX_TABLE_SIZE 2048
    #define SUCCESS         1
    
    typedef enum token
    {
    
    	T_0=0,
    	T_1=1,
    	T_2=2,
    
    	T_INT=3,
    	T_IF=4,
    	T_ELSE=5,
    	T_WHILE=6,
    	T_FOR=7,
    	T_READ=8,
    	T_WRITE=9,
    	T_BEGIN=10,
    	T_FOUNCTION=11,
    	T_END=12,
    	T_THEN=13,
    
    	T_ADD=14,
    	T_SUB=15,
    	T_MUL=16,
    	T_DIV=17,
    	T_L=18,
    	T_LE=19,
    	T_G=20,
    	T_GE=21,
    	T_NE=22,
    	T_E=23,
    	T_ASSIGN=24,
    	T_LPAR=25,
    	T_RPAR=26,
    	T_COM=27,
    	T_SEM=28,
    
    	T_RETURN=29,
    	T_ERROR=30,
    	T_SYMBOL=31,
    	T_CONSTANT=32
    } Token;
    
    typedef struct LEX
    {
    	Token read;
    	struct LEX *next;
    	size_t line;
    }MYLEX;
    
    
    #ifndef SUCCESS
    #define SUCCESS   1
    #endif
    
    MYLEX lex_head,error_temp;
    MYLEX* current;
    Token error_TOKEN=T_0;
    /*初始化信息*/
    
    /*函数申明*/
    int item(void);
    int Block(void);
    int Factor(void);
    int Variable(void);
    int Function(void);
    int Constant(void);
    int Parameter (void );
    int Identifier (void);
    int Main_Program(void);
    int Function_call(void);
    void emergency_exit(void);
    int Read_statement(void);
    int Write_statement(void);
    int Execute_statement(void);
    MYLEX* Advance(MYLEX* read);
    MYLEX* Read_file(MYLEX* read);
    int Relational_operators(void);
    int Assignment_statement(void);
    int Variable_declaration(void);
    int Function_declaration(void);
    int match(Token read,Token aim);
    int Conditional_statement(void);
    int Arithmetic_expression(void);
    int Declarative_statement(void);
    int Conditional_expression(void);
    int Execute_statement_table(void);
    int error(MYLEX *current,Token aim);
    int Declarative_statement_table(void);
    
    
    
    void emergency_exit(void)
    {
        MYLEX* EXIT_TEEMP=lex_head.next;
        current=EXIT_TEEMP;
        while(!current)
        {
            current=EXIT_TEEMP->next;
            free(EXIT_TEEMP);
        }
        exit(0);
    }
    
    int error(MYLEX *current,Token aim)
    {
        FILE *error_file=fopen("error.txt","a");
        if(!error_file)
        {
            perror("can`t open file \n");
        }
        if(error_temp.line<current->line)
        {
            error_temp=*current;
            error_TOKEN=aim;
            fprintf(error_file,"error in line:%d\tTOKEN:%d\texpeced Token is :%d\n",
                    error_temp.line,error_temp.read,error_TOKEN);
    
        }
        fclose(error_file);
    
        return SUCCESS;
    }
    //************************************
    // Method:    Read_file
    // FullName:  Read_file
    // Access:    public
    // Returns:   int
    // Qualifier:读文件
    // Parameter: void
    //************************************
    MYLEX* Read_file(MYLEX* current)
    {
        current->line=0;
        current->next=NULL;
        current->read=T_0;
    
        MYLEX* Temp;
        Temp=current;
        FILE* READ_FILE=fopen("GRAMER.txt","r");
        if(!READ_FILE)
        {
            printf("can`t load file GRAMER.txt, a file like GRAMER.txt is expectd here \n");
            exit(0);
        }
    
        while(!feof(READ_FILE))
        {
            Temp->next=(MYLEX*)malloc(sizeof(MYLEX));
            Temp=Temp->next;
            if(!Temp)
            {
                printf("can`t malloc a space for this program!\n");
                emergency_exit();
            }
            Temp->line=0;
            Temp->next=NULL;
            Temp->read=T_0;
            fscanf(READ_FILE,"%d\t%d\n",&(Temp->line),&(Temp->read));
    
        }
        fclose(READ_FILE);
        return current->next;
    }
    
    //************************************
    // Method:    Advance
    // FullName:  Advance
    // Access:    public
    // Returns:   Token
    // Qualifier:	前进移向下一个TOKEN
    // Parameter: void
    //************************************
    MYLEX* Advance(MYLEX* current)
    {
        FILE*EAT_FILE=fopen("symtab2.txt","a");
        FILE* LEFT_FILE=fopen("detail.txt","a");
        if((!EAT_FILE)||(!LEFT_FILE))
        {
            perror("can`t open file");
            emergency_exit();
        }
        MYLEX* Temp=current->next;
        printf("you have eat the one is :%3d\n\n follow is \n",current->read);
        fprintf(EAT_FILE,
                "\nyou have eat the one is line:%3d\tToken:%3d\n",
                current->line,
                current->read);
        fprintf(LEFT_FILE,
                "\n\ncurrent eat the one is line:%3d\tToken:%3d\nfollow is\n",
                current->line,
                current->read);
        for(Temp; Temp!=NULL; Temp=Temp->next)
        {
            printf("line:%3d \t token=%3d\n",
                   Temp->line,Temp->read);
            fprintf(LEFT_FILE,"line:%3d \t token=%3d\n",
                    Temp->line,Temp->read);
        }
        fclose(EAT_FILE);
        fclose(LEFT_FILE);
        if(current==NULL)
        {
            printf("all work is to be done!,there is an error in your project!\n ");
            emergency_exit();
        }
        return current->next;
    }
    
    //************************************
    // Method:    main
    // FullName:  main
    // Access:    public
    // Returns:   int
    // Qualifier:	主程序
    // Parameter: int agrc
    // Parameter: char * agrv[]
    //************************************
    int main(int agrc, char*agrv[])
    {
        error_temp.line=0;
        error_temp.next=NULL;
        error_temp.read=T_0;
    
        printf("\t编译原理实验二\n\tXXXXXXXXX\n\tNEWPLAN\n");
        current=Read_file(&lex_head);
        system("pause");
    
        if(Main_Program())
        {
            printf("\n\ncongratulation\tsuccessfully!\n\n");
            system("del error.txt");
        }
        else
        {
            printf(" sorry you have failed!\n");
            printf("error in line:%d\tTOKEN:%d\texpeced Token is :%d\n",
                   error_temp.line,error_temp.read,error_TOKEN);
        }
    
        system("pause");
        emergency_exit();/*释放空间,函数返回*/
        return 0;
    }
    
    //************************************
    // Method:    match
    // FullName:  match
    // Access:    public
    // Returns:   int
    // Qualifier:	匹配
    // Parameter: Token read
    // Parameter: Token aim
    //************************************
    int match(Token read ,Token aim)
    {
        if(read==aim)
            return SUCCESS;
        error(current,aim);
        return !SUCCESS;
    
    
    //    return read==aim? SUCCESS : !SUCCESS;
    }
    
    //************************************
    // Method:    Main_Program
    // FullName:  Main_Program
    // Access:    public
    // Returns:   int
    // Qualifier:	开始主程序
    // Parameter: void
    //************************************
    int Main_Program(void)
    {
        if(!Block())
            return !SUCCESS;
        return SUCCESS;
    }
    
    //************************************
    // Method:    Block
    // FullName:  Block
    // Access:    public
    // Returns:   int
    // Qualifier:/*分程序*/
    // Parameter: Token read
    //************************************
    int Block(void)
    {
        MYLEX* Temp=current;
        if(!match(current->read,T_BEGIN))
        {
            current=Temp;
            return !SUCCESS;
        }
        current= Advance(current);
        if(!Declarative_statement_table())
        {
    
            current=Temp;
            return !SUCCESS;
        }
    
        if(!match(current->read,T_SEM))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
    
    
        if(!Execute_statement_table())
        {
            current=Temp;
            return !SUCCESS;
        }
        if(!match(current->read,T_END))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
        return SUCCESS;
    }
    
    
    //************************************
    // Method:    Declarative_statement_table
    // FullName:  Declarative_statement_table
    // Access:    public
    // Returns:   int
    // Qualifier:/*说明语句表*/
    // Parameter: Token read
    //************************************
    int Declarative_statement_table()
    {
        MYLEX* Temp=current;
        if(!Declarative_statement())
        {
            current=Temp;
            return !SUCCESS;
        }
        /*采用扩展的BNF方法消除左递归*/
        for(;;)
        {
            if(!match(current->read,T_SEM))
                break;
    
            Temp=current;
            current =Advance(current);
    
            if(!Declarative_statement())
            {
                current=Temp;
                break;
            }
        }
        return SUCCESS;
    }
    
    
    //************************************
    // Method:    Declarative_statement
    // FullName:  Declarative_statement
    // Access:    public
    // Returns:   int
    // Qualifier:/*说明语句*/
    // Parameter: Token read
    //************************************
    int Declarative_statement(void)
    {
        MYLEX* Temp=current;
        if(Variable_declaration())
        {
            return SUCCESS;
        }
        else
        {
            current=Temp;
        }
        if(Function_declaration())
            return SUCCESS;
    
    
        current=Temp;
        return !SUCCESS;
    
    }
    
    
    //************************************
    // Method:    Execute_statement_table
    // FullName:  Execute_statement_table
    // Access:    public
    // Returns:   int
    // Qualifier:/*执行语句表*/
    // Parameter: Token read
    //************************************
    int Execute_statement_table(void)
    {
        MYLEX* Temp=current;
        if(!Execute_statement())
        {
            current=Temp;
            return !SUCCESS;
        }
    
        for(;;)
        {
            if(!match(current->read,T_SEM))
                break;
            Temp=current;
            current=Advance(current);
    
            if(!Execute_statement())
            {
                current=Temp;
                break;
            }
        }
        return SUCCESS;
    }
    
    
    //************************************
    // Method:    Execute_statement
    // FullName:  Execute_statement
    // Access:    public
    // Returns:   int
    // Qualifier:/*执行语句*/
    // Parameter: Token read
    //************************************
    int Execute_statement(void)
    {
        MYLEX* Temp=current;
    
        if(Read_statement())
        {
            return SUCCESS;
        }
        else
        {
            current=Temp;
        }
        if(Write_statement())
        {
            return SUCCESS;
        }
        else
        {
            current=Temp;
        }
    
        if(Conditional_statement())
        {
            return SUCCESS;
        }
        else
        {
            current=Temp;
        }
        if(Assignment_statement())
        {
            return SUCCESS;
        }
    
    
        current=Temp;
        return !SUCCESS;
    }
    
    
    //************************************
    // Method:    Variable_declaration
    // FullName:  Variable_declaration
    // Access:    public
    // Returns:   int
    // Qualifier:/*变量说明*/
    // Parameter: Token read
    //************************************
    int Variable_declaration(void)
    {
        MYLEX* Temp=current;
        if(!match(current->read , T_INT))
        {
            current=Temp;
            return !SUCCESS;
        }
        current =Advance(current);
        if(Variable())
            return SUCCESS;
        current=Temp;
        return !SUCCESS;
    }
    
    
    //************************************
    // Method:    Function_declaration
    // FullName:  Function_declaration
    // Access:    public
    // Returns:   int
    // Qualifier:/*函数说明*/
    // Parameter: Token read
    //************************************
    int Function_declaration(void)
    {
        MYLEX* Temp=current;
        if(!match(current->read, T_INT))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
    
        if(!match(current->read, T_FOUNCTION))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
    
        if(!Identifier())
        {
            current=Temp;
            return !SUCCESS;
        }
        if(!match(current->read, T_LPAR))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
    
        if(!Parameter())
        {
            current=Temp;
            return !SUCCESS;
        }
        if(!match(current->read, T_RPAR))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
        if(!match(current->read,T_SEM))
        {
            current=Temp;
            return !SUCCESS;
        }
        current =Advance(current);
    
        if(!Function())
        {
            current=Temp;
            return !SUCCESS;
        }
    
        return SUCCESS;
    }
    
    
    //************************************
    // Method:    Variable
    // FullName:  Variable
    // Access:    public
    // Returns:   int
    // Qualifier:/*变量*/
    // Parameter: Token read
    //************************************
    int Variable(void)
    {
        MYLEX* Temp=current;
        if(! match(current->read,T_SYMBOL))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
        return SUCCESS;
    }
    
    
    
    //************************************
    // Method:    Identifier
    // FullName:  Identifier
    // Access:    public
    // Returns:   int
    // Qualifier:/*标识符*/
    // Parameter: Token read
    //************************************
    int Identifier  (void)
    {
        MYLEX* Temp=current;
        if(! match(current->read,T_SYMBOL))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
        return SUCCESS;
    }
    
    
    //************************************
    // Method:    Parameter
    // FullName:  Parameter
    // Access:    public
    // Returns:   int
    // Qualifier:/*参数*/
    // Parameter: Token read
    //************************************
    int Parameter  (void)
    {
        MYLEX* Temp=current;
        if(!Variable())
        {
            current=Temp;
            return !SUCCESS;
        }
        return SUCCESS;
    }
    
    
    //************************************
    // Method:    Function
    // FullName:  Function
    // Access:    public
    // Returns:   int
    // Qualifier:/*函数体*/
    // Parameter: Token read
    //************************************
    int Function(void)
    {
        MYLEX* Temp=current;
        if(!match(current->read, T_BEGIN))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
    
        if(!Declarative_statement_table())
        {
            current=Temp;
            return !SUCCESS;
        }
    
        if(!match(current->read, T_SEM))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
    
        if(!Execute_statement_table())
        {
            current=Temp;
            return !SUCCESS;
        }
    
        if(!match(current->read,T_END))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
        return SUCCESS;
    }
    
    
    //************************************
    // Method:    Read_statement
    // FullName:  Read_statement
    // Access:    public
    // Returns:   int
    // Qualifier:/*读语句*/
    // Parameter: Token read
    //************************************
    int Read_statement(void)
    {
        MYLEX* Temp=current;
        if(!match(current->read,T_READ))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
        if(!match(current->read,T_LPAR))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
    
        if(!Variable())
        {
            current=Temp;
            return !SUCCESS;
        }
    
        if(!match(current->read,T_RPAR))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
        return SUCCESS;
    }
    
    
    //************************************
    // Method:    Write_statement
    // FullName:  Write_statement
    // Access:    public
    // Returns:   int
    // Qualifier:/*写语句*/
    // Parameter: Token read
    //************************************
    int Write_statement(void)
    {
        MYLEX* Temp=current;
        if(!match(current->read,T_WRITE))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
    
        if(!match(current->read,T_LPAR))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
    
        if(!Variable())
        {
            current=Temp;
            return !SUCCESS;
        }
    
        if(!match(current->read,T_RPAR))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
        printf("current to be got is:line=%d\t %d\n",current->line,current->read);
        return SUCCESS;
    }
    
    
    //************************************
    // Method:    Assignment_statement
    // FullName:  Assignment_statement
    // Access:    public
    // Returns:   int
    // Qualifier:/*赋值语句*/
    // Parameter: Token read
    //************************************
    int Assignment_statement(void)
    {
        MYLEX* Temp=current;
        if(!Variable())
        {
            current=Temp;
            return !SUCCESS;
        }
    
        if(!match(current->read,T_ASSIGN))
        {
            current=Temp;
            return !SUCCESS;
        }
        current =Advance(current);
    
        if(Arithmetic_expression())
            return SUCCESS;
        current=Temp;
        return !SUCCESS;
    }
    
    //************************************
    // Method:    Conditional_statement
    // FullName:  Conditional_statement
    // Access:    public
    // Returns:   int
    // Qualifier:/*条件语句*/
    // Parameter: Token read
    //************************************
    int Conditional_statement(void)
    {
        MYLEX* Temp=current;
        if(!match(current->read,T_IF))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
    
        if(!Conditional_expression())
        {
    
            current=Temp;
            return !SUCCESS;
        }
    
        if(!match(current->read,T_THEN))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
    
        if(!Execute_statement())
        {
            current=Temp;
            return !SUCCESS;
        }
    
        if(!match(current->read,T_ELSE))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
        if(Execute_statement())
            return SUCCESS;
        current=Temp;
        return !SUCCESS;
    }
    
    //************************************
    // Method:    Arithmetic_expression
    // FullName:  Arithmetic_expression
    // Access:    public
    // Returns:   int
    // Qualifier:/*算术表达式*/
    // Parameter: Token read
    //************************************
    int Arithmetic_expression(void)
    {
        MYLEX* Temp=current;
        if(!item())
        {
            current=Temp;
            return !SUCCESS;
        }
    
        for(;;)
        {
            if(!match(current->read,T_SUB))
                break;
    
            Temp=current;
            current=Advance(current);
            if(!item())
            {
                current=Temp;
                break;
            }
        }
        return SUCCESS;
    }
    
    //************************************
    // Method:    item
    // FullName:  item
    // Access:    public
    // Returns:   int
    // Qualifier:/*项*/
    // Parameter: Token read
    //************************************
    int item(void)
    {
        MYLEX* Temp=current;
        if(!Factor())
        {
            current=Temp;
            return !SUCCESS;
        }
    
        for(;;)
        {
            if(!match(current->read,T_MUL))
                break;
            Temp=current;
            current=Advance(current);
            if(!Factor())
            {
                current=Temp;
                break;
            }
        }
        return SUCCESS;
    }
    
    //************************************
    // Method:    Constant
    // FullName:  Constant
    // Access:    public
    // Returns:   int
    // Qualifier:/*常数*/
    // Parameter: Token read
    //************************************
    int Constant(void)
    {
        MYLEX* Temp=current;
        if(!match(current->read,T_CONSTANT))
        {
            return !SUCCESS;
        }
        current=Advance(current);
        return SUCCESS;
    }
    
    //************************************
    // Method:    Conditional_expression
    // FullName:  Conditional_expression
    // Access:    public
    // Returns:   int
    // Qualifier:/*条件表达式*/
    // Parameter: Token read
    //************************************
    int Conditional_expression(void)
    {
        MYLEX* Temp=current;
        if(!Arithmetic_expression())
        {
            current=Temp;
            return !SUCCESS;
        }
        printf("line = %d,token=%d\n");
        if(!Relational_operators())
        {
            current=Temp;
            return !SUCCESS;
        }
        if(!Arithmetic_expression())
        {
            current=Temp;
            return !SUCCESS;
        }
        return SUCCESS;
    }
    
    //************************************
    // Method:    Relational_operators
    // FullName:  Relational_operators
    // Access:    public
    // Returns:   int
    // Qualifier:/*关系运算符*/
    // Parameter: Token read
    //************************************
    int Relational_operators(void)
    {
        if(match(current->read,T_L))
        {
            current=Advance(current);
            return SUCCESS;
        }
        if(match(current->read,T_LE))
        {
            current=Advance(current);
            return SUCCESS;
        }
        if(match(current->read,T_G))
        {
            current=Advance(current);
            return SUCCESS;
        }
        if(match(current->read,T_GE))
        {
            current=Advance(current);
            return SUCCESS;
        }
        if(match(current->read,T_E))
        {
            current=Advance(current);
            return SUCCESS;
        }
        if(match(current->read,T_NE))
        {
            current=Advance(current);
            return SUCCESS;
        }
        return !SUCCESS;
    
    }
    
    
    //************************************
    // Method:    Factor
    // FullName:  Factor
    // Access:    public
    // Returns:   int
    // Qualifier:/*因子*/
    // Parameter: Token read
    //************************************
    int Factor(void)
    {
        MYLEX* Temp=current;
        if(Function_call())
        {
            return SUCCESS;
        }
        else
        {
            current=Temp;
        }
        if(Variable())
        {
            return SUCCESS;
        }
        else
        {
            current=Temp;
        }
        if(Constant())
        {
            return SUCCESS;
        }
    
        current=Temp;
        return !SUCCESS;
    }
    
    //************************************
    // Method:    Function_call
    // FullName:  Function_call
    // Access:    public
    // Returns:   int
    // Qualifier:/*函数调用*/
    // Parameter: Token read
    //************************************
    int Function_call(void)
    {
        MYLEX* Temp=current;
        if(!Identifier())
        {
            current=Temp;
            return !SUCCESS;
        }
        if(!match(current->read,T_LPAR))
        {
            current=Temp;
            return !SUCCESS;
        }
        current=Advance(current);
    
        if( !Arithmetic_expression())
        {
            current=Temp;
            return !SUCCESS;
        }
        if(!match(current->read,T_RPAR))
        {
            current=Temp;
            return !SUCCESS;
        }
    
        current=Advance(current);
        return SUCCESS;
    
    }
    

    结果分析

    成功结果


    失败结果:


    展开全文
  • /*题目:自顶向下分析法(递归下降分析程序构造) *说明:功能是利用函数之间的递归调用模拟语法树自上而下的构造过程。 *作者:董正荣 *邮箱:chinadongzr@163.com *开发环境:VC6.0 *时间:2011-5-21 */ #...
    /*题目:自顶向下分析法(递归下降分析程序构造)
     *说明:功能是利用函数之间的递归调用模拟语法树自上而下的构造过程。
    *作者:董正荣 *邮箱:chinadongzr@163.com *开发环境:VC6.0 *时间:2011-5-21 */ #include<stdio.h> #include<string.h> //全局变量 char exp[30],gra[30],prod[30]="",chExp='#'; int expSize=0,graSize=0,step=0; //函数声明 /*(1)E->TG (2)G->+TG|-TG|ε (3)T->FS (4)S->*FS|/FS|ε (5)F->(E)|i */ int E(); int T(); int G(); int S(); int F(); //功能函数实现 /*打印文法*/ void printGrammar() { printf("\t\t\t 递归下降分析程序构造\n\t\t(作者:董正荣,邮箱:chinadongzr@163.com)\n"); printf("-----------------------------------------------------------\n"); printf("\t\t\t (1)E->TG\n"); printf("\t\t\t (2)G->+TG|-TG|ε\n"); printf("\t\t\t (3)T->FS\n"); printf("\t\t\t (4)S->*FS|/FS|ε\n"); printf("\t\t\t (5)F->(E)|i\n"); printf("-----------------------------------------------------------\n"); } void GetExp() { printf("请输入表达式:(以#结束)\t"); gets(exp);//获得输入表达式 expSize=strlen(exp); chExp=exp[0]; printf("----------------------------------------------------------\n"); //puts(exp);//显示 } void printHead() { printf("步骤:\t 语法栈:\t\t输入串:\t产生式:\n"); } void printStep() { printf("%d\t%-20s %10s\t \t%-15s\n",step,gra,exp,prod); strcpy(prod,""); step++; if(chExp=='#'&&gra[graSize-1]=='#') { printf("\n表达式分析成功!\n"); } } //语法栈入栈,匹配的语法产生式顺序入栈 void pushGraStack(char* ch) { for(int i=0;i<strlen(ch);i++) { gra[graSize]=ch[strlen(ch)-1-i]; graSize++; } } //语法栈出栈,返回字符ch char popGraStack() { char ch; ch=gra[graSize-1]; gra[graSize-1]='\0'; graSize--; return ch; } //表达式出栈,匹配字符ch void nextChar() { for(int i=0;i<expSize-1;i++) { exp[i]=exp[i+1]; } exp[expSize-1]='\0'; expSize--; chExp=exp[0]; printf("当前chExp=:%c\n",chExp); } //初始化语法栈 void InitGra() { gra[graSize]='#'; graSize++; gra[graSize]='E'; graSize++; printStep(); } //错误打印 void printError() { printf("\n表达式不匹配!\n"); } //主程序 void main() { printGrammar();//输出文法 GetExp();//获取输入表达式 printHead();//打印题头 InitGra();//初始化语法栈 E(); printf("Recursive Down Analyse App!\n"); } /*(1)E->TG (2)G->+TG|-TG|ε (3)T->FS (4)S->*FS|/FS|ε (5)F->(E)|i */ int E() { popGraStack(); char graE[]="TG"; pushGraStack(graE); strcpy(prod,"E-->TG"); printStep(); T(); G(); return 1; } /*(1)E->TG (2)G->+TG|-TG|ε (3)T->FS (4)S->*FS|/FS|ε (5)F->(E)|i */ int T() { popGraStack(); char graT[]="FS"; pushGraStack(graT); strcpy(prod,"T-->FS"); printStep(); F(); S(); return 1; } /*(1)E->TG (2)G->+TG|-TG|ε (3)T->FS (4)S->*FS|/FS|ε (5)F->(E)|i */ int G() { if(chExp=='+'||chExp=='-') { popGraStack(); char graG[]={chExp,'T','G','\0'}; pushGraStack(graG); strcpy(prod,"G-->"); strcat(prod,graG); printStep(); popGraStack(); nextChar(); strcpy(prod,"匹配"); printStep(); T(); G(); return 1; } else { strcpy(prod,"G-->ε"); printStep(); popGraStack(); strcpy(prod,"匹配"); printStep(); return 1; } } /*(1)E->TG (2)G->+TG|-TG|ε (3)T->FS (4)S->*FS|/FS|ε (5)F->(E)|i */ int F() { if(chExp=='(') { popGraStack(); char graF[]="(E)"; pushGraStack(graF); strcpy(prod,"F-->(E)"); printStep(); popGraStack(); nextChar(); strcpy(prod,"匹配"); printStep(); E(); if(chExp==')') { popGraStack(); nextChar(); strcpy(prod,"匹配"); printStep(); return 1; } else { printError(); return 0; } } else if(chExp=='i') { strcpy(prod,"F-->i"); printStep(); //nextChar(); popGraStack(); nextChar(); strcpy(prod,"匹配"); printStep(); return 1; } else { printError(); return 0; } } /*(1)E->TG (2)G->+TG|-TG|ε (3)T->FS (4)S->*FS|/FS|ε (5)F->(E)|i */ int S() { if(chExp=='*'||chExp=='/') { popGraStack(); char graS[]={chExp,'F','S','\0'}; pushGraStack(graS); strcpy(prod,"S-->"); strcat(prod,graS); printStep(); popGraStack(); nextChar(); strcpy(prod,"匹配"); printStep(); F(); S(); return 1; } else { strcpy(prod,"S-->ε"); printStep(); popGraStack(); strcpy(prod,"匹配"); printStep(); return 1; } }

    转载于:https://www.cnblogs.com/MichaelDongzr/archive/2011/05/21/2052547.html

    展开全文
  • 递归下降是语法分析中最易懂的一种方法。它的主要原理是,对每个非终极符按其产生式结构构造相应语法分析子程序,其中终极符产生匹配命令,而非终极符则产生过程调用命令。因为文法递归相应子程序也递归,所以称...
  • 通过这一次的实验发现,词法分析器做的有很多不严谨的地方,因为词法分析器不应该是一个独立的,而是需要和语法分析器甚至下面的语义相关联,所以在一开始我们就应该对其有一定的规划,但是刚开始的时候没有这个意识...
  • 自定向下预测分析法(LL(1)非递归) 通过之前得学习,可以知道,使用自顶向下得预测分析法,选择产生式时没必要进行回溯,每一步推导仅有一个产生式符合要求,否则就出错。 怎么知道选哪个产生式呢?在进行推导之前...
  • 递归下降分析法

    千次阅读 2013-11-06 15:47:07
    各类参看书都会给的一个例子: LL(0)文法 ... 递归向分析法为每一个非终结符建立相应的子程序,然后模拟语法树自动向下推倒,在推倒过程中遇到终结符则检查是否匹配,遇到非终结符则调用相应的
  • 自顶向下分析方法:递归下降分析法,LL1分析法。其实本质上核心思想是一样的,也就是LL,从左至右,最左推导,因而我觉得其实可以把前一个称为LL0分析法,即不使用向前看符号,这也是他们的不同点,具体实现不同罢了...
  • 编制一个递归下降分析程序,实现对词法分析程序所提供的单词序列的语法检查和结构分析
  • 算术表达式的文法是G[E]: E→E+T| E-T| T T→T*F| T/F| F F→(E)| i 用递归下降分析法按文法G[E]对算术表达式(包括+、-、*、/、()的算术表达式)进行语法分析,判断该表达式是否正确。
  • 1.观察给定语,如果遇到左递归,则需要改写语法来消除左递归 2.根据给定的语法,生成相应符号的First集和Fllow集 3.依照First集和Fllow集实现语法分析器的代码 一、消除左递归: 原本为左递归的语法会使得.....
  • 先调用词法分析程序后将单词保存到一个全局的字符串数组result中,并声明全局int变量ip,指向当前的单词下标,然后调用程序函数,即可完成一步步的递归,最后分析出能否规约为一颗语法树。 下面的代码是语法分析...
  • 递归子程序是一种确定的自顶向下语法分析方法,要求文法是LL(1)文法。它的实现思想是对应文法中每个非终结符编写一个递归过程,每个过程的功能是识别由该非终结符推出的串,当某非终结符的产生式有多个候选式时...

空空如也

空空如也

1 2 3 4 5 ... 16
收藏数 315
精华内容 126
关键字:

递归向下分析法