• 大小: 954KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-07-15
  • 语言: 其他
  • 标签: sample  完整代码  

资源简介

检查要求: a) 启动程序后,先输出作者姓名、班级、学号(可用汉语、英语或拼音); b) 请求输入测试程序名,键入程序名后自动开始词法分析并输出结果; c) 输出结果为单词的二元式序列(样式见样板输出1和2); d) 要求能发现下列词法错误和指出错误性质和位置: 非法字符,即不是SAMPLE字符集的符号; 字符常数缺右边的单引号(字符常数要求左、右边用单引号界定,不能跨行); 注释部分缺右边的界符*/(注释要求左右边分别用/*和*/界定,不能跨行)。

资源截图

代码片段和文件信息

#include
#include
#include
#include
#include
using namespace std;

struct node{           
string word;
string type;
};

void display();    //输出本人的信息
void file();       //输出分析结果

void main()
{
    display();
file();
int s;   
cout<<“请输入数字:1.继续  0.退出“<    cin>>s;
    while(s==1){
if(s==1){ 
file();
cout<<“请输入数字:1.继续  0.退出“<    cin>>s;
}
if(s==0){
  exit(0);
}
}
}

void file()
{
node A[35];  //保留字
node B[22];   //39-60是单界符和双界符
A[0].word=“and“; A[0].type=“1“;
A[1].word=“array“; A[1].type=“2“;
A[2].word=“begin“; A[2].type=“3“;
A[3].word=“bool“; A[3].type=“4“;
A[4].word=“call“; A[4].type=“5“;
A[5].word=“case“; A[5].type=“6“;
A[6].word=“char“; A[6].type=“7“;
A[7].word=“constant“; A[7].type=“8“;
A[8].word=“dim“; A[8].type=“9“;
A[9].word=“do“; A[9].type=“10“;
A[10].word=“else“; A[10].type=“11“;
A[11].word=“end“; A[11].type=“12“;
A[12].word=“false“; A[12].type=“13“;
A[13].word=“for“; A[13].type=“14“;
A[14].word=“if“; A[14].type=“15“;
A[15].word=“input“; A[15].type=“16“;
A[16].word=“integer“; A[16].type=“17“;
A[17].word=“not“; A[17].type=“18“;
A[18].word=“of“; A[18].type=“19“;
A[19].word=“or“; A[19].type=“20“;
A[20].word=“output“; A[20].type=“21“;
A[21].word=“procedure“; A[21].type=“22“;
A[22].word=“program“; A[22].type=“23“;
A[23].word=“read“; A[23].type=“24“;
A[24].word=“real“; A[24].type=“25“;
A[25].word=“repeat“; A[25].type=“26“;
A[26].word=“set“; A[26].type=“27“;
A[27].word=“stop“; A[27].type=“28“;
A[28].word=“then“; A[28].type=“29“;
A[29].word=“to“; A[29].type=“30“;
A[30].word=“true“; A[30].type=“31“;
A[31].word=“until“; A[31].type=“32“;
A[32].word=“var“; A[32].type=“33“;
A[33].word=“while“; A[33].type=“34“;
A[34].word=“write“; A[34].type=“35“;
B[0].word=“(“; B[0].type=“39“;
B[1].word=“)“; B[1].type=“40“;
B[2].word=“*“; B[2].type=“41“;
B[3].word=“*/“; B[3].type=“42“;
B[4].word=“+“; B[4].type=“43“;
B[5].word=““; B[5].type=“44“;
B[6].word=“-“; B[6].type=“45“;
B[7].word=“.“; B[7].type=“46“;
B[8].word=“..“; B[8].type=“47“;
B[9].word=“/“; B[9].type=“48“;
B[10].word=“/*“; B[10].type=“49“;
B[11].word=“:“; B[11].type=“50“;
B[12].word=“:=“; B[12].type=“51“;
B[13].word=“;“; B[13].type=“52“;
B[14].word=“<“; B[14].type=“53“;
B[15].word=“<=“; B[15].type=“54“;
B[16].word=“<>“; B[16].type=“55“;
B[17].word=“=“; B[17].type=“56“;
B[18].word=“>“; B[18].type=“57“;
B[19].word=“>=“; B[19].type=“58“;
B[20].word=“[“; B[20].type=“59“;
B[21].word=“]“; B[21].type=“60“;

char ch[100];
ifstream infile;              //定义一个输入流对象
cout<<“请输入需分析的文件路径及名称:“< cin>>ch;
infile.open(chios::in);  //打开文件
if(!infile)
{
cout<<“文件不存在或无法打开“< }

cout<
char buffer[50];              //比较字符串
char* s=new char[100];   //定义输入的字符串
int line=0;

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      74752  2013-12-04 22:48  词法分析器\Debug\vc60.idb

     文件     118784  2013-12-04 18:23  词法分析器\Debug\vc60.pdb

     文件     593983  2013-12-04 18:23  词法分析器\Debug\词法分析器.exe

     文件     840648  2013-12-04 18:23  词法分析器\Debug\词法分析器.ilk

     文件     392126  2013-12-04 18:23  词法分析器\Debug\词法分析器.obj

     文件    2177588  2013-12-04 17:21  词法分析器\Debug\词法分析器.pch

     文件    1147904  2013-12-04 18:23  词法分析器\Debug\词法分析器.pdb

     文件        301  2013-12-02 23:26  词法分析器\text1.txt

     文件        126  2013-12-03 13:27  词法分析器\text2.txt

     文件        145  2013-12-03 13:27  词法分析器\text3.txt

     文件       8398  2013-12-04 18:23  词法分析器\词法分析器.cpp

     文件       3451  2013-12-04 16:53  词法分析器\词法分析器.dsp

     文件        528  2013-12-05 00:29  词法分析器\词法分析器.dsw

     文件      41984  2013-12-05 00:29  词法分析器\词法分析器.ncb

     文件      48640  2013-12-05 00:29  词法分析器\词法分析器.opt

     文件        774  2013-12-04 18:23  词法分析器\词法分析器.plg

     目录          0  2013-12-04 18:23  词法分析器\Debug

     目录          0  2013-12-05 00:29  词法分析器

----------- ---------  ---------- -----  ----

              5450132                    18


评论

共有 条评论