⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄

📁 这是一个词法分析器
💻
📖 第 1 页 / 共 2 页
字号:
#include "stdlib.h"
#include "string.h"
#include "stdio.h"

#define ACC -2
#define syl_if    0
#define syl_else  1
#define syl_while 2
#define syl_begin 3
#define syl_end   4
#define a         5
#define semicolon 6
#define e         7
#define jinghao   8
#define s         9
#define L        10
#define tempsy   11
#define EA       12
#define EO       13
#define plus     14
#define times    15
#define becomes  16
#define op_and   17
#define op_or    18
#define op_not   19
#define rop      20
#define lparent  21
#define rparent  22
#define ident    23
#define intconst 24


char   ch='\0';  /*从字符缓冲区中读取当前字符*/
int    count=0;  /*词法分析结果缓冲区计数器*/
static char spelling[10]={" "};  /*存放识别的字*/
static char line[81]={" "};  /*一行字符缓冲区( 最多 80 个字符)*/
char   *pline;  /*字符缓冲区指针*/
static char ntab1[100][10];  /*变量名表:共100项,每项长度为10*/

struct ntab
       {
	 int tc;  /*真值*/
	 int fc;  /*假值*/
       }ntab2[200];  /*在布尔表达式 ) 中保存有关布尔变量的真、假值*/
int    label=0;  /*指向 ntab2 的指针*/
struct rewords
       {
	 char sp[10];
	 int  sy;
       };   /*匹配表的结构,用来与输入缓冲区中的单词进行匹配*/

struct rewords rewords[8]=
      {{"if",syl_if},
       {"else",syl_else},
       {"while",syl_while},
       {"{",syl_begin},
       {"}",syl_end},
       {"&&",op_and},
       {"||",op_or},
       {"!",op_not}};  /*匹配表初始化,大小为8*/

struct aa{
	 int syl;  /*存放名字*/
	 int pos;  /*存放名字所对应的地址*/
       }buf[100],  /*词法分析结果缓冲区*/
       n,  /*读取二元式的当前字符*/
       n1,  /*当前表达式中的字符*/
       E,  /*非终结符*/
       sstack[100],   /*算术或布尔表达式加工处理使用的符号栈*/
       ibuf[100],  /*算术或布尔表达式使用的缓冲区*/
       stack[1000];  /*语法分析加工处理使用的符号栈*/
struct aa oth;  /*四元式中空白位置*/
struct fourexp
       {
	 char op[10];
	 struct aa arg1;
	 struct aa arg2;
	 int    result;
       }fexp[200]; /*四元式的结构定义*/
int    ssp=0;  /*指向sstack栈指针*/
struct aa *pbuf=buf;  /*指向词法分析缓冲区的指针*/
int    nlength=0;  /*词法分析中记录单词的长度*/
int    tt1=0;  /*变量名表指针*/
FILE   *cfile;  /*源程序文件,~为结束符*/
int    lnum=0;  /*源程序行数记数*/
int    sign=0;  /*sign=0为赋值语句;sign=1 为while语句;sign=3为if语句*/
int    newt=0;  /*临时变量计数器*/
int    nxq=100;  /* nxq 总是指向下一个将要形成的四元式地址,每次执行gen()时,地址自动增1*/
int    lr;  /*扫描LR分析表1过程中保存的当前状态值*/
int    lr1;  /*扫描LR分析表2或表3所保存的当前状态值*/
int    sp=0;  /*查找LR分析表时状态栈的栈顶指针*/
int    stack1[100];  /*状态栈1定义*/
int    sp1=0;  /*状态栈1的栈顶指针*/
int    num=0;  /*算术或布尔表达式缓冲区指针*/
struct ll
       {
	 int nxq1;  /*记录下一条四元式的地址*/
	 int tc1;  /*真值链*/
	 int fc1;  /*假值链*/
       }labelmark[10];  /*记录语句嵌套层次的数组,即记录嵌套中每层的布尔表达式e的首地址*/
int    labeltemp[10];  /*记录语句嵌套层次的数组,即记录每一层else之前的四元式地址*/
int    pointmark=-1;  /*labelmark数组指针*/
int    pointtemp=-1;  /*labeltemp数组指针*/
int    g=0;

