• C++写的程序识别Pascal代码


    学习编译技术,老师出了个题目:给我们一份简单的pascal代码,让我们用C语言识别代码中的单词,并分析出单词的类别,没学过C,我直接用C++写了一份!

    用C++写代码的时候,感觉没用到什么编译技术里的知识,郁闷!老师出的是什么题目啊!

    贴一下代码,希望给感兴趣的刚入门的朋友一些小小的帮助!

    pascal代码:

    Begin
      f:=1;
      lastf:=0;
      n:=0;
      For I:=1 To 20 Do
      Begin
       if(n mod 4 = 0) THEN WRITELN;
      End
    End

    C++程序源代码:

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cctype>
    using namespace std;

    // global var
    const char * keywords [9] = {"begin", "end", "for", "to", "do", "if", "then", "writeln", "mod"}; // 关键字
    const char op_signs [2] = {':', '='};  // 运算符
    const char divid_signs[3] = {';', '(', ')'}; // 边界符

    const char * szPath = "d:\code.txt"; // 文件路径

    // function
    bool IsEnd(char ch);
    bool IsAlpha(char ch);
    bool IsDigit(char ch);
    bool IsDivid(char ch);
    bool IsOprate(char ch);
    void Back(fstream * f);
    char * ToLower(char * str);
    bool IsKeyWord(string strWord);
    string Append(string str, char ch);
    void StrToCharArr(char *pArr, string str);


    void main()
    {
     fstream fi(szPath, ios::in); // Read Style
     if(!fi)
     {
      cout << "file open faild!" << endl;
      return;
     }
     char ch;
     string strWord = "";
     char Word[100];
     ch = fi.get();
     while(ch != EOF)
     {
      
      if(IsAlpha(ch)) // 字母
      {
       while(!(IsEnd(ch) || IsOprate(ch) || IsDivid(ch)))
       {
        strWord = Append(strWord, ch);
        ch = fi.get();
        if(ch == EOF)
         return;
       }
       if(IsKeyWord(strWord))
        cout << strWord+"关键字" << endl;
       else
        cout << strWord+"标识符" << endl;
       if(IsOprate(ch) || IsDivid(ch))
        Back(&fi);
       strWord = "";
      }
      else if(IsOprate(ch)) // 运算符
      {
       while(!(IsEnd(ch) || IsDigit(ch) || IsDivid(ch) || IsAlpha(ch)))
       {
        strWord = Append(strWord, ch);
        ch = fi.get();
        if(ch == EOF)
         return;
       }

       cout << strWord+"运算符" << endl;

       if(IsDivid(ch) || IsAlpha(ch) || IsDigit(ch))
        Back(&fi);
       strWord = "";
      }
      else if(IsDigit(ch)) // 数值
      {
       while(!(IsEnd(ch) || IsOprate(ch) || IsDivid(ch) || IsAlpha(ch)))
       {
        strWord = Append(strWord, ch);
        ch = fi.get();
        if(ch == EOF)
         return;
       }

       cout << strWord+"数值" << endl;

       if(IsDivid(ch) || IsAlpha(ch) || IsOprate(ch))
        Back(&fi);
       strWord = "";
      }
      else if(IsDivid(ch)) // 边界符
      {
       while(!(IsEnd(ch) || IsOprate(ch) || IsDigit(ch) || IsAlpha(ch)))
       {
        strWord = Append(strWord, ch);
        ch = fi.get();
        if(ch == EOF)
         return;
       }

       cout << strWord+"边界符" << endl;

       if(IsDigit(ch) || IsAlpha(ch) || IsOprate(ch))
        Back(&fi);
       strWord = "";
      }

      ch = fi.get();
     } 
    }

    // 判断是否是字母
    bool IsAlpha(char ch)
    {
     if(ch>=65 && ch <= 122)
      return true;
     else
      return false;
    }

    // 判断是否是数字
    bool IsDigit(char ch)
    {
     if(ch >=48 && ch <=57)
      return true;
     else
      return false;
    }

    // 判断是否是空格、换行符
    bool IsEnd(char ch)
    {
     if(ch == 10 || ch == 32)
      return true;
     else
      return false;
    }

    // 判断是否是边界符
    bool IsDivid(char ch)
    {
     for(int i=0; i<3; i++)
     {
      if(ch == divid_signs[i])
       return true;
     }
     return false;
    }

    // 判断是否是关键字
    bool IsKeyWord(string strWord)
    {
     char pword [100];
     StrToCharArr(pword,strWord);
        for(int i=0; i<9; i++)
     {
      ToLower(pword);
      if(0==strcmp(keywords[i], pword))
       return true;
      else
       continue;
     }
     return false;
    }

    // 判断是否是运算符
    bool IsOprate(char ch)
    {
     for(int i=0; i<2; i++)
     {
      if(ch == op_signs[i])
       return true;
     }
     return false;
    }

    // 文件指针后移
    void Back(fstream * f)
    {
     f->seekg(-1, ios::cur);
    }

    // 组装单词
    string Append(string str, char ch)
    {
     str += ch;
     return str;
    }

    // 将字符串转换为小写
    char * ToLower(char * str)
    {
     int i=0;
     while(str[i] != '\0')
     {
      str[i] = tolower(str[i]);
      i++;
     }
     return str;
    }

    // 将字符串转换为字符数组
    void StrToCharArr(char *pArr, string str)
    {
     for(int i=0; i<= str.length(); i++)
     {
      pArr[i] = str[i];
     }
    }

  • 相关阅读:
    批量kill 进程
    MySQL数据库多表查询和事务
    js方法执行中休眠几秒
    js中 .map()和.filter()以及他们的区别
    封装:elementui之confirm消息提示框、确认提示弹框
    配置Maven环境变量
    idea 缺失右侧maven窗口
    js中 判断数组 是否包含某元素 Array.indexOf("x")== 1
    idea 如何将项目改成maven项目 在pom.xml 文件上右键 Add as Maven Project
    C#的进化——C#发展史、C#1.010.0语法系统性梳理、C#与JAVA的对比 致敬作者
  • 原文地址:https://www.cnblogs.com/CPFlying/p/1700607.html
Copyright © 2020-2023  润新知