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

📄 parse.h

📁 是一个不错的语法分析器,里面实现了语法分析的算法,并判断了一些优先级的控制和对栈的操作.
💻 H
字号:
/************************************************************

  Title:    Operator-Precedence Parsing
  Author:   Dake Song, Class 0201 of Computer Science
  Date:     Mar.18 of 2005

**************************************************************/

#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 == 'b')
    {
        return 'S';
    }
    else if (*c == '(' || *c == 'a')
    {
        return 'A';
    }
    else if (*c == 'A')
    {
        return 'P';
    }
}

/*
    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 + -