詞法分析c語言函數(shù) 詞法分析C語言

C語言 詞法分析

詞法分析是編譯原理的一部分,

創(chuàng)新互聯(lián)公司主營(yíng)羅定網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都App制作,羅定h5成都微信小程序搭建,羅定網(wǎng)站營(yíng)銷推廣歡迎羅定等地區(qū)企業(yè)咨詢

主要分析詞的組成的,我是這樣認(rèn)為的,

就比如,你的這個(gè)例子,你的詞法分析完成后,

可將

-3.14

*

34

+

(

8

-

45

+

3

)

+

9

*

3.4

這些都分別的找到,然后才能運(yùn)算啊,要不,你知道怎么去運(yùn)算,

當(dāng)然,這個(gè),我個(gè)人認(rèn)為,要是做的話,采用棧做比較好,像數(shù)據(jù)結(jié)構(gòu)那樣,一個(gè)數(shù)字棧,一個(gè)符號(hào)棧,然后將找到的進(jìn)行壓棧出棧來完成.

求一個(gè)C語言詞法分析器源代碼

我有,這是這學(xué)期剛做的,

#include iostream

#include fstream

#include sstream

#include string

#include vector

#include algorithm

using namespace std;

bool isLetter(char ch){

if ((ch='A' ch='Z') || (ch='a' ch='z')) return true;

else return false;

}

bool isDigit(char ch){

if (ch='0' ch='9') return true;

else return false;

}

bool isP(char ch){

if(ch=='+'||ch=='*'||ch=='-'||ch=='/') return true;

//ch==':'||ch==','||ch=='='||ch==';'||ch=='('||ch==')'

else return false;

}

bool isJ(char ch){

if(ch==','||ch==';'||ch=='.'||ch=='('||ch==')'||ch=='['||ch==']'||ch=='='||ch==':'||ch==''||ch==''||ch=='{'||ch=='}'||ch=='#') return true;

//

else return false;

}

bool isBlank(char ch){

if(ch==' '||ch=='\t') return true;

else return false;

}

int main(){

string src,ste,s;

char ch0,ch,ch1[2];

char ktt[48][20]={"and","begin","const","div","do","else","end","function","if","integer",

"not","or","procedure","program","read","real","then","type","var","while","write","標(biāo)識(shí)符","無符號(hào)數(shù)",

",",";",":",".","(",")","[","]","..","++","--","+","-","*","/","=","","","","="

,"=",":=","{","}","#"};

int pos=0;

FILE *fp;

fp=fopen("d:\\in.txt","r");

ch0=fgetc(fp);

while(ch0!=EOF)

{

//if(ch0!='\t'){src+=ch0;}

src+=ch0;

ch0=fgetc(fp);

}

src+='#';

coutsrcendl;

ch=src[pos++];

ste=" ";

for(int j=0;j47;j++){coutjktt[j]endl;}

cout"詞法分析:\n";

while(ch!='#')

{

char str[20];

if(ch!='\n')

{

if(isDigit(ch))

{ //判斷常數(shù)

int i=0;

while(isDigit(ch)||ch=='.')

{

str[i++]=ch;

//i++;

ch=src[pos++];

}

str[i]='\0';

ste=ste+"|"+"22";

coutstr;

continue;

}

else if(isLetter(ch))

{ //判斷字符

int i=0,j;

while(isLetter(ch)||isDigit(ch))

{

str[i++]=ch;

//i++;

ch=src[pos++];

}

str[i]='\0';

for(j=0;j21;j++){ //判斷是否關(guān)鍵字

int t=strcmp(str,ktt[j]);

if(t==0) {

stringstream ss;

ste+="|";

ssste;ssj;

ssste;

break;

}

}

if(j==21){ste=ste+"|"+"21";}

// cout" ";

coutstr;

continue;

}

else if(isP(ch)){ ///判斷是否運(yùn)算符

int i=0,j;

str[i++]=ch;

str[i]='\0';

for(j=34;j38;j++){

int t=strcmp(str,ktt[j]);

if(t==0) {

stringstream ss;

ste+="|";

ssste;ssj;

ssste;

break;

}

}

coutstr;

ch=src[pos++];

continue;

}

else if(isJ(ch)) //判斷是否界符

{

int i=0,j;

while(isJ(ch))

{

str[i++]=ch;

ch=src[pos++];

}

str[i]='\0';

for(j=23;j47;j++){

int t=strcmp(str,ktt[j]);

if(t==0) {

stringstream ss;

ste+="|";

ssste;ssj;

ssste;

break;

}

}

coutstr;

continue;

}

else if(isBlank(ch))

{

coutch;

ch=src[pos++];

continue;

}

}

else{

coutsteendl;

ste=" ";

}

ch=src[pos++];

}

return 0;

}

還有運(yùn)行效果圖,和實(shí)驗(yàn)報(bào)告 ,你要的話留下郵箱

用C語言描述的單詞符號(hào)的詞法分析程序。大神們幫幫忙

#includestdio.h #includestring.h #define nmax 14 #define norw 8 /*關(guān)鍵字個(gè)數(shù)*/ #define al 10 #define getchdo if(-1==getch()) return -1 #define getsymdo if(-1==getsym()) return -1 char ch; /*獲取字符的緩沖區(qū),getch使用*/ char word[norw][al]; /*保留字*/ char fname[al]; /*文件名*/ char id[al+1]; /*存放標(biāo)識(shí)符或保留字*/ char line[81]; /*讀取行緩沖區(qū)*/ char a[al+1]; /*臨時(shí)符號(hào),多處的字節(jié)用于存放0*/ char anum[nmax+1]; /*臨時(shí)符號(hào),存放number*/ char inum[nmax+1]; /*存放常數(shù)*/ int cc,ll; /*cc表示當(dāng)前字符(ch)的位置*/ int num; /*常數(shù)*/ int err; //錯(cuò)誤計(jì)數(shù)器 FILE * fin; FILE * fout; FILE * fas; /*詞法分析結(jié)果文件*/ /*函數(shù)執(zhí)行出錯(cuò),退出程序*/ int getch(); /*讀取一行字符*/ int getsym(); /*從文件fin中讀取一行字符,保存到字符緩沖區(qū)line中*/ int getch() { if(cc==ll) { ll=0; cc=0; ch=' '; while(ch!=10) { if(feof(fin)) { return -1; } if(EOF==fscanf(fin,"%c",ch)) { line[ll]=0; break; } line[ll]=ch; ll++; } } ch=line[cc]; cc++; return 0; } /*讀取一個(gè)分詞*/ int getsym() { int i,j,k; while(ch==' '||ch==10||ch==9) //忽略空格,換行和TAB { getchdo; } if(ch='a'ch='z') //判斷是否為關(guān)鍵字或標(biāo)識(shí)符 { k=0; do{ if(kal){ a[k]=ch; k++; } getchdo; }while(ch='a'ch='z'||ch='0'ch='9'); a[k]=0; if(kal)printf("error"); strcpy(id,a); i=0; j=norw-1; do{ k=(i+j)/2; if(strcmp(id,word[k])=0) {j=k-1; } if(strcmp(id,word[k])=0) { i=k+1; } }while(i=j); if(i-1j) { fprintf(fas,"Keyword %s\n",id);/*分詞為關(guān)鍵字*/ } return 0; } 這里面的代碼寫不開,你加我的QQ:412686686,我直接傳文件給你好了,你這個(gè)問題的代碼太多了,我剛好昨天給別人寫了一個(gè)。

一個(gè)C語言詞法分析題目

//給你課程設(shè)計(jì)的詞法部分呵呵

#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標(biāo)識(shí)

/*****************************重要數(shù)據(jù)結(jié)構(gòu)的聲明開始*************************/

struct delos

{

int code,value;

}*result; //結(jié)果

//變量表

struct analyse

{

int state;

char sign;

};

struct list

{

int value;

list *next;

};

//條件語句的LR(1)分析表,110表示接受,999表示出錯(cuò)

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}

};

/*****************************重要數(shù)據(jù)結(jié)構(gòu)的聲明結(jié)束*************************/

/*********************************全局變量聲明開始**************************/

int place=1;

int nextpos=1;

stacklist * stknext;

stacklist * stktrue;

stacklist * stkfalse;

stackint stkpos;

stackdelos stktemp;//常量,變量,臨時(shí)變量

delos temp;

delos gen[50][4];//生成的三地址

string str[31]={"","int","if","else","float","print","標(biāo)識(shí)符","常數(shù)",

"=","+","*","**",";","(",")",

"{","}","[","]","-","/",",","","||","!",

"==","","","=","=","!="};

//變量

string *var;

int varlen=0,nowvar=1;

//常量

float *myconst;

int constlen=0,nowconst=1;

int resultlen=0,nowresult=0;

/*********************************全局變量聲明結(jié)束**************************/

void renewresult()

{

delos *p3=result;

int i;

resultlen+=10;

result=new delos[resultlen];

for(i=0;iresultlen;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;inowvar;i++)

var[i]=p1[i];

delete[] p1;

}

void renewconst()

{

float *p2=myconst;

int i;

constlen+=10;

myconst=new float[constlen];

for(i=0;inowconst;i++)

myconst[i]=p2[i];

delete[] p2;

}

bool isletter(char c) //判別是否字母

{

if(c64c91||c96c123)

return true;

return false;

}

bool isdigital(char c) //判別是否數(shù)字

{ if(c47c58)

return true;

return false;

}

int reserve(char c[],int i)

{

string s(c,0,i);

for(int j=1;j7;j++)

if(s==str[j])

return j;

return 0;

}

void insertresult(int code,int value)

{ if(nowresultresultlen)

renewresult();

result[nowresult].code=code;

result[nowresult++].value=value;

}

void insertid(char c[],int i)

{ string s(c,0,i);

insertresult(ID,nowvar);

if(nowvarvarlen)

renewvar();

var[nowvar++]=s;

}

//插入常數(shù),為浮點(diǎn)型

void insertconst(char c[],int i)

{ int d=0,j;

float a=0,b=1;

while(c[d]!='.'di)

d++;

for(j=d-1;j=0;j--)

{ a=a+(c[j]-48)*b;

b=b*10;

}

b=10;

for(j=d+1;ji;j++)

{a=a+(c[j]-48)/b;

b=b*10;

}

insertresult(CONSTANT,nowconst);

if(nowconstconstlen)

renewconst();

myconst[nowconst++]=a;

}

/**********************************詞法分析函數(shù)開始***********************/

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 :"strtokenendl;

}

}

else if(ch=='|')

{ ch=myfile.get();

if(ch=='|')

insertresult(op_or,0);

else

{

myfile.seekg(-1,ios::cur);

myfile.get(strtoken,10);

cout"ERROR :"strtokenendl;

}

}

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 :"strtokenendl;

myfile.seekg(1,ios::cur);

}

}

}

myfile.close();

cout"詞法分析成功啦!!"endl;

}

/**********************************詞法分析函數(shù)結(jié)束***********************/

編譯原理課程設(shè)計(jì)-詞法分析器設(shè)計(jì)(C語言)

#include?"stdio.h"??????????????????/*定義I/O庫所用的某些宏和變量*/

#include?"string.h"?????????????????/*定義字符串庫函數(shù)*/

#include?"conio.h"??????????????????/*提供有關(guān)屏幕窗口操作函數(shù)*/

#include?"ctype.h"??????????????????/*分類函數(shù)*/

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

token[8];?????????????????????/*存放構(gòu)成單詞符號(hào)的字符串*/

char?ch;

int?syn,???????????????????????????/*存放單詞字符的種別碼*/

n,

sum,???????????????????????????/*存放整數(shù)型單詞*/

m,p;???????????????????????????/*p是緩沖區(qū)prog的指針,m是token的指針*/

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

void?scaner(){

m=0;

sum=0;

for(n=0;n8;n++)

token[n]='\0';

ch=prog[p++];

while(ch=='?')

ch=prog[p++];

if(isalpha(ch))????/*ch為字母字符*/{

while(isalpha(ch)||isdigit(ch))????/*ch?為字母字符或者數(shù)字字符*/{

token[m++]=ch;

ch=prog[p++];}

token[m++]='\0';

ch=prog[p--];

syn=10;

for(n=0;n6;n++)

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

syn=n+1;

break;}}

else

if(isdigit(ch))????/*ch是數(shù)字字符*/{

while(isdigit(ch))????/*ch是數(shù)字字符*/{

sum=sum*10+ch-'0';

ch=prog[p++];}

ch=prog[p--];

syn=11;}

else

switch(ch){

case'':m=0;token[m++]=ch;ch=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=0;token[m++]=ch;ch=prog[p++];

if(ch=='='){

syn=24;

token[m++]=ch;}

else{

syn=23;

ch=prog[p--];}

break;

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

if(ch=='='){

syn=18;

token[m++]=ch;}

else{

syn=17;

ch=prog[p--];}

break;

case'+':syn=13;token[0]=ch;break;

case'-':syn=14;token[0]=ch;break;

case'*':syn=15;token[0]=ch;break;

case'/':syn=16;token[0]=ch;break;

case'=':syn=25;token[0]=ch;break;

case';':syn=26;token[0]=ch;break;

case'(':syn=27;token[0]=ch;break;

case')':syn=28;token[0]=ch;break;

case'#':syn=0;token[0]=ch;break;

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();

}

程序測(cè)試結(jié)果

對(duì)源程序begin?x:=9:?if?x9?then?x:=2*x+1/3;?end?#的源文件,經(jīng)過詞法分析后輸出如下圖5-1所示:

具體的你在修改修改吧

分享題目:詞法分析c語言函數(shù) 詞法分析C語言
當(dāng)前網(wǎng)址:http://muchs.cn/article26/hhecjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、虛擬主機(jī)微信公眾號(hào)、網(wǎng)站收錄、響應(yīng)式網(wǎng)站商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化