对合法标识符识别

happycoolman 2008-12-29 12:47:20
谁会有关 有限自动机的运行原理编制程序,使得程序能够对合法标识符识别. 求个源程序 谢谢大家
...全文
342 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
liberpc 2008-12-30
  • 打赏
  • 举报
回复
编译原理,学习
pingzi_1119 2008-12-30
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 jieao111 的回复:]
难得见飞雪mm代码有注释,,一定是copy的
[/Quote]
飞雪是个mm???
baihacker 2008-12-29
  • 打赏
  • 举报
回复
DFA只是思想...不一定要看到明显的什么状态转移的...
就呆在云上 2008-12-29
  • 打赏
  • 举报
回复
就根据合法标识符的要求来写就是了啊
很简单的啊
那就几种
然后你做一个字符串数组里面存放关键字,你自己添加呵呵
waizqfor 2008-12-29
  • 打赏
  • 举报
回复
UP
happycoolman 2008-12-29
  • 打赏
  • 举报
回复
好的 我试下哦 谢谢
jieao111 2008-12-29
  • 打赏
  • 举报
回复
难得见飞雪mm代码有注释,,一定是copy的
zenny_chen 2008-12-29
  • 打赏
  • 举报
回复
嗯。不错。以上是比较典型的手工进行词法分析的方法。
星羽 2008-12-29
  • 打赏
  • 举报
回复
panyulun 2008-12-29
  • 打赏
  • 举报
回复
//interface and implementation of state machine classes
//file finite.h
#include <string.h>

struct parent
{
static char *expression;
static index;
static end_state; //1 when last state is reached,0 otherwise
static doom_state; //1 when illegal input occurs,0 otherwise
parent(char *expr)
{
expression=new char [strlen(expr)];
strcpy(expression,expr);
end_state=0;
doom_state=0;
index=0;
}
virtual parent *transition() {}
};
char *parent::expression=0;
int parent::index=0;
int parent::end_state=0;
int parent::doom_state=0;

struct state1:public parent
{
parent *ptr2,*ptr3,*ptr4,*ptr5;
state1():parent(expression) {}
parent *transition();
};

struct state2:public parent
{
parent *ptr2;
state2():parent(expression) {}
parent *transition();
};

struct state3:public parent
{
parent *ptr3,*ptr4;
state3():parent(expression) {}
parent *transition();
};

struct state4:public parent
{
parent *ptr4;
state4():parent(expression) {}
parent *transition();
};

struct state5:public parent
{
parent *ptr2,*ptr4,*ptr5;
state5():parent(expression) {}
parent *transition();
};

parent *state1::transition()
{
switch (expression[index++])
{
case 'A': return ptr2;
case 'B': return ptr3;
case 'C': return ptr4;
case 'D': return ptr5;
case '\0': doom_state=1;
default: doom_state=1;
}
}

parent *state2::transition()
{
switch (expression[index++])
{
case 'E': return ptr2;
case 'I': end_state=1;
case '\0': doom_state=1;
default: doom_state=1;
}
}

parent *state3::transition()
{
switch (expression[index++])
{
case 'F': return ptr3;
case 'M': return ptr4;
case 'J': end_state=1;
case '\0': doom_state=1;
default: doom_state=1;
}
}

parent *state4::transition()
{
switch (expression[index++])
{
case 'G': return ptr4;
case 'K': end_state=1;
break;
case '\0': doom_state=1;
default: doom_state=1;
}
}

parent *state5::transition()
{
switch (expression[index++])
{
case 'O': return ptr2;
case 'H': return ptr5;
case 'L': end_state=1;
case 'N': return ptr4;
case '\0': doom_state=1;
default: doom_state=1;
}
}
//main program for finite-state machine
#include <stdio.h>
#include <conio.h>
#include "finite.h"

state1 s1;
state2 s2;
state3 s3;
state4 s4;
state5 s5;

void build_state_machine()
{
s1.ptr2=&s2;
s1.ptr3=&s3;
s1.ptr4=&s4;
s1.ptr5=&s5;
s2.ptr2=&s2;
s3.ptr3=&s3;
s3.ptr4=&s4;
s4.ptr4=&s4;
s5.ptr2=&s2;
s5.ptr4=&s4;
s5.ptr5=&s5;
}

void main()
{
build_state_machine();
char input_string[80];
printf("\nEnter input expression:");
scanf("%s",input_string);
parent state_machine(input_string);
parent *ptr;
ptr=s1.transition();
while (ptr->end_state != 1 && ptr->doom_state != 1)
{
ptr=ptr->transition();
}
if (ptr->end_state == 1)
{ printf("\nValid input expression!"); }
else
{ printf("\nInvalid input expression!"); }
getch();
}
帅得不敢出门 2008-12-29
  • 打赏
  • 举报
回复
hmm 哈哈.
xianyuxiaoqiang 2008-12-29
  • 打赏
  • 举报
回复
编译原理学好了很有好处。楼主加油。
baihacker 2008-12-29
  • 打赏
  • 举报
回复
hmm...
xiaopoy 2008-12-29
  • 打赏
  • 举报
回复
hmm,
int IsAlpha(char c);
int IsDigit(char c);
int IsKeyWord(const char* str);
int IsIdentifer(const char* str);
照这样看这4个是唯一值得回复的
baihacker 2008-12-29
  • 打赏
  • 举报
回复

//随便写了点,自己查一下错
#include <stdio.h>

int IsAlpha(char c)
{return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_';}

int IsDigit(char c)
{return c >= '0' && c <= '9';}

int IsKeyWord(const char* str)
{return 0;}

int IsIdentifer(const char* str)
{
//非状态机部分
if (!str) return 0;

//进入初始状态

{
//第一个状态转移到失败状态或者第二个状态
if (!IsAlpha(*str)) return 0;
}

{
//第二个状态
//如果是'\0'则转换到第三个状态
//如果是字符或者数字则停留在第二个状态
//其它字符则转移到失败状态
for (++str; *str; ++str)
if (!IsAlpha(*str) && !IsDigit(*str))
return 0;
}

{
//如果是关键字则失败,否则第四个状态
}

//成功识别
return 1;
}

int main()
{
const char* test[] = {"123","_","a123", "1", "abcdABCD134", "a_b_c_1234", "baihacker"};
int i = 0;
for (i = 0; i < sizeof(test)/sizeof(test[0]); ++i)
printf("%s\t%d\n", test[i], IsIdentifer(test[i]));
return 0;
}

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