资源简介
目的:熟练掌握自下而上的语法分析方法,并能用程序实现。
要求:
1. 使用如下文法:
E E+T | T
T T*F | F
F (E) | id
2. 对于任意给定的输入串(词法记号流)进行语法分析,要求采用LR分析器来完成。手工构造LR分析表,利用移进-归约分析算法(P69 图3.12)输出(P70 表3.8)对应的动作部分。如:
输入:id*+id/(id+id)#
输出:移进
按 F->id归约
移进
error
……
3. 要有一定的错误处理功能。即对错误能提示,并且能在一定程度上忽略尽量少的记号来进行接下来的分析。
例如:
从状态0开始的记号流为:bm
将b移进之后,栈里的情况应该为: 0 b 2
此时查表发现 action[2,m]=error
输出打印:error
把A和状态1相继压入栈,用户指针后移到FOLLOW(A)对应的元素继续分析。
代码片段和文件信息
#include
#include
#include
#include
using namespace std;
class getstring
{
public:list istream;
stack label;
bool flag;
list::iterator getite(int i)
{
int j=0;
list::iterator it1=istream.begin();
while(j!=i)
{
j++;it1++;
}
return it1;
}
void getis()
{
string s1s2;
cout<<“请输入表达式:“< cin>>s1;
for(int i=0;i {
if(s1[i]==‘+‘||s1[i]==‘*‘||s1[i]==‘(‘||s1[i]==‘)‘)
{
s2.push_back(s1[i]);istream.push_back(s2);s2.clear();
}
else if(s1[i]==‘i‘)
{
s2.push_back(s1[i]);i++;
if(s1[i]==‘d‘){s2.push_back(s1[i]);istream.push_back(s2);s2.clear();}
else{error();}
}
else if(s1[i]==‘\0‘)
{
continue;
}
else
{error();}
}
istream.push_back(“$“);flag=false;
}
void error()
{
cout<<“错误!“< exit(0);
}
void test()
{
if(label.empty()||istream.empty()){exit(0);}
}
void gototable(string s1string s2)
{
if(s1==“0“&&s2==“E“)
{
label.push(“1“);
}
if((s1==“0“||s1==“4“)&&s2==“T“)
{
label.push(“2“);
}
if((s1==“0“||s1==“4“||s1==“6“)&&s2==“F“)
{
label.push(“3“);
}
if(s1==“4“&&s2==“E“)
{
label.push(“8“);
}
if(s1==“6“&&s2==“T“)
{
label.push(“9“);
}
if(s1==“7“&&s2==“F“)
{
label.push(“10“);
}
}
void analyse()
{
label.push(“0“);
string stoplistops1;
while(!(label.empty()||istream.empty())&&!(label.top()==“1“&&istream.front()==“$“))
{
stop=label.top();listop=istream.front();
if((stop==“0“||stop==“4“||stop==“6“||stop==“7“)&&listop==“id“)
{
label.push(“id“);label.push(“5“);istream.pop_front();cout<<“移进“< }
if((stop==“0“||stop==“4“||stop==“6“||stop==“7“)&&listop==“(“)
{
label.push(“(“);label.push(“4“);istream.pop_front();cout<<“移进“< }
if((stop==“1“||stop==“8“)&&listop==“+“)
{
label.push(“+“);label.push(“6“);istream.pop_front();cout<<“移进“< }
if((stop==“2“||stop==“9“)&&listop==“*“)
{
label.push(“*“);label.push(“7“);istream.pop_front();cout<<“移进“< }
if(stop==“8“&&listop==“)“)
{
label.
相关资源
- 国际象棋的qt源代码
- C++中头文件与源文件的作用详解
- C++多线程网络编程Socket
- VC++ 多线程文件读写操作
- 利用C++哈希表的方法实现电话号码查
- 移木块游戏,可以自编自玩,vc6.0编写
- C++纯文字DOS超小RPG游戏
- VC++MFC小游戏实例教程(实例)+MFC类库
- 编译原理c语言编译器
- 编译原理实验-词法分析(c语言代码)
- 连铸温度场计算程序(C++)
- 6自由度机器人运动学正反解C++程序
- Em算法(使用C++编写)
- libstdc++-4.4.7-4.el6.i686.rpm
- VC++实现CMD命令执行与获得返回信息
- 白话C++(全)
- C++标准库第1、2
- 大数类c++大数类
- C++语言编写串口调试助手
- c++素数筛选法
- C++ mqtt 用法
- 商品库存管理系统 C++ MFC
- c++ 多功能计算器
- C++17 In Detail
- 嵌入式QtC++编程课件
- 颜色识别形状识别STM103嵌入式代码
- c++ 邮件多附件群发
- c++ 透明代理(hookproxy)
- mfc 调用redis
- FTP客户端源码(c++)
评论
共有 条评论