CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C++ 语言

pl0 词法分析

楼主qlks(强力扣杀)2004-12-03 22:45:24 在 C/C++ / C++ 语言 提问

谁有C++写的pl0词法分析,发我一份,只要词法分析的。谢  
  lovelydavid@hotmail.com 问题点数:100、回复次数:2Top

1 楼o1n(小毛子)回复于 2004-12-03 23:14:48 得分 100

#define   MAX   1000  
  #include   <stdio.h>  
  #include   <iostream.h>  
  #include   <iomanip.h>  
  #include   <stdlib.h>  
  #include   <string.h>  
   
  int   inputpro();  
  int   sylex();  
  int   cleararray();  
  int   judgeword();  
  char   *judgeoperator(char   *);  
  char   buffer[MAX];  
  char   word[MAX];  
  char   *p;  
  int   x=1;  
  int   type=1;  
  int   m=0;  
   
  int   main()  
  {  
  inputpro();  
  sylex();  
  return   0;  
  }  
   
  int   inputpro()     //输入处理函数  
  {  
     
      int   ch,i=0;  
      printf(   "请输入程序代码段,以'#'作为结尾:\n"   );  
      while   ((ch   =   getchar())   !=   '#')   {  
          buffer[i]   =   ch;  
  i++;  
      }  
      return   0;  
  }  
   
  int   sylex()     //词法分析函数  
  {  
  int   count=0;  
  p=buffer;  
  count   =   strlen(buffer);  
  printf("\n");  
  printf("               **词法分析表**                                 \n");  
  printf("\n");  
  printf("序号           单词         类别             自身值   \n");  
  for   (p=buffer;p<buffer+count;p++)   {  
  if   (*p   ==   '   '   ||   *p   ==   '\n'   ||   *p   ==   '+'   ||   *p   ==   '-'   ||   *p   ==   '*'   ||   *p   ==   '/'   ||  
  *p   ==   ':'   ||   *p   ==   '<'   ||   *p   ==   '>'   ||   *p   ==   '='   ||   *p   ==   '('   ||   *p   ==   ')'   ||    
  *p   ==   ';')   {  
  judgeword();  
  p=judgeoperator(p);  
  continue;  
  }  
  word[m++]   =   *p;  
  }  
   
  return   0;  
  }  
   
  int   cleararray()   //清空单词数组  
  {  
  int   count=0;  
  int   i=0;  
  count   =   strlen(word);  
  for   (i=0;i<count;i++)   {  
  word[i]   =   '\0';  
  }  
  return   0;  
  }  
   
  int   judgeword()       //判断关键字以及整常数  
  {  
  if   (strcmp(word,   "if")   ==   0)   {  
  type   =   3;  
  cout<<setw   (3)<<   x++   <<setw   (10)<<word<<setw   (7)<<type<<endl;  
  cleararray();  
  m=0;  
   
  }  
   
  else   if   (strcmp(word,   "then")==   0)   {  
  type   =   4;  
  cout<<setw   (3)<<   x++   <<setw   (10)<<word<<setw   (7)<<type<<endl;  
  cleararray();  
  m=0;  
  }  
   
  else   if   (strcmp(word,   "else")   ==   0)   {  
  type   =   5;  
  cout<<setw   (3)<<   x++   <<setw   (10)<<word<<setw   (7)<<type<<endl;  
  cleararray();  
  m=0;  
  }  
  else   if   (word[0]>='0'   &&   word[0]<='9')   {     //打印整常数  
  type   =2;  
  cout<<setw   (3)<<   x++   <<setw   (10)<<word<<setw   (7)<<type<<setw   (12)<<word<<endl;  
  cleararray();  
  m=0;  
  }  
  else   if   (strcmp(word,   "")!=0)     //打印标识符  
  {  
  type   =1;  
  cout<<setw   (3)<<   x++   <<setw   (10)<<word<<setw   (7)<<type<<setw   (12)<<word<<endl;  
  cleararray();  
  m=0;  
  }  
   
  return   0;  
  }  
   
  char   *judgeoperator(char   *p)  
  {  
  if   (*p   ==   '+')   {  
  word[0]   =   *p;  
  type   =   6;  
  cout<<setw   (3)<<   x++   <<setw   (10)<<word<<setw   (7)<<type<<endl;  
  cleararray();  
  }  
   
  if   (*p   ==   '-')   {  
  word[0]   =   *p;  
  type   =   7;  
  cout<<setw   (3)<<   x++   <<setw   (10)<<word<<setw   (7)<<type<<endl;  
  cleararray();  
  }  
   
  if   (*p   ==   '*')   {  
  word[0]   =   *p;  
  type   =   8;  
  cout<<setw   (3)<<   x++   <<setw   (10)<<word<<setw   (7)<<type<<endl;  
  cleararray();  
  }  
   
  if   (*p   ==   '/')   {  
  word[0]   =   *p;  
  type   =   9;  
  cout<<setw   (3)<<   x++   <<setw   (10)<<word<<setw   (7)<<type<<endl;  
  cleararray();  
  }  
   
  if   (*p   ==   ':')   {         //处理:=符号,暂时没有出错处理  
  word[0]   =   *p;  
  p++;  
  word[1]   =   *p;  
  type   =   10;  
  cout<<setw   (3)<<   x++   <<setw   (10)<<word<<setw   (7)<<type<<endl;  
  cleararray();  
  }  
  else   if   (*p   ==   '=')   {  
  word[0]   =   *p;  
  type   =   13;  
  cout<<setw   (3)<<   x++   <<setw   (10)<<word<<setw   (7)<<type<<endl;  
  cleararray();  
  }  
   
  if   (*p   ==   '<')   { //处理<>符号  
  word[0]   =   *p;  
  if   (*++p   ==   '>')   {  
  word[1]   =   *p;  
  type   =   14;  
  cout<<setw   (3)<<   x++   <<setw   (10)<<word<<setw   (7)<<type<<endl;  
  cleararray();  
  }  
  else   {  
  --p;  
  cleararray();  
  word[0]   =   *p;  
  type   =   11;  
  cout<<setw   (3)<<   x++   <<setw   (10)<<word<<setw   (7)<<type<<endl;  
  cleararray();  
  }  
  }  
  else   if   (*p   ==   '>')   {  
  word[0]   =   *p;  
  type   =   12;  
  cout<<setw   (3)<<   x++   <<setw   (10)<<word<<setw   (7)<<type<<endl;  
  cleararray();  
  }  
   
  if   (*p   ==   '(')   {  
  word[0]   =   *p;  
  type   =   15;  
  cout<<setw   (3)<<   x++   <<setw   (10)<<word<<setw   (7)<<type<<endl;  
  cleararray();  
  }  
   
  if   (*p   ==   ')')   {  
  word[0]   =   *p;  
  type   =   17;  
  cout<<setw   (3)<<   x++   <<setw   (10)<<word<<setw   (7)<<type<<endl;  
  cleararray();  
  }  
   
  if   (*p   ==   ';')   {  
  word[0]   =   *p;  
  type   =   17;  
  cout<<setw   (3)<<   x++   <<setw   (10)<<word<<setw   (7)<<type<<endl;  
  cleararray();  
  }  
   
  return   p;  
  }Top

2 楼greenteanet(扎扎实实打基础,保持一颗平常心。)回复于 2004-12-03 23:17:26 得分 0

不好意思,我没有Top

相关问题

  • 词法分析器(用c)
  • LEX词法分析器怎么用?
  • 词法分析器的算法,救急?!!!
  • 编译中的词法分析相关
  • 高分求助!!词法分析程序
  • “词法分析”,英文怎么说?
  • 词法分析程序设计问题
  • 谁有java写的词法分析程序.象pl0那样的.
  • 如何做RTF文件词法分析器???
  • 请大家给个方案:关于中文词法分析?

关键词

  • 分析
  • word
  • inputpro
  • sylex
  • 词法分析
  • buffer
  • printf
  • ch
  • max
  • count

得分解答快速导航

  • 帖主:qlks
  • o1n

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo