编译原理实验求助

实验一 词法分析实验(6学时)

[实验目的]
1、了解词法分析的主要任务。
[实验内容]
1、对形如下列的常量说明进行处理。
Const i=10;
j=100;
r=1.23;
c=’good’;
t=true;
[实验要求]
1、从键盘上输入常量说明,最后以问号(或其它符号)作结束标志;
2、处理各常量说明,计算各个常量的值和类型;
3、输出个常量名、常量的值和类型。

程序框架如下,高分请高手帮我写下main函数,要能编译通过

#include <stdio.h>
#include "LJL_Scanner.h"
#include "string.h"

/* 解释执行时使用的栈的最大尺寸。 */
#define stacksize 500

int main()
{

} //main()

int getsym()
{
int i,j,k;

while(cCurChar==' '||cCurChar==10||cCurChar==9) /* 忽略空格、换行和TAB */
{
getchdo;
}

if(cCurChar>='a'&&cCurChar<='z')
{ /* 名字或保留字以a..z开头 */
k=0;
do
{
if(k<al)
{
sTemp[k]=cCurChar;
k++;
}
getchdo;
}
while(cCurChar>='a'&&cCurChar<='z'||cCurChar>='0'&&cCurChar<='9');

sTemp[k]=0;
strcpy(sId,sTemp);
i=0;
j=norw-1;

do /* 搜索当前符号是否为保留字 */
{
k=(i+j)/2;
if(strcmp(sId,sWord[k])<=0)j=k-1;
if(strcmp(sId,sWord[k])>=0)i=k+1;
}
while(i<=j);

if(i-1>j)symCur=wsym[k];
else symCur=ident; /* 搜索失败则,是名字或数字 */

} //if(cCurChar>='a'&&cCurChar<='z')

else //if(cCurChar>='a'&&cCurChar<='z')
{
if(cCurChar>='0'&&cCurChar<='9')
{ /* 检测是否为数字:以0..9开头 */
k=0;
iCurNumber=0;
symCur=number;
do
{
iCurNumber=10*iCurNumber+cCurChar-'0';
k++;
getchdo;
}
while(cCurChar>='0'&&cCurChar<='9'); /* 获取数字的值 */
k--;
if(k>nmax)error(30); /*数字位超长*/

} //if(cCurChar>='0'&&cCurChar<='9')
else
{
if(cCurChar==':') /* 检测赋值符号 */
{
getchdo;
if(cCurChar=='=')
{
symCur=becomes;
getchdo;
}
else
{
symCur=nul; /* 不能识别的符号 */
}
} //if(cCurChar==':')
else
{
if(cCurChar=='<') /* 检测小于或小于等于符号 */
{
getchdo;
if(cCurChar=='=')
{
symCur=leq;
getchdo;
}
else
{
symCur=lss;
}
} //if(cCurChar=='<')

else //if(cCurChar=='<')
{
if(cCurChar=='>') /* 检测大于或大于等于符号 */
{
getchdo;
if(cCurChar=='=')
{
symCur=geq;
getchdo;
}
else
{
symCur=gtr;
}
}
else
{
symCur=ssym[cCurChar];
/* 当符号不满足上述条件时,全部按照单字符符号处理 */
getchdo;
} //if(cCurChar=='>') else

} //if(cCurChar=='<') else

} //if(cCurChar==':') else

} //if(cCurChar>='0'&&cCurChar<='9') else

} //if(cCurChar>='a'&&cCurChar<='z') else

return 0;

} //getsym()
头函数文件,太长放不进来

1)定义
所有token或者叫单词的有限自动机。
2)将有限自动机用代码实现。
3)写分析程序,利用你定义的有限自动机来识别所有的“单词”。并将识别出来的单词的相关信息,如名称,位置,类别等记录在相关的数据结构中。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2007-04-25
我不知道
第2个回答  2007-04-23
还有些函数都没有实现,你怎么做
第3个回答  2007-04-23
这种不动脑又不花钱的学生真没用。
第4个回答  2007-04-26
//.呵呵.C++版.
#include <iostream>
#include <fstream>
#include <stack>
#include <map>
#include <string>

using namespace std;

#define zhengshu 1 //int
#define IF 2 //if
#define ELSE 3 //else
#define shishu 4 //float
#define PRINT 5 //print
#define ID 6 //identify
#define CONSTANT 7 //constant
#define op_fuzhi 8 //=
#define op_add 9 //+
#define op_mul 10 //*
#define op_2star 11 //**
#define div_fenhao 12 //;
#define syl_ls 13 //(
#define syl_rs 14 //)
#define syl_lb 15 //{
#define syl_rb 16 //}
#define sbl_lm 17 //[
#define sbl_rm 18 //]
#define op_sub 19 //-
#define op_div 20 // /
#define div_douhao 21 //,
#define rop_yu 22 //&&
#define op_or 23 //||
#define rop_fei 24 //!
#define rop_equal 25 //==
#define rop_dayu 26 //>
#define rop_xiaoyu 27 //<
#define rop_buxiaoyu 28 //>=
#define rop_budayu 29 //<=
#define rop_uneql 30 //!=
#define TEMP 31
#define NULL 0
#define JMP 32
#define GOTO 33 //goto标识

/*****************************重要数据结构的声明开始*************************/
struct delos
{
int code,value;
}*result; //结果
//变量表
struct analyse
{
int state;
char sign;
};

struct list
{
int value;
list *next;
};

//条件语句的LR(1)分析表,110表示接受,999表示出错
int table[38][20]={
/*0*/{3,999,999,999,999,999,999,999,999,999,999,999,4,999,1,2,999,999,999,999},
/*1*/{999,999,999,999,999,999,999,999,999,999,999,999,999,110,999,999,999,999,999,999},
/*2*/{74,999,999,999,999,999,999,999,999,999,999,999,74,74,999,999,999,999,5,999},
/*3*/{999,6,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999},
/*4*/{999,999,999,999,7,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999},
/*5*/{999,999,999,999,999,999,999,999,999,999,999,999,999,61,999,999,999,999,999,999},
/*6*/{999,12,999,999,999,999,999,9,999,999,999,11,13,999,999,999,8,10,999,999},
/*7*/{999,12,999,999,999,999,999,999,999,999,999,11,13,999,999,999,999,14,999,999},
/*8*/{999,999,15,999,999,16,17,999,999,999,999,999,999,999,999,999,999,999,999,999},
/*9*/{999,12,999,999,999,999,999,9,999,999,999,11,13,999,999,999,18,10,999,999},
/*10*/{999,999,999,999,999,999,999,999,19,20,21,999,999,999,999,999,999,999,999,999},
/*11*/{999,12,999,999,999,999,999,999,999,999,999,11,13,999,999,999,999,22,999,999},
/*12*/{999,12,999,999,999,999,999,999,999,999,999,11,13,999,999,999,999,23,999,999},
/*13*/{999,999,73,73,999,73,73,999,73,73,73,999,999,73,999,999,999,999,999,999},
/*14*/{999,999,999,64,999,999,999,999,999,20,21,999,999,64,999,999,999,999,999,999},
/*15*/{74,999,999,999,999,999,999,999,999,999,999,999,74,74,999,999,999,999,24,999},
/*16*/{74,999,999,999,999,999,999,999,999,999,999,999,74,74,999,999,999,999,999,25},
/*17*/{74,999,999,999,999,999,999,999,999,999,999,999,74,74,999,999,999,999,999,26},
/*18*/{999,999,67,999,999,67,67,999,999,999,999,999,999,999,999,999,999,999,999,999},
/*19*/{999,12,999,999,999,999,999,999,999,999,999,11,13,999,999,999,999,27,999,999},
/*20*/{999,12,999,999,999,999,999,999,999,999,999,11,13,999,999,999,999,28,999,999},
/*21*/{999,12,999,999,999,999,999,999,999,999,999,11,13,999,999,999,999,29,999,999},
/*22*/{999,999,71,71,999,71,71,999,71,71,71,999,999,71,999,999,999,999,999,999},
/*23*/{999,999,30,999,999,999,999,999,999,20,21,999,999,999,999,999,999,999,999,999},
/*24*/{3,999,999,999,999,999,999,999,999,999,999,999,4,999,999,31,999,999,999,999},
/*25*/{999,12,999,999,999,999,999,9,999,999,999,11,13,999,999,999,32,10,999,999},
/*26*/{999,12,999,999,999,999,999,9,999,999,999,11,13,999,999,999,33,10,999,999},
/*27*/{999,999,68,999,999,68,68,999,999,20,21,999,999,999,999,999,999,999,999,999},
/*28*/{999,999,69,69,999,69,69,999,69,69,21,999,999,69,999,999,999,999,999,999},
/*29*/{999,999,70,70,999,70,70,999,70,70,70,999,999,70,999,999,999,999,999,999},
/*30*/{999,999,72,72,999,72,72,999,72,72,72,999,999,72,999,999,999,999,999,999},
/*31*/{999,999,999,75,999,999,999,999,999,999,999,999,999,63,999,999,999,999,999,34},
/*32*/{999,999,65,999,999,65,65,999,999,999,999,999,999,999,999,999,999,999,999,999},
/*33*/{999,999,66,999,999,16,66,999,999,999,999,999,999,999,999,999,999,999,999,999},
/*34*/{999,999,999,35,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999},
/*35*/{74,999,999,999,999,999,999,999,999,999,999,999,74,74,999,999,999,999,36,999},
/*36*/{3,999,999,999,999,999,999,999,999,999,999,999,4,999,999,37,999,999,999,999},
/*37*/{999,999,999,62,999,999,999,999,999,999,999,999,999,62,999,999,999,999,999,999}
};

/*****************************重要数据结构的声明结束*************************/

/*********************************全局变量声明开始**************************/
int place=1;
int nextpos=1;
stack<list *> stknext;
stack<list *> stktrue;
stack<list *> stkfalse;
stack<int> stkpos;
stack<delos> stktemp;//常量,变量,临时变量
delos temp;
delos gen[50][4];//生成的三地址

string str[31]={"","int","if","else","float","print","标识符","常数",
"=","+","*","**",";","(",")",
"{","}","[","]","-","/",",","&&","||","!",
"==",">","<",">=","<=","!="};
//变量
string *var;
int varlen=0,nowvar=1;
//常量
float *myconst;
int constlen=0,nowconst=1;
int resultlen=0,nowresult=0;

/*********************************全局变量声明结束**************************/

void renewresult()
{
delos *p3=result;
int i;
resultlen+=10;
result=new delos[resultlen];
for(i=0;i<resultlen;i++)
{ result[i].code=p3[i].code;
result[i].value=p3[i].value;
}
delete[] p3;
}

void renewvar()
{
string *p1=var;
int i;
varlen+=10;
var=new string[varlen];
for(i=0;i<nowvar;i++)
var[i]=p1[i];
delete[] p1;
}

void renewconst()
{
float *p2=myconst;
int i;
constlen+=10;
myconst=new float[constlen];
for(i=0;i<nowconst;i++)
myconst[i]=p2[i];
delete[] p2;
}

bool isletter(char c) //判别是否字母
{
if(c>64&&c<91||c>96&&c<123)
return true;
return false;
}

bool isdigital(char c) //判别是否数字
{ if(c>47&&c<58)
return true;
return false;
}

int reserve(char c[],int i)
{
string s(c,0,i);
for(int j=1;j<7;j++)
if(s==str[j])
return j;
return 0;
}

void insertresult(int code,int value)
{ if(nowresult>resultlen)
renewresult();
result[nowresult].code=code;
result[nowresult++].value=value;
}

void insertid(char c[],int i)
{ string s(c,0,i);
insertresult(ID,nowvar);
if(nowvar>varlen)
renewvar();
var[nowvar++]=s;
}
//插入常数,为浮点型
void insertconst(char c[],int i)
{ int d=0,j;
float a=0,b=1;
while(c[d]!='.'&&d<i)
d++;

for(j=d-1;j>=0;j--)
{ a=a+(c[j]-48)*b;
b=b*10;
}

b=10;
for(j=d+1;j<i;j++)
{a=a+(c[j]-48)/b;
b=b*10;
}
insertresult(CONSTANT,nowconst);
if(nowconst>constlen)
renewconst();
myconst[nowconst++]=a;
}

/**********************************词法分析函数开始***********************/
void wordanalyse()
{
char strtoken[10];
int i=0,code;
char ch;
ifstream myfile;
myfile.open("sourcefile.txt");
if(!myfile)
{ cout<<"Can not open input file !"<<endl;
return;
}

while(!myfile.eof())
{ i=0;
for(ch=myfile.get();ch==' '||ch==13||ch==10;ch=myfile.get())
;
if(isletter(ch))
{while(isletter(ch)||isdigital(ch))
{strtoken[i++]=ch;
ch=myfile.get();
}
myfile.seekg(-1,ios::cur);
code=reserve(strtoken,i);
if(code==0)
insertid(strtoken,i);
else
{insertresult(code,0);
}
}
else if(isdigital(ch))
{while(isdigital(ch)||ch=='.')
{strtoken[i++]=ch;
ch=myfile.get();
}
myfile.seekg(-1,ios::cur);
insertconst(strtoken,i);
}
else if(ch=='=')
{ ch=myfile.get();
if(ch=='=')
insertresult(rop_equal,0);
else
{insertresult(op_fuzhi,0);
myfile.seekg(-1,ios::cur);
}
}
else if(ch=='+')
{insertresult(op_add,0);
}
else if(ch=='*')
{ ch=myfile.get();
if(ch=='*')
insertresult(op_2star,0);
else
{insertresult(op_mul,0);
myfile.seekg(-1,ios::cur);
}
}
else if(ch==';')
{ insertresult(div_fenhao,0);
}
else if(ch=='(')
{insertresult(syl_ls,0);
}
else if(ch==')')
{insertresult(syl_rs,0);
}
else if(ch=='{')
{ insertresult(syl_lb,0);
}
else if(ch=='}')
{ insertresult(syl_rb,0);
}
else if(ch=='[')
{ insertresult(sbl_lm,0);
}
else if(ch==']')
{ insertresult(sbl_rm,0);
}
else if(ch=='-')
{ insertresult(op_sub,0);
}
else if(ch=='/')
{ insertresult(op_div,0);
}
else if(ch==',')
{ insertresult(div_douhao,0);
}
else if(ch=='&')
{ ch=myfile.get();
if(ch=='&')
insertresult(rop_yu,0);
else
{
myfile.seekg(-1,ios::cur);
myfile.get(strtoken,10);
cout<<"ERROR :"<<strtoken<<endl;
}
}

else if(ch=='|')
{ ch=myfile.get();
if(ch=='|')
insertresult(op_or,0);
else
{
myfile.seekg(-1,ios::cur);
myfile.get(strtoken,10);
cout<<"ERROR :"<<strtoken<<endl;
}
}
else if(ch=='!')
{ ch=myfile.get();
if(ch=='=')
insertresult(rop_uneql,0);
else
{insertresult(rop_fei,0);
myfile.seekg(-1,ios::cur);
}
}
else if(ch=='>')
{ ch=myfile.get();
if(ch=='=')
insertresult(rop_buxiaoyu,0);
else
{insertresult(rop_dayu,0);
myfile.seekg(-1,ios::cur);
}
}
else if(ch=='<')
{ ch=myfile.get();
if(ch=='=')
insertresult(rop_budayu,0);
else
{insertresult(rop_xiaoyu,0);
myfile.seekg(-1,ios::cur);
}
}
else
{if(ch!=-1)
{myfile.seekg(-1,ios::cur);
myfile.get(strtoken,10);
cout<<"ERROR :"<<strtoken<<endl;
myfile.seekg(1,ios::cur);
}
}
}
myfile.close();
cout<<"词法分析成功啦!!"<<endl;
}
/**********************************词法分析函数结束***********************/本回答被提问者和网友采纳
相似回答