📄 parse.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 + -