编译原理词法分析程序

编译原理词法分析程序,第1张

#include <iostream>

#include <ctypeh>

#include <fstream>

#include <stringh>

#include <malloch>

using namespace std;

ifstream fp("sourcetxt",ios::in);

char cbuffer;

char key[13]={"if","else","for","while","do","return","break","continue","int","void"

,"main","const","printf"}; //关键字

char border[7]={ "," , ";" , "{" , "}" , "(" , ")" ,"//"}; //分界符

char arithmetic[6]={"+" , "-" , "" , "/" , "++" , "--"}; //运算符

char relation[7]={"<" , "<=" , "=" , ">" , ">=" , "==" ,"!="}; //关系运算符

char lableconst[80]; //标识符

int constnum=40;

int lableconstnum=0;

int linenum=1; //统计常数和标识符数量

int search(char searchchar[],int wordtype)

{

int i=0,t=0;

switch (wordtype)

{

case 1:

{ for (i=0;i<=12;i++) //关键字

{

if (strcmp(key[i],searchchar)==0)

return(i+1);

}

return(0);}

case 2:

{

for (i=0;i<=6;i++) //分界符

{

if (strcmp(border[i],searchchar)==0)

return(i+1);

}

return(0);

}

case 3:

{

for (i=0;i<=5;i++) //运算符

{

if (strcmp(arithmetic[i],searchchar)==0)

return(i+1);

}

return(0);

}

case 4:

{

for (i=0;i<=6;i++) //关系运算符

{

if (strcmp(relation[i],searchchar)==0)

return(i+1);

}

return(0);

}

case 5:

{

for (t=40;t<=constnum;t++) //常数

{

if (strcmp(searchchar,lableconst[t])==0)//判断该常数是否已出现过

return(t+1);

}

lableconst[t-1]=(char )malloc(sizeof(searchchar));//为新的元素分配内存空间

strcpy(lableconst[t-1],searchchar);//为数组赋值lableconst指针数组名

constnum++; //常数个数自加

return(t);

}

case 6:

{

for (i=0;i<=lableconstnum;i++)

{

if (strcmp(searchchar,lableconst[i])==0) //判断标识符是否已出现过

return(i+1);

}

lableconst[i-1]=(char )malloc(sizeof(searchchar));

strcpy(lableconst[i-1],searchchar);

lableconstnum++; //标识符个数自加

return(i);

}

default:cout<<"错误!";

}

}

char alphaprocess(char buffer) //字符处理过程

{

int atype;

int i=-1;

char alphatp[20];

while ((isalpha(buffer))||(isdigit(buffer)))

//这两个函数分别是判字符和判数字函数位于ctypeh中

{

alphatp[++i]=buffer;

fpget(buffer);

}

alphatp[i+1]='\0';//在末尾添加字符串结束标志

if (atype=search(alphatp,1))

cout<<"linenum: "<<linenum<<" String= "<<alphatp<<"\t\t\t"<<"关键字"<<endl;

else

{

atype=search(alphatp,6); //标识符

cout<<"linenum: "<<linenum<<" String= "<<alphatp<<"\t\t\t"<<"标识符"<<endl;

}

return(buffer);

}

char digitprocess(char buffer) //数字处理过程

{

int i=-1;

char digittp[20];

int dtype;

while ((isdigit(buffer)))

{

digittp[++i]=buffer;

fpget(buffer);

}

digittp[i+1]='\0';

dtype=search(digittp,5);

cout<<"linenum: "<<linenum<<" String= "<<digittp<<"\t\t\t"<<"数据"<<endl;

return(buffer);

}

char otherprocess(char buffer) //分界符、运算符、逻辑运算符、等

{

int i=-1;

char othertp[20];

int otype,otypetp;

othertp[0]=buffer;

othertp[1]='\0';

if (otype=search(othertp,3))

{

fpget(buffer);

othertp[1]=buffer;

othertp[2]='\0';

if (otypetp=search(othertp,3)) //判断该运算符是否是

//由连续的两个字符组成的

{

cout<<"linenum: "<<linenum<<" String= "<<othertp<<"\t\t\t"<<"运算符"<<endl;

fpget(buffer);

goto out;

}

else //单字符逻辑运算符

{

othertp[1]='\0';

cout<<"linenum: "<<linenum<<" String= "<<othertp<<"\t\t\t"<<"逻辑运算符"<<endl;

goto out;

}

}

if (otype=search(othertp,4)) //关系运算符

{

fpget(buffer);

othertp[1]=buffer;

othertp[2]='\0';

if (otypetp=search(othertp,4)) //判断该关系运算符是否是

//由连续的两个字符组成的

{

cout<<"linenum: "<<linenum<<" String= "<<othertp<<"\t\t\t"<<"关系运算符"<<endl;

fpget(buffer);

goto out;

}

else //单字符逻辑运算符

{

othertp[1]='\0';

cout<<"linenum: "<<linenum<<" String= "<<othertp<<"\t\t\t"<<"逻辑运算"<<endl;

goto out;

}

}

if (buffer=='!') //"=="的判断

{

fpget(buffer);

if (buffer=='=')

//cout<<"!= (2,2)\n";

fpget(buffer);

goto out;

}

else

{

if (otype=search(othertp,2)) //分界符

{

cout<<"linenum: "<<linenum<<" String= "<<othertp<<"\t\t\t"<<"分界符"<<endl;

fpget(buffer);

goto out;

}

}

if ((buffer!='\n')&&(buffer!=' '))

cout<<"错误!,字符非法"<<"\t\t\t"<<buffer<<endl;

fpget(buffer);

out: return(buffer);

}

void main()

{

int i;

for (i=0;i<=50;i++)

{

lableconst[i]=" ";//用于保存标识符

}

if (!fp)

cout<<"文件打开错误!!"<<endl;

else

{

fpget (cbuffer);

while (!fpeof())

{

if(cbuffer=='\n')

{

linenum++;

fpget(cbuffer);

}

else if (isalpha(cbuffer))

{

cbuffer=alphaprocess(cbuffer);

}

else if (isdigit(cbuffer))

{

cbuffer=digitprocess(cbuffer);

}

else

cbuffer=otherprocess(cbuffer);

}

}

cout<<"标识符个数是:"<<lableconstnum<<"分别是"<<endl;

i=0;

while(i<lableconstnum)

{

cout<<lableconst[i++]<<" ";

}

cout<<endl;

cout<<"完成\n";

getchar();

}

分类: 电脑/网络 >> 程序设计 >> 其他编程语言

问题描述:

完成以下正则文法所描述的Pascal语言子集单词符号的词法分析程序。

<标识符>→字母| <标识符>字母| <标识符>数字

<无符号整数>→数字| <无符号整数>数字

<单字符分界符> →+ |- | |; |(|)

<双字符分界符>→<大于>=|<小于>=|<小于>>|<冒号>=|<斜竖>

<小于>→<

<等于>→=

<大于>→>

<冒号> →:

<斜竖> →/

该语言的保留字 :begin end if then else for do while and or not

说明:

1 该语言大小写不敏感。

2 字母为a-z A-Z,数字为0-9。

3可以对上述文法进行扩充和改造。

4 ‘/……/’为程序的注释部分。

[设计要求]

1、 给出各单词符号的类别编码。

2、 词法分析程序应能发现输入串中的错误。

3、 词法分析作为单独一遍编写,词法分析结果为二元式序列组成的中间文件。

4、设计两个测试用例(尽可能完备),并给出测试结果。

解析:

这种问题 …… 会有人解答吗

using System;

using SystemCollections;

using SystemWindowsForms;

using SystemLinq;

using SystemText;namespace 词法分析

{

class Program

{

static void Main(string[] args)

{

ArrayList al = new ArrayList();

ConsoleWriteLine("请输入关键词的个数");

int nI = 0;

try

{

nI = ConvertToInt32(ConsoleReadLine());

}

catch (Exception)

{

MessageBoxShow("输入有误");

return;

}

ConsoleWriteLine("请输入关键字");

string subString = "";

for (int i = 0; i < nI; i++)

{

subString = ConsoleReadLine();

alAdd(subString);

} ConsoleWriteLine("请输入带分析的字符串。");

string mainString = ConsoleReadLine();

int counter = 0,found =0; foreach (string item in al)

{

for (found = mainStringIndexOf(item[0]);

found + itemLength <= mainStringLength && -1 != found;

found = mainStringIndexOf(item[0], found))

{

if (item == mainStringSubstring(found, itemLength))

{

counter++;

found++;

}

} ConsoleWriteLine(item + "出现次数:" + counter);

counter = 0; } }

}

}

以上就是关于编译原理词法分析程序全部的内容,包括:编译原理词法分析程序、编译原理课程-简单词法分析器设计(C或C++)、C#怎么实现词法分析器等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zz/10010264.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-04
下一篇2023-05-04

发表评论

登录后才能评论

评论列表(0条)

    保存