static int action[20][11]=
/* 0 */{{   2, -1,  3,  4, -1,  5, -1, -1, -1,  1, -1},
/* 1 */ {  -1, -1, -1, -1, -1, -1, -1, -1,ACC, -1, -1},
/* 2 */ {  -1, -1, -1, -1, -1, -1, -1,  6, -1, -1, -1},
/* 3 */ {  -1, -1, -1, -1, -1, -1, -1,  7, -1, -1, -1},
/* 4 */ {   2, -1,  3,  4, -1,  5, -1, -1, -1,  9,  8},
/* 5 */ {  -1, -1, -1, -1, -1, -1, 10, -1, -1, -1, -1},
/* 6 */ {   2, -1,  3,  4, -1,  5, -1, -1, -1, 11, -1},
/* 7 */ {   2, -1,  3,  4, -1,  5, -1, -1, -1, 12, -1},
/* 8 */ {  -1, -1, -1, -1, 13, -1, -1, -1, -1, -1, -1},
/* 9 */ {   2, -1,  3,  4,105,  5, -1, -1, -1,  9, 14},
/* 10*/ {  -1,104, -1, -1,104, -1, -1, -1,104, -1, -1},
/* 11*/ {  -1, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1},
/* 12*/ {  -1,102, -1, -1,102, -1, -1, -1,102, -1, -1},
/* 13*/ {  -1,103, -1, -1,103, -1, -1, -1,103, -1, -1},
/* 14*/ {  -1, -1, -1, -1,106, -1, -1, -1, -1, -1, -1},
/* 15*/ {   2, -1,  3,  4, -1,  5, -1, -1, -1, 16, -1},
/* 16*/ {  -1,101, -1, -1,101, -1, -1, -1,101, -1, -1}};



static int action1[10][7]=
/* 0 */  {{  3, -1, -1,  2, -1, -1,  1},
/* 1 */   { -1,  4,  5, -1, -1,ACC, -1},
/* 2 */   {  3, -1, -1,  2, -1, -1,  6},
/* 3 */   { -1,104,104, -1,104,104, -1},
/* 4 */   {  3, -1, -1,  2, -1, -1,  7},
/* 5 */   {  3, -1, -1,  2, -1, -1,  8},
/* 6 */   { -1,  4,  5, -1,  9, -1, -1},
/* 7 */   { -1,101,  5, -1,101,101, -1},
/* 8 */   { -1,102,102, -1,102,102, -1},
/* 9 */   { -1,103,103, -1,103,103, -1}};


