C语言词法分析include "globals.h"
#include "util.h"
#include "scan.h"
typedef enum {
START, INID, INNUM, INCOMMENT, INCOMMENT_ASTERISK,
INDIV, INLT, INGT, INASSIGN, INNOT,
DONE
} StateType;
char token_string[MAXTOKENLEN 1];
#define BUFLEN 256
static char linebuf[BUFLEN];
static int linepos = 0;
static int bufsize = 0;
static int eof = 0;
static char get_next_char()
{
if(eof)
return EOF;
if(lineposbufsize)
return linebuf[linepos];
else {
lineno;
if(fgets(linebuf, BUFLEN-1, source)) {
if(echo_source)
fprintf(listing, "M: %s", lineno, linebuf);
bufsize = strlen(linebuf);
linepos = 0;
return linebuf[linepos];
} else {
eof = 1;
return EOF;
}
}
}
static void unget_char()
{
if(!eof)
linepos--;
}
static struct {
char *str;
TokenType tok;
} reserved_words[MAXRESERVED] = {
{"if", IF}, {"else", ELSE},
{"int", INT}, {"return", RETURN},
{"void", VOID}, {"while", WHILE}
};
static TokenType reserved_lokkup(const char *s)
{
int i;
for ( i = 0; iMAXRESERVED; i) {
if(!strcmp(reserved_words[i].str, s))
return reserved_words[i].tok;
}
return ID;
}
TokenType get_token()
{
int index = 0;
TokenType cur_token;
StateType state = START;
int save;
while(state != DONE) {
char c = get_next_char();
save = TRUE;
switch(state) {
case START:
if(isdigit(c))
state = INNUM;
else if(isalpha(c))
state = INID;
else if(c == '/')
state = INDIV;
else if(c == '')
state = INLT;
else if(c == '')
state = INGT;
else if(c == '=')
state = INASSIGN;
else if(c == '!')
state = INNOT;
else if(isblank(c) || c == '\n')
save = FALSE;
else {
state = DONE;
switch(c) {
case EOF:
save = FALSE;
cur_token = ENDFILE;
break;
case ' ': cur_token = PLUS; break;
case '-': cur_token = MINUS; break;
case '*':
cur_token = MUL;
if (get_next_char() == '/') {
cur_token = ERROR;
} else unget_char();
break;
case ';': cur_token = SEMI; break;
case ',': cur_token = COMMA; break;
case '(': cur_token = LPAREN; break;
case ')': cur_token = RPAREN; break;
case '[': cur_token = LBRACKET; break;
case ']': cur_token = RBRACKET; break;
case '{': cur_token = LBRACE; break;
case '}': cur_token = RBRACE; break;
default:
cur_token = ERROR;
break;
}
}
break;
case INCOMMENT:
save = FALSE;
if(c == '*')
state = INCOMMENT_ASTERISK;
else if(c == EOF) {
cur_token = ERROR;
state = DONE;
}
break;
case INCOMMENT_ASTERISK:
save = FALSE;
if(c == '/') {
state = START;
index = 0;
}else if(c == '*'){
}else if(c == EOF) {
cur_token = ERROR;
state = DONE;
}else{
state = INCOMMENT;
}
break;
case INID:
if(!isalpha(c)) {
unget_char();
state = DONE;
save = FALSE;
cur_token = ID;
}
break;
case INNUM:
if(!isdigit(c)) {
unget_char();
state = DONE;
save = FALSE;
cur_token = NUM;
}
break;
case INDIV:
if(c == '*') {
state = INCOMMENT;
save = FALSE;
} else {
unget_char();
save = FALSE;
state = DONE;
cur_token =DIV;
}
break;
case INLT:
if(c == '=') {
state = DONE;
cur_token = LE;
} else {
unget_char();
state = DONE;
save = FALSE;
cur_token =LT;
}
break;
case INGT:
if(c == '=') {
state = DONE;
cur_token = GE;
} else {
unget_char();
save = FALSE;
state = DONE;
cur_token =GT;
}
break;
case INASSIGN:
if(c == '=') {
state = DONE;
cur_token = EQ;
} else {
unget_char();
save = FALSE;
state = DONE;
cur_token =ASSIGN;
}
break;
case INNOT:
state = DONE;
if(c == '=') {
cur_token = NEQ;
} else {
unget_char();
save = FALSE;
cur_token =ERROR;
}
break;
case DONE:
default:
fprintf(listing, "Scanner Bug: state = %d\n", state);
state = DONE;
cur_token = ERROR;
break;
}
if(save(indexMAXTOKENLEN))
token_string[index] = c;
if(state == DONE) {
token_string[index] = '\0';
if(cur_token == ID)
cur_token = reserved_lokkup(token_string);
}
}
if(trace_scan) {
fprintf(listing, "\t%d: ", lineno);
print_token(cur_token, token_string);
}
return cur_token;
}
用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 -1char 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 "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语言如何编写函数的求导求导数有两种,一种是表达式求导,一种是数值求导 。
表达式求导:需要对表达式进行词法分析,然后用常见的求导公式进行演算,求得导函数 。在这方面,数学软件matrix,maple做得非常好 。如果自己用C进行编程,不建议 。
数值求导:利用导数的定义,用差分计算,当自变量趋于0时,前后两次差分收敛到需要精度,计算结束 。这种方法可以求得某一点的导数 。
例如:
求一阶导数,原函数 y = f(x),程序中是float f(float x){ ...}
dx=0.01;//设 dx 初值
do{
dd1=(f(x0) - f(x0 dx))/dx;//计算导数dd1
dx = 0.5 * dx;// 减小步长
dd2=(f(x0) - f(x0 dx))/dx;//计算导数dd2
}while (fabs(dd1-dd2) = 1e-06) //判断新旧导数值之差是否满足精度 , 满足则得结果,不满足则返回
【词法分析c语言函数 词法分析c语言函数公式】词法分析c语言函数的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于词法分析c语言函数公式、词法分析c语言函数的信息别忘了在本站进行查找喔 。
推荐阅读
- 安卓微信关联是什么标志,手机微信关联号是什么意思
- python注册表中找不到,pycharm注册表
- net4.6.2如何卸载,net45怎么卸载
- 秋导师直播录屏怎么录,秋导师直播录屏怎么录视频
- mysql怎么转移 mysql转移数据库
- 网易云音乐下载音乐怎么转到u盘,网易云下载的歌曲怎么转到u盘
- 电脑没插u盘为什么显示插入u盘,电脑没插优盘却有显示一个
- phpcms首页调用文章,php的首页文件在哪个目录下
- go语言定时执行 go语言执行cmd命令