编译原理实验一——简单词法分析

来源:互联网 发布:java开源大全注册 编辑:程序博客网 时间:2024/05/21 21:38

[实验任务]
完成以下正则文法所描述的Pascal语言子集单词符号的词法分析程序。
<标识符>→字母︱ <标识符>字母︱ <标识符>数字
    <无符号整数>→数字︱ <无符号整数>数字
    <单字符分界符> →+ ︱- ︱* ︱; ︱(︱)
    <双字符分界符>→<大于>=︱<小于>=︱<小于>>︱<冒号>=︱<斜竖>*
    <小于>→<                   
<等于>→=
<大于>→>
<冒号> →:
<斜竖> →/
该语言的保留字 :begin  end  if  then  else  for  do  while  and or not   说明: 1 该语言大小写不敏感。
     2 字母为a-z A-Z,数字为0-9。
3可以对上述文法进行扩充和改造。
4 ‘/*……*/’为程序的注释部分。
[设计要求]
1、给出各单词符号的类别编码。
2、词法分析程序应能发现输入串中的错误。
3、词法分析作为单独一遍编写,词法分析结果为二元式序列组成的中间文件。
4、设计两个测试用例(尽可能完备),并给出测试结果。 

demo.cpp

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "demo.h"

char token[20];

int lookup(char *token) {
 for (int i = 0; i < 11; i++) {
  if (strcmp(token, KEY_WORDS[i]) == 0) {
   return i+1;
  }
 }

 return 0;
}

char getletter(FILE *fp) {
 return tolower(fgetc(fp));
}

void out(FILE *fp, int c, char *value) {
 fprintf(fp, "%d,%s/n", c, value);
}

void report_error(FILE *fp, char ch) {
 fprintf(fp, "There must be some error./n");
 fprintf(fp, "%c.../n", ch);
 fprintf(fp, "^/n");
}

void scanner(FILE *infile, FILE *outfile) {
 char ch;
 int i, c;
 do {
  do {
   ch = getletter(infile);
  } while(isspace(ch));
  if (isalpha(ch)) {
   token[0] = ch;
   ch = getletter(infile);
   i = 1;
   while (isalnum(ch)) {
    token[i] = ch;
    i++;
    ch = getletter(infile);
   }
   token[i] = '/0';
   fseek(infile, -1, 1);
   c = lookup(token);
   if (c == 0) {
    out(outfile, ID, token);
   }
   else {
    out(outfile, c, " ");
   }
  }
  else {
   if (isdigit(ch)) {
    token[0] = ch;
    ch = getletter(infile);
    i = 1;
    while (isdigit(ch)) {
     token[i] = ch;
     i++;
     ch = getletter(infile);
    }
    token[i] = '/0';
    fseek(infile, -1, 1);
    out(outfile, INT, token);
   }
   else {
    switch(ch) {
    case '<':
     {
      ch = getletter(infile);
      if (ch == '=') {
       out(outfile, LE, " ");
      }
      else if (ch == '>') {
       out(outfile, NE, " ");
      }
      else {
       fseek(infile, -1, 1);
       out(outfile, LT, " ");
      }
      break;
     }
    case '=':
     {
      out(outfile, EQ, " ");
      break;
     }
    case '>':
     {
      ch = getletter(infile);
      if (ch == '=') {
       out(outfile, GE, " ");
      }
      else {
       fseek(infile, -1, 1);
       out(outfile, GT, " ");
      }
      break;
     }
    case ':':
     {
      ch = getletter(infile);
      if (ch == '=') {
       out(outfile, FU, " ");
      }
      else {
       fseek(infile, -1, 1);
       out(outfile, MAO, " ");
      }
      break;
     }
    case '/':
     {
      ch = getletter(infile);
      if (ch == '*') {
       out(outfile, ZHU, " ");
      }
      else {
       fseek(infile, -1, 1);
       out(outfile, XIE, " ");
      }
      break;
     }
    case '+':
     {
      out(outfile, JIA, " ");
      break;
     }
    case '-':
     {
      out(outfile, JIAN, " ");
      break;
     }
    case '*':
     {
      out(outfile, CHEN, " ");
      break;
     }
    case ';':
     {
      out(outfile, FEN, " ");
      break;
     }
    case '(':
     {
      out(outfile, ZUO, " ");
      break;
     }
    case ')':
     {
      out(outfile, YOU, " ");
      break;
     }
    default:
     {
      if (ch != EOF) {
       report_error(outfile, ch);
      }
      break;
     }
    }
   }
  }
 } while(ch != EOF);

 return;
}

void main() {
 FILE *in, *out;

 in = fopen("pascal.txt", "r");
 out = fopen("result.txt", "w");

 scanner(in, out);
 fprintf(out, "0,#");

 fclose(in);
 fclose(out);

 printf("Finished!/n");
}