📄 新建 文本文档 (3).txt
字号:
对C++进行词法和语法分析。
要求:
词法分析部分写出相应的正规集、正规式、NFA、DFA
写出语法分析所采用的方法和完成的语法分析功能
编写出响应的编译程序
写出完整的课程设计报告
说明:课程设计报告包括的基本内容有:
一、课程设计题目
二、课程设计的目的
三、课程设计的基本内容和实现功能介绍
四、词法分析:包括系统的词法规则,相应的正规集、正规式、NFA、DFA
五、语法分析:包括语法规则,分析所采用的技术和算法
0
我要推荐
作者:tyu
专家分:20
会员信息
发短消息
所属BLOG
发表时间:2005-6-7 22:43:00 [回复] [引用]
1 楼
不是吧
你大几了
要搞这个
此帖尚未评分
作者:compiler
专家分:80
会员信息
发短消息
所属BLOG
发表时间:2005-6-9 23:30:00 [回复] [引用]
2 楼
前些日子写的LL(1)语法分析器。可以构造非递归的FIRST和FOLLOW集还有预测分析表。希望对你有帮助。好运。
此帖尚未评分
作者:compiler
专家分:80
会员信息
发短消息
所属BLOG
发表时间:2005-6-9 23:31:00 [回复] [引用]
3 楼
// Using STL
// C++ forever ...
# include <iostream>
# include <stdlib.h>
# include <list>
# include <string>
using namespace std;
class elem
{
public:
string name;
list<string> first; // first set
list<string> follow; // follow set
};
const int MAXSIZE = 16; // maxium terminal symbol and maxium no-terminal symbol
const int MAXLINE = 32; // maxium lines of the input grammer
extern int lineno; // record how many lines
extern int colineno;
const string ee = "e"; // the empty string
extern bool isLL1Grammer; // judge whether a given grammer is a LL(1) grammer
extern elem symbol[MAXSIZE]; // to hold first and follow set for each symbol
extern string LL1_table[MAXSIZE][MAXSIZE]; // only for showing non-empty elements
extern list<string> production[MAXLINE]; // for getting grammer from standard input stream
extern list<string> coProduction[MAXLINE];
extern list<string> VN; // hold no-terminal symbols
extern list<string> VT; // hold terminal symbols
extern list<string> First;
extern list<string> Follow;
/* ---------------- function declaration --------------------- */
void getGrammer();
void getCoGrammer();
bool isLeftRecursion();
void emitLeftRecursion(); // for emiting later...
void print(list<string>& ); // to print structure
void removeDup(list<string>& ); // to remove duplicate
void getVN(); // get set of no-terminals
void getVT(); // get set of terminals
bool isIn(string,list<string>&); // judge if a given string is in a list
void listToString(string& ,list<string> ); // copy list to a string
void noRightReserve(); // for printing message
void getName(); // get the name of symbol
void buildFirst(); // build FIRST set
void printFirst(); // print FIRST of each no-terminals
void buildFollow(); // build FOLLOW set
void printFollow(); // print FOLLOW of each non-terminals
void initTable(); // inisialize the LL1 table
void buildTable(); // build LL1 table ( using some algorithm )
void printTable(); // print the LL(1) table
/* ---------------------------------------------------------- */
此帖尚未评分
作者:compiler
专家分:80
会员信息
发短消息
所属BLOG
发表时间:2005-6-9 23:31:00 [回复] [引用]
4 楼
# include "global.h"
using namespace std;
int main()
{
//////////// get grammer for standard stream
getGrammer();
getCoGrammer();
//////// emit_leftRecursion.cpp ////////////////
if(isLeftRecursion()){
cout << "***********************************" << endl;
cout << "This version cannot deal with left recursion grammer." <<endl;
cout << "The version will coming after handing in my Java " << endl;
cout << "homework :( . @ Math Frog " << endl;
cout << "***********************************" << endl;
}
else{
//////////// getVnVt.cpp ////////////////////////
getVN();
getVT();
/////////////////////////////////////////////////
/// constructFirstAndFollow.cpp ////////////////
buildFirst();
printFirst();
buildFollow();
printFollow();
////////////////////////////////////////////////
//////////////// bottomtoUpAnylysis.cpp ////////
initTable();
buildTable();
printTable();
}
/////////////// tools.cpp /////////////////////
system("PAUSE");
noRightReserve();
system("PAUSE");
return 0;
}
此帖尚未评分
作者:compiler
专家分:80
会员信息
发短消息
所属BLOG
发表时间:2005-6-9 23:31:00 [回复] [引用]
5 楼
# include "global.h"
/*-----------------------------------------
To get grammer from the standard input
stream.
avoid file processing :P
------------------------------------------*/
string choice; // for user's decision
int lineno = 0;
list<string> production[MAXLINE];
void showMessage()
{
cout << " LL(1) parsing machine @ Math Frog && FLower @ " << endl;
cout << endl;
cout << " ** Please input the grammer you want to process ** \n";
cout << "\n ** REMIND : Just type a space after entering a symbol **";
cout << "\n ** press # to end inputing production **";
cout << "\n ** press $ to end inputing grammer **" << endl;
cout << "***********************************" << endl;
}
void getGrammer()
{
while(true){
showMessage();
string tmp; // holding input string temporily
for(;lineno < MAXLINE;){
cout << "line " << lineno << ": ";
cin >> tmp;
while(tmp != "#" && tmp != "$"){
production[lineno].push_back(tmp);
cin >> tmp;
}
if(tmp == "$") break; // end inputing grammer
lineno++;
}
cout << "***********************************" << endl;
cout << endl;
for(int j = 0;j < lineno;j++)
print(production[j]);
cout << endl;
cout << "***********************************" << endl;
cout << endl;
cout << "\nIs the grammer you want to anlysis ? " << endl;
cout << endl;
cout << "(Type Y for yes and N for no)" << endl;
cin >> choice;
if(choice == "Y")
break;
for(int i = 0;i < lineno;i++) // everything just begins
production[i].erase(production[i].begin(),production[i].end());
lineno = 0;
}
}
void getCoGrammer()
{
list<string> hold;
for(int line = 0;line < lineno;line++){
list<string>::iterator iter = production[line].begin();
iter++; // to the ->
iter++; // to the first on the right of the production
while(iter != production[line].end()){
hold.push_back(production[line].front());
hold.push_back("->");
if(iter == --production[line].end())
hold.push_back(*iter);
else{
while(*iter != "|" && iter != production[line].end()){
hold.push_back(*iter);
if(iter == --production[line].end()) break;
iter++;
}
}
coProduction[colineno++] = hold; // just use the overloaded =
hold.erase(hold.begin(),hold.end()); // make hold empty list
iter++;
}
}
}
此帖尚未评分
作者:compiler
专家分:80
会员信息
发短消息
所属BLOG
发表时间:2005-6-9 23:32:00 [回复] [引用]
6 楼
# include "global.h"
// import
list<string> VN;
list<string> VT;
void getVN()
{
for(int i = 0;i < lineno;i++)
VN.push_back(*(production[i].begin()));
if(!VN.empty()){
removeDup(VN); // remove duplicated elements
cout << "***********************************" << endl;
cout << "** The set of non-ternminal symbols of this grammer is as follow ** \n";
cout << "***********************************" << endl;
cout << endl;
print(VN);
cout << endl;
cout << "***********************************" << endl;
}
}
void getVT()
{
string tmp;
list<string>::iterator point,hold;
for(int i = 0;i < lineno;i++){
point = production[i].begin();
while(*point != "->")
point++;
hold = ++point;
while(hold != production[i].end()){
tmp = *hold;
if(tmp != "|" && !isIn(tmp,VN) && tmp != "e")
VT.push_back(*hold); // push to the set of VT
hold++;
}
}
if(!VT.empty()){
removeDup(VT);
cout << "***********************************" << endl;
cout << "** The set of terminal symbols of this grammer is as follow ** \n";
cout << "***********************************" << endl;
cout << endl;
print(VT);
cout << endl;
cout << "***********************************" << endl;
cout << endl;
}
}
此帖尚未评分
作者:compiler
专家分:80
会员信息
发短消息
所属BLOG
发表时间:2005-6-9 23:32:00 [回复] [引用]
7 楼
/*
Some tool functions
using very simple linear algorithm
*/
# include "global.h"
void print(list<string>& l)
{
list<string>::iterator iter = l.begin();
while(iter != l.end())
cout << *iter++ << " ";
cout << endl;
}
void removeDup(list<string>& l)
{
string hold;
list<string>::iterator sIter,pIter;
pIter = l.begin();
while(pIter != l.end()){
hold = *pIter;
sIter = pIter;
sIter++;
while(sIter != l.end())
if(*sIter == hold)
l.erase(sIter++);
else sIter++;
pIter++;
}
}
bool isIn(string target,list<string>& s) // to decide whether a symbol is a terminal symbol or
// non-terminal symbol
{ // or for something judgement in later procession
// sequencial search , only for small instance :)
// I am not quite sophlicated in algorithm
list<string>::iterator iter = s.begin();
while(iter != s.end()){
if(*iter == target)
return true;
iter++;
}
return false;
}
void listToString(string& s,list<string> l)
{
// the string has fixed ...
list<string>::iterator iter;
for(iter = l.begin();iter != l.end();iter++){
s.append(*iter); // append to the string
s.append(" ");
}
}
void noRightReserve()
{
// holding for later usage
cout << "***********************************" << endl;
cout << "* LL(1) parsing version 0.5 " << endl;
cout << "***********************************" << endl;
cout << "\n\n\n*Me aNd FlOwEr ArE LiViG HaPPiLY Now\n";
cout << "* AnD HoW MuCh We LoVe ThIs WoRlD\n";
cout << "* Mathematics Worm Association" << endl;
cout << " * Math Frog 6.8.2005 " << endl;
cout << " @ No Right Reserve" << endl;
cout << "***********************************" << endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -