编制C语言子集的词法分析程序

编制C语言子集的词法分析程序,第1张

#include <iostream>

#include <string>

using namespace std

string key[6] = {"begin", "if", "then", "while", "do", "end"}

//关键字

bool isKey( string str, int &syn) //判断是否为关键字,若是传回相

应关键码的种别名

{

int i

for(i=0i<6i++)

{

if(str == key[i])

{

syn = i + 1

return true

}

}

return false

}

bool isLetter(char c) //是否为字母

{

if((c >= 'A' &&c <= 'Z') || (c >= 'a' &&c <= 'z'))

return true

else

return false

}

bool isDigit(char c) //是否为数字

{

if(c >= '0' &&c <= '9')

return true

else

return false

}

void analyse(FILE *fileP)

{

int n

char c

string str = ""

while((c = fgetc(fileP)) != EOF)

{

if(c == ' ' || c == '\n' || c == '\t')

continue

else if(isDigit(c)) //数字

{

while(isDigit(c))

{

str += c

c = fgetc(fileP)

}

fseek(fileP, -1, SEEK_CUR)

cout <<"(11, " <<str <<")" <<endl

str = ""

}

else if(isLetter(c)) //字母开头的

{

while(isDigit(c) || isLetter(c))

{

str += c

c = fgetc(fileP)

}

fseek(fileP, -1, SEEK_CUR)

if(isKey(str, n))

cout <<"(" <<n <<", " <<str <<")" <<endl//关键码

else

cout <<"(10, " <<"\'"<<str <<"\'" <<")" <<endl//标志符

str = ""

}

else // *** 作符等

{

switch(c)

{

case '+':

cout <<"(13, +)" <<endl

break

case '-':

cout <<"(14, -)" <<endl

break

case '*':

cout <<"(15, *)" <<endl

break

case '/':

cout <<"(16, /)" <<endl

break

case ':':

{

if(c=fgetc(fileP) == '=')

cout <<"(18, :=)" <<endl

else

{

cout <<"(17, :)" <<endl

fseek(fileP, -1, SEEK_CUR)

}

break

}

case '<':

{

c=fgetc(fileP)

if(c == '=')

cout <<"(22, <=)" <<endl

else if(c == '>')

cout <<"(21, <>)" <<endl

else

{

cout <<"(20, <)" <<endl

fseek(fileP, -1, SEEK_CUR)

}

break

}

case '>':

{

c=fgetc(fileP)

if(c == '=')

cout <<"(24, >=)" <<endl

else

{

cout <<"(23, >)" <<endl

fseek(fileP, -1, SEEK_CUR)

}

break

}

case '=':

cout <<"(25, =)" <<endl

break

case '':

cout <<"(26, )" <<endl

break

case '(':

cout <<"(27, ()" <<endl

break

case ')':

cout <<"(28, ))" <<endl

break

case '#':

cout <<"(0, #)" <<endl

break

}

}

}

}

int main()

{

FILE *fileP

fileP = fopen("test.txt", "r")

cout <<"------词法分析如下------" <<endl

analyse(fileP)

return 0

}

#include "stdio.h"                  /*定义I/O库所用的某些宏和变量*/

#include "string.h"                 /*定义字符串库函数*/

#include "conio.h"                  /*提供有关屏幕窗口 *** 作函数*/

#include "ctype.h"                  /*分类函数*/

char prog[80]={'\0'},

token[8]                     /*存放构成单词符号的字符串*/

char ch

int syn,                           /*存放单词字符的种别码*/

n,

sum,                           /*存放整数型单词*/

m,p                           /*p是缓冲区prog的指针,m是token的指针*/

char *rwtab[6]={"begin","if","then","while","do","end"}

void scaner(){

m=0

sum=0

for(n=0n<8n++)

token[n]='\0'

ch=prog[p++]

while(ch==' ')

ch=prog[p++]

if(isalpha(ch))    /*ch为字母字符*/{

while(isalpha(ch)||isdigit(ch))    /*ch 为字母字符或者数字字符*/{

token[m++]=ch

ch=prog[p++]}

token[m++]='\0'

ch=prog[p--]

syn=10

for(n=0n<6n++)

if(strcmp(token,rwtab[n])==0)    /*字符串的比较*/{

syn=n+1

break}}

else

if(isdigit(ch))    /*ch是数字字符*/{

while(isdigit(ch))    /*ch是数字字符*/{

sum=sum*10+ch-'0'

ch=prog[p++]}

ch=prog[p--]

syn=11}

else

switch(ch){

case'<':m=0token[m++]=chch=prog[p++]

if(ch=='>'){

syn=21

token[m++]=ch}

else if(ch=='='){

syn=22

token[m++]=ch}

else{

syn=20

ch=prog[p--]}

break

case'>':m=0token[m++]=chch=prog[p++]

if(ch=='='){

syn=24

token[m++]=ch}

else{

syn=23

ch=prog[p--]}

break

case':':m=0token[m++]=chch=prog[p++]

if(ch=='='){

syn=18

token[m++]=ch}

else{

syn=17

ch=prog[p--]}

break

case'+':syn=13token[0]=chbreak

case'-':syn=14token[0]=chbreak

case'*':syn=15token[0]=chbreak

case'/':syn=16token[0]=chbreak

case'=':syn=25token[0]=chbreak

case'':syn=26token[0]=chbreak

case'(':syn=27token[0]=chbreak

case')':syn=28token[0]=chbreak

case'#':syn=0token[0]=chbreak

default:syn=-1}}

main()

{

printf("\n\nThe significance of the figures:\n"

"1.figures 1 to 6 said Keyword\n"

"2.figures 10 and 11 said Other indicators\n"

"3.figures 13 to 28 said Operators\n")

p=0

printf("\nplease input string:\n")

do {

ch=getchar()

prog[p++]=ch

}while(ch!='#')

p=0

do{

scaner()

switch(syn){

case 11: printf("(%d,%d)\n",syn,sum)break

case -1: printf("\n ERROR\n")break

default: printf("(%d,%s)\n",syn,token)

}

}while(syn!=0)

getch()

}

程序测试结果

对源程序begin x:=9: if x>9 then x:=2*x+1/3 end #的源文件,经过词法分析后输出如下图5-1所示:

具体的你在修改修改吧

这是我们实验的作业,代码如下:

#include <iostream>

#include<string>

using namespace std

#define MAX 22

char ch =' '

string key[15]={"begin","end","if","then","else","while","write","read",

"do", "call","const","char","until","procedure","repeat"}

int Iskey(string c){ //关键字判断

int i

for(i=0i<MAXi++) {

if(key[i].compare(c)==0) return 1

}

return 0

}

int IsLetter(char c) {//判断是否为字母

if(((c<='z')&&(c>='a'))||((c<='Z')&&(c>='A'))) return 1

else return 0

}

int IsDigit(char c){ //判断是否为数字

if(c>='0'&&c<='9') return 1

else return 0

}

void analyse(FILE *fpin){

string arr=""

while((ch=fgetc(fpin))!=EOF) {

arr=""

if(ch==' '||ch=='\t'||ch=='\n'){}

else if(IsLetter(ch)){

while(IsLetter(ch)||IsDigit(ch)) {

if((ch<='Z')&&(ch>='A')) ch=ch+32

arr=arr+ch

ch=fgetc(fpin)

}

fseek(fpin,-1L,SEEK_CUR)

if (Iskey(arr)){cout<<arr<<"\t$关键字"<<endl}

else cout<<arr<<"\t$普通标识符"<<endl

}

else if(IsDigit(ch)){

while(IsDigit(ch)||ch=='.'&&IsDigit(fgetc(fpin))){

arr=arr+ch

ch=fgetc(fpin)

}

fseek(fpin,-1L,SEEK_CUR)

cout<<arr<<"\t$无符号实数"<<endl

}

else switch(ch){

case'+':

case'-' :

case'*' :

case'=' :

case'/' :cout<<ch<<"\t$运算符"<<endlbreak

case'(' :

case')' :

case'[' :

case']' :

case'' :

case'.' :

case',' :

case'{' :

case'}' :cout<<ch<<"\t$界符"<<endlbreak

case':' :{ch=fgetc(fpin)

if(ch=='=') cout<<":="<<"\t$运算符"<<endl

else {cout<<"="<<"\t$运算符"<<endl

fseek(fpin,-1L,SEEK_CUR)}

}break

case'>' :{ch=fgetc(fpin)

if(ch=='=') cout<<">="<<"\t$运算符"<<endl

if(ch=='>')cout<<">>"<<"\t$输入控制符"<<endl

else {cout<<">"<<"\t$运算符"<<endl

fseek(fpin,-1L,SEEK_CUR)}

}break

case'<' :{ch=fgetc(fpin)

if(ch=='=')cout<<"<="<<"\t$运算符"<<endl

else if(ch=='<')cout<<"<<"<<"\t$输出控制符"<<endl

else if(ch=='>') cout<<"<>"<<"\t$运算符"<<endl

else{cout<<"<"<<"\t$运算符"<<endl

fseek(fpin,-1L,SEEK_CUR)}

}break

default : cout<<ch<<"\t$无法识别字符"<<endl

}

}

}

void main(){

char in_fn[30]

FILE * fpin

cout<<"请输入源文件名(包括路径和后缀名):"

for(){

cin>>in_fn

if((fpin=fopen(in_fn,"r"))!=NULL) break

else cout<<"文件路径错误!请输入源文件名(包括路径和后缀名):"

}

cout<<"\n********************分析如下*********************"<<endl

analyse(fpin)

fclose(fpin)

}

自己写一个源文件,TXT格式的,简单的几个句子就可以!


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

原文地址: http://outofmemory.cn/yw/7808856.html

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

发表评论

登录后才能评论

评论列表(0条)

保存