//
// main.cpp
// cifa
//
// Created by 杨毅帆 on 2017/5/18.
// Copyright © 2017年 杨毅帆. All rights reserved.
//
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream productionfile("/Users/YangYifan/归档/production.cpp");
ifstream firstfile("/Users/YangYifan/归档/first.cpp");
ifstream followfile("/Users/YangYifan/归档/follow.cpp");
ifstream fomulafile("/Users/YangYifan/归档/fomula.cpp");
class line{
public:
char a[100];
int n;
line()
{
n = 0;
}
int findline(char x)
{
for(int i = 0 ; i < n ; i++)
if(a[i] == x)
return i;
return -1;
}
void input(char x)
{
if(findline(x) == -1)
{
a[n] = x;
n++;
}
}
};
line l;
class list{
public:
string key;
string value[100];
int n;
list()
{
n = 0;
}
void setkey(string k)
{
key = k;
}
void input(string k,int i)
{
value[i] = k;
n++;
}
void input(string k)
{
value[n] = k;
n++;
}
string getvalue(int n)
{
return value[n];
}
int findvalue(string k)
{
for(int i = 0 ;i < 100 ; i++)
if(k == value[i])
return i;
return -1;
}
};
class production{
public:
list List[100];
int n;
production()
{
n = 0;
}
int findkey(string k)
{
for(int i = 0 ; i < n ; i++)
if(List[i].key == k)
return i;
return -1;
}
void input(string k,string t)
{
if(findkey(k) == -1)
{
List[n].setkey(k);
List[n].input(t);
n++;
}
else
{
List[findkey(k)].input(t,List[findkey(k)].n);
}
}
int findproduction(string k)
{
for(int i = 0 ;i < n ;i++)
if(List[i].findvalue(k) != -1)
return i;
return -1;
}
string getkey(int i)
{
return List[i].key;
}
};
production pro;
class matrix{
public:
list List[100];
int n;
matrix()
{
n = 0;
}
int findkey(string k)
{
for(int i = 0; i < n;i++)
if(List[i].key == k)
return i;
return -1;
}
void input(string v,string k,char t)
{
if(findkey(v) == -1)
{
List[n].key = v;
l.input(t);
List[n].value[l.findline(t)] = k;
n++;
}
else
{
l.input(t);
List[findkey(v)].value[l.findline(t)] = k;
}
}
string getproduction(string k,char c)
{
l.input(c);
if(findkey(k) == -1)
return NULL;
return List[findkey(k)].value[l.findline(c)];
}
};
matrix rule;
void setstatment()
{
string ver;
string target;
while(firstfile>>ver)
{
string sentence;
if(pro.findkey(ver) != -1)
{
target = ver;
getline(firstfile,sentence);
continue;
}
if(ver == "@")
{
getline(firstfile,sentence);
while(followfile>>ver)
{
getline(followfile,sentence);
if(ver == target)
{
int i = 0;
while(sentence[i] != '}')
{
if((sentence[i] != '{')&&(sentence[i] != ',')&&(sentence[i] != ' '))
{
rule.input(target, "@", sentence[i]);
}
i++;
}
break;
}
}
}
else
{
getline(firstfile,sentence);
int i = 0;
while(sentence[i] != '}')
{
if((sentence[i] != '{')&&(sentence[i] != ',')&&(sentence[i] != ' '))
{
rule.input(target, ver, sentence[i]);
}
i++;
}
}
}
}
class stack{
public:
char a[100];
char *base;
char *top;
stack()
{
base = a;
top = base;
}
void push(string k)
{
for(int i = (int)k.size()-1; i > -1;i--)
{
if(k[i] != '\'')
{
*top = k[i];
top++;
}
else
{
*top = k[i-1];
top++;
*top = k[i];
top++;
i--;
}
}
}
bool isEmpty()
{
if(top == base)
return true;
else
return false;
}
string pop()
{
if(!isEmpty())
{
top --;
if(*top == '\'')
{
top--;
string a = top;
return a.substr(0,2);
}
else
{
string a = top;
return a.substr(0,1);
}
}
else
{
return "";
}
}
string getsurplus()
{
string a;
for(int i = 0;i< top-base;i++)
{
if(*(base+i) != '@')
a += *(base+i);
}
return a;
}
};
stack analysis;
void show(int n,string a,string b,string c)
{
printf("%-7d",n);
int aa = (int)a.size();
int bb = (int)b.size();
int cc = (int)c.size();
cout<<a;
for(int i = 15-aa;i>-1;i--)
cout<<" ";
cout<<b;
for(int i = 15-bb;i>-1;i--)
cout<<" ";
cout<<c;
for(int i = 15-cc;i>-1;i--)
cout<<" ";
cout<<endl;
}
void showfore(string a,string b,string c,string d)
{
int aa = (int)b.size();
int bb = (int)c.size();
int cc = (int)d.size();
cout<<a;
cout<<b;
for(int i = 15-aa;i>-1;i--)
cout<<" ";
cout<<c;
for(int i = 15-bb;i>-1;i--)
cout<<" ";
cout<<d;
for(int i = 15-cc;i>-1;i--)
cout<<" ";
cout<<endl;
}
bool isIllegal(string k)
{
for(int i = 0;i < k.size();i++)
{
if(l.findline(k[i]) == -1)
return true;
}
return false;
}
string makeProduction(string a,string b)
{
return a+"->"+b;
}
void aq(string k)
{
if(isIllegal(k))
{
cout<<k<<" 不是合法的句子"<<endl;
return;
}
k += "#";
analysis.push("#");
analysis.push(pro.List[0].key);
string stackchar;
string fomulachar;
int i = 0;
int j = 1;
showfore("次数 ","分析栈 ", "输入串 ", "所用产生式");
cout<<endl;
show(0,analysis.getsurplus(),k,"");
while(1)
{
stackchar = analysis.pop();
if(stackchar == "@")
stackchar = analysis.pop();
fomulachar = k[i];
if(stackchar != fomulachar && stackchar != "#")
{
analysis.push(rule.getproduction(stackchar, fomulachar[0]));
show(j,analysis.getsurplus(),k.substr(i,k.size()-i),makeProduction(stackchar,rule.getproduction(stackchar, fomulachar[0])));
}
else if(stackchar == fomulachar && stackchar != "#")
{
i++;
show(j,analysis.getsurplus(),k.substr(i,k.size()-i),"");
}
else if(stackchar == "#" && fomulachar == "#")
break;
else if(stackchar == "#" && fomulachar != "#")
{
cout<<k.substr(0,k.size()-1)<<" 不是文法的句子"<<endl;
return;
}
else if(j > 100)
cout<<"error!"<<endl;
j++;
}
}
int main()
{
string ver;
while(productionfile>>ver)
{
l.input('i');
l.input('+');
l.input('*');
l.input('(
没有合适的资源?快使用搜索试试~ 我知道了~
cifa.zip_C++
共26个文件
commit_editmsg:1个
description:1个
index:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 80 浏览量
2022-09-24
09:13:07
上传
评论
收藏 30KB ZIP 举报
温馨提示
一个用c++编写的简单词法分析实现,供初学者学习参考。
资源推荐
资源详情
资源评论
收起资源包目录
cifa.zip (26个子文件)
cifa
cifa.xcodeproj
project.xcworkspace
contents.xcworkspacedata 149B
xcuserdata
YangYifan.xcuserdatad
UserInterfaceState.xcuserstate 13KB
project.pbxproj 8KB
xcuserdata
YangYifan.xcuserdatad
xcschemes
cifa.xcscheme 3KB
xcschememanagement.plist 476B
.git
index 884B
hooks
README.sample 177B
config 137B
description 73B
refs
tags
heads
objects
05
f866521933202af9baa67502efac6253884817 2KB
89
b97628c959d5f14f407141cd20df673b9ad618 301B
97
27217cab3012ef6fb13b64839eb4d489717b2c 820B
info
cc
f7f73d15a7d37f4b6e5124de10be3e29fd127a 130B
57
f5cef895e05293c5067ef084de767eb3a52a36 131B
a9
61ba22c994eac38d2d97330f3bd78e91e93ce3 69B
pack
1c
298b238f979bab76b8cd406688c53960fc2aed 63B
49
5c0dd0fa160f3cf8f3c2ca7807a717bddbe86e 99B
56
afd3d4c46bef75018a3a0d8373abb2d994c38a 51B
26
e44c503127917402ba68f0dc684999f98df150 230B
5f
b29d109dabf25e4f27f03ff8538487683a9adb 80B
67
8e54dee23c2f5c7fec77dbdf0ff028600bae0b 53B
info
exclude 40B
COMMIT_EDITMSG 15B
HEAD 23B
.DS_Store 6KB
cifa
main.cpp 8KB
共 26 条
- 1
资源评论
朱moyimi
- 粉丝: 65
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功