static int action2[16][11]=
/* 0 */  {{  1, -1,  4, -1,  5, -1, -1, -1, 13,  7,  8},
/* 1 */   {  1,  2, -1,101, -1,101,101,101, -1, -1, -1},
/* 2 */   {  3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
/* 3 */   { -1, -1, -1,102, -1,102,102,102, -1, -1, -1},
/* 4 */   {  1, -1,  4, -1,  5, -1, -1, -1, 11,  7,  8},
/* 5 */   {  1, -1,  4, -1,  5, -1, -1, -1,  6,  7,  8},
/* 6 */   { -1, -1, -1,104, -1,  9, 10,104, -1, -1, -1},
/* 7 */   {  1, -1,  4, -1,  5, -1, -1, -1, 14,  7,  8},
/* 8 */   {  1, -1,  4, -1,  5, -1, -1, -1, 15,  7,  8},
/* 9 */   {105, -1,105, -1,105, -1, -1, -1, -1, -1, -1},
/*10 */   {107, -1,107, -1,107, -1, -1, -1, -1, -1, -1},
/*11 */   { -1, -1, -1, 12, -1,  9, 10, -1, -1, -1, -1},
/*12 */   { -1, -1, -1,103, -1,103,103,103, -1, -1, -1},
/*13 */   { -1, -1, -1, -1, -1,  9, 10,ACC, -1, -1, -1},
/*14 */   { -1, -1, -1,106, -1,  9, 10,106, -1, -1, -1},
/*15 */   { -1, -1, -1,108, -1,  9, 10,108, -1, -1, -1}};


/**********************************/
readline()
{
  char ch1;
  int i;
  for(i=0;i<81;i++) line[i]='';

  pline=line;
  ch1=getc(cfile);
  while(ch1!='\n')
  {
    *pline=ch1;
    pline++;
    ch1=getc(cfile);
  }
  *pline='\0';
  pline=line;
}


/****************************/
readch()
{
  if(ch=='\0')
  {
    readline();
    lnum++;
  }
  ch=*pline;
  pline++;
}

/*******************************/
find(char spel[])
{
  int ss1=0;
  int ii=0;
  while((ss1==0) && (ii<nlength))
  {
   if(!strcmp(spel,ntab1[ii])) ss1=1;
   ii++;
  }
  if (ss1==1) return ii-1;
  else return -1;
}

/**********************************/
identifier()
{
  int iii=0,j,k;
  int ss=0;
  k=0;
  do
  {
    spelling[k]=ch;
    k++;
    readch();
  }while(((ch>='a')&&(ch<='z'))||((ch>='0')&&(ch<='9'))|| ch=='&' || ch=='|');
  pline--;
  spelling[k]='\0';
  while((ss==0)&&(iii<8))
  {
    if(!strcmp(spelling,rewords[iii].sp)) ss=1;
    iii++;
  }

  if(ss==1)
  {
    buf[count].syl=rewords[iii-1].sy;
  }
  else
  {
    buf[count].syl=ident;
    j=find(spelling);
    if(j==-1)
    {
      buf[count].pos=tt1;
      strcpy(ntab1[tt1],spelling);
      tt1++;
      nlength++;
    }
    else buf[count].pos=j;
  }
  count++;
  for(k=0;k<10;k++) spelling[k]='';
}

/******************************/
number()
{
  int ivalue=0;
  int digit;
  do
  {
   digit=ch-'0';
   ivalue=ivalue*10+digit;
   readch();
   }while((ch>='0')&&(ch<='9'));
   buf[count].syl=intconst;
   buf[count].pos=ivalue;
   count++;
   pline--;
}

/**********************************/
scan()
{
  int i;
  while(ch!='~')
  {
   switch(ch)
   {
     case ' ': break;


     case 'a':
     case 'b':
     case 'c':
     case 'd':
     case 'e':
     case 'f':
     case 'g':
     case 'h':
     case 'i':
     case 'j':
     case 'k':
     case 'l':
     case 'm':
     case 'n':
     case 'o':
     case 'p':
     case 'q':
     case 'r':
     case 's':
     case 't':
     case 'u':
     case 'v':
     case 'w':
     case 'x':
     case 'y':
     case 'z':
     case '{':
     case '}':
     case '&':
     case '|':
     case '!':
	       identifier();
	       break;

     case '0':
     case '1':
     case '2':
     case '3':
     case '4':
     case '5':
     case '6':
     case '7':
     case '8':
     case '9': number();
	       break;
     case '<':
	      readch();
	      if(ch=='=')
	      {
		buf[count].pos=0;
	      }
	      else
	      {
		if(ch=='>') buf[count].pos=4;
		else
		{
		  buf[count].pos=1;
		  pline--;
		}
	      }
	      buf[count].syl=rop;
	      count++;
	      break;

     case '>':
	      readch();
	      if(ch=='=')
	      {
		 buf[count].pos=2;
	      }
	      else
	      {
		 buf[count].pos=3;
		 pline--;
	      }
	      buf[count].syl=rop;
	      count++;
	      break;
     case '(':
	      buf[count].syl=lparent;
	      count++;
	      break;
     case ')':
	      buf[count].syl=rparent;
	      count++;
	      break;
     case '#':
	      buf[count].syl=jinghao;
	      count++;
	      break;
     case '+':
	      buf[count].syl=plus;
	      count++;
	      break;
     case '*':
	      buf[count].syl=times;
	      count++;
	      break;
     case '=':
	      readch();
	      if(ch=='=')
	      {
		buf[count].syl=rop;
		buf[count].pos=5;
	      }
	      else
	      {
		buf[count].syl=becomes;
		pline--;
	      }
	      count++;
	      break;
     case ';':
	      buf[count].syl=semicolon;
	      count++;
	      break;
     }

     readch();
   }
   buf[count].syl=-1;
}

/******************************/
readnu()
{
   if(pbuf->syl>=0)
   {
      n.syl=pbuf->syl;
      n.pos=pbuf->pos;
      pbuf++;
   }
}

/*******************************/
newtemp()
{
   newt++;
   return newt;
}

/******************************/
gen(char op1[],struct aa arg11,struct aa arg22,int result1)
{
   strcpy(fexp[nxq].op,op1);
   fexp[nxq].arg1.syl=arg11.syl;
   fexp[nxq].arg1.pos=arg11.pos;
   fexp[nxq].arg2.syl=arg22.syl;
   fexp[nxq].arg2.pos=arg22.pos;
   fexp[nxq].result=result1;
   nxq++;
   return nxq-1;
}


/******************************/
merg(int p1,int p2)
{
   int p;
   if(p2==0) return p1;
   else
   {
     p=p2;
     while(fexp[p].result!=0) p=fexp[p].result;
     fexp[p].result=p1;
     return p2;
   }
}

/***************************/
backpatch(int p,int t)
{
   int tempq;
   int q;
   q=p;
   while(q!=0)
   {
      tempq=fexp[q].result;
      fexp[q].result=t;
      q=tempq;
   }
}

/**************************/
change1(int chan)
 {
  switch(chan)
  {
   case ident:
   case intconst: return 0;
   case plus:     return 1;
   case times:    return 2;
   case lparent:  return 3;
   case rparent:  return 4;
   case jinghao:  return 5;
   case tempsy:   return 6;
  }
}

/***************************/
change2(int chan)
{
  switch(chan)
  {
  case ident:
  case intconst: return 0;
  case rop:      return 1;
  case lparent:  return 2;
  case rparent:  return 3;
  case op_not:   return 4;
  case op_and:   return 5;
  case op_or:    return 6;
  case jinghao:  return 7;
  case tempsy:   return 8;
  case EA:       return 9;
  case EO:       return 10;
  }
}

lrparse1(int num)
{
   lr1=action1[stack1[sp1]][change1(n1.syl)];
   if(g++==4) getch();
   printf("\tstack1[%d]=%d\t\t n1=%d\t\tlr1=%d\n",sp1,stack[sp1].pos,n1.syl,lr1);
   if(lr1==-1)
   {
      printf("\n suan shu or fu zhi Error!\n");
      getch();
      /*exit(0);  */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -