⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parse.h

📁 输入一个DO-WHILE的语句,进行词法分析,词法分析器利用超前搜索,状态转换等方法,将源程序转化成为一个一个的单词符号二元式,如果词法分析无误,则进入语法分析部分,使用简单优先法进行文法分析,为每个
💻 H
字号:

#ifndef PARSE_H
#define PARSE_H

#include "Global.h"
#include "Stack.h"
#include "Prece.h"

/*
    Print the error message     */
void error()
{
    printf("The input string doesn't match the grammar!");
}

void succeed()
{
    printf("Succeed! The input match the grammar.");
}

/*
    Reduce terminals or nonterminals to a nonterminals  */
char Reduce(const char* c)
{
    if (*c == 'd')
    {
        return 'S';
    }
    else if (*c == 'c' )
    {
        return 'B';
    }
    else if (*c == 'a')
    {
        return 'E';
    }
}

/*
    the core algorithm of this project      	*/
bool Parse()
{
    char c;     /* Store the top terminal of the stack      */
    char t;     /* Store the most recently popped terminal  */
    int  ip = 0;    /* The index pointing to the input buffer   */
    InitStack();
    while (true)
    {
        TopValue(&c);
        if (c=='S' && Buff[ip]=='#')
        {
	    succeed();
            return true;
        }
        else
        {
            switch (GetPrecedence(&c, &Buff[ip]))
            {
                case LT:
                case EQ:
                {
                    Push(&Buff[ip++]);
		            PrintStack();
                    break;
                }
                case GT:
                {                    	
                    Pop(&c);
                    do
                    {
                        t = c;
                        Pop(&c);
            	    }
                    while (GetPrecedence(&c, &t) != LT);
                    Push(&c);
                    t = Reduce(&t);
                    Push(&t);
            	    PrintStack();
                    break;
                }
                default:
                {
                    error();
                    return false;
                }
            }
        }
    }
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -