大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

词法分析c语言函数 词法分析C语言

C语言 词法分析

词法分析是编译原理的一部分,

创新互联公司主营罗定网站建设的网络公司,主营网站建设方案,成都App制作,罗定h5成都微信小程序搭建,罗定网站营销推广欢迎罗定等地区企业咨询

主要分析词的组成的,我是这样认为的,

就比如,你的这个例子,你的词法分析完成后,

可将

-3.14

*

34

+

(

8

-

45

+

3

)

+

9

*

3.4

这些都分别的找到,然后才能运算啊,要不,你知道怎么去运算,

当然,这个,我个人认为,要是做的话,采用栈做比较好,像数据结构那样,一个数字栈,一个符号栈,然后将找到的进行压栈出栈来完成.

求一个C语言词法分析器源代码

我有,这是这学期刚做的,

#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","标识符","无符号数",

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

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

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

{ //判断常数

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++){ //判断是否关键字

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)){ ///判断是否运算符

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;

}

还有运行效果图,和实验报告 ,你要的话留下邮箱

用C语言描述的单词符号的词法分析程序。大神们帮帮忙

#includestdio.h #includestring.h #define nmax 14 #define norw 8 /*关键字个数*/ #define al 10 #define getchdo if(-1==getch()) return -1 #define getsymdo if(-1==getsym()) return -1 char ch; /*获取字符的缓冲区,getch使用*/ char word[norw][al]; /*保留字*/ char fname[al]; /*文件名*/ char id[al+1]; /*存放标识符或保留字*/ char line[81]; /*读取行缓冲区*/ char a[al+1]; /*临时符号,多处的字节用于存放0*/ char anum[nmax+1]; /*临时符号,存放number*/ char inum[nmax+1]; /*存放常数*/ int cc,ll; /*cc表示当前字符(ch)的位置*/ int num; /*常数*/ int err; //错误计数器 FILE * fin; FILE * fout; FILE * fas; /*词法分析结果文件*/ /*函数执行出错,退出程序*/ int getch(); /*读取一行字符*/ int getsym(); /*从文件fin中读取一行字符,保存到字符缓冲区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; } /*读取一个分词*/ int getsym() { int i,j,k; while(ch==' '||ch==10||ch==9) //忽略空格,换行和TAB { getchdo; } if(ch='a'ch='z') //判断是否为关键字或标识符 { 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);/*分词为关键字*/ } return 0; } 这里面的代码写不开,你加我的QQ:412686686,我直接传文件给你好了,你这个问题的代码太多了,我刚好昨天给别人写了一个。

一个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;

stacklist * stknext;

stacklist * stktrue;

stacklist * stkfalse;

stackint stkpos;

stackdelos 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;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) //判别是否数字

{ 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;

}

//插入常数,为浮点型

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;

}

/**********************************词法分析函数开始***********************/

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;

}

/**********************************词法分析函数结束***********************/

编译原理课程设计-词法分析器设计(C语言)

#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=0;n8;n++)

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=0;n6;n++)

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

}

程序测试结果

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

具体的你在修改修改吧


新闻名称:词法分析c语言函数 词法分析C语言
网页网址:http://dzwzjz.com/article/hhecjg.html
在线咨询
服务热线
服务热线:028-86922220
TOP