91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 6KB
    文件類型: .cpp
    金幣: 1
    下載: 0 次
    發(fā)布日期: 2022-11-30
  • 語言: C/C++
  • 標簽: 詞法分析??

資源簡介

一、實驗任務:編制一個讀單詞過程,從輸入的源程序中,識別出各個具有獨立意義的單詞,即基本保留字、標識符、常數(shù)、運算符、分隔符五大類。并依次輸出各個單詞的內(nèi)部編碼及單詞符號。(遇到錯誤時可顯示“Error”,然后跳過錯誤部分繼續(xù)顯示)

資源截圖

代碼片段和文件信息

#include?
#include?
#include?
#include?
#include?

using?namespace?std;

ifstream?infile(“Test.txt“ios::in);
ofstream?outfile(“Answear.txt“ios::out);
char?cbuffer;
char?*key[13]={“var““const““begin““end““while““do““read““write““if““then““procedure““call““odd“};??
char?*border[5]={?““??“;“???“(“??“)“?“.“};????????????????//分界符
char?*arithmetic[4]={“+“??“-“??“*“??“/“?};????????????????????//運算符
char?*relation[7]={“<“??“=“??“>“??“#“??“<=“??“>=“?“:=“};?????????????//關系運算符???


int?search(char?searchchar[]int?wordtype)
{
????int?i=0t=0;
????switch?(wordtype)
{
???????case?1:
???{?
???for?(i=0;i<=12;i++)?????????//關鍵字
???{
??????????????????if?(strcmp(key[i]searchchar)==0)
???????????????????? return(i+1);
???}
????????????????return(0);
???}
???????case?2:
???{
????????????for?(i=0;i<=4;i++)????//分界符
{
???????????if?(strcmp(border[i]searchchar)==0)
???????????????return(i+1);
}?
????????????return(0);
???}??
????????case?3:
{
????????????????for?(i=0;i<=3;i++)????//運算符
{
???????????????if?(strcmp(arithmetic[i]searchchar)==0)
??return(i+1);
}
return(0);
}??
????????case?4:
{
??for?(i=0;i<=6;i++)????//關系運算符
??{
if?(strcmp(relation[i]searchchar)==0)
????????????????return(i+1);
??}
?????????????????return(0);
}
????????case?5:???//空格
return?(1);
?
}
}


char?alphaprocess(char?buffer)????????????????//字符處理過程
{
?int?atype;
?int?i=-1;
?char?alphatp[20];
?????while?((isalpha(buffer))||(isdigit(buffer)))??//這兩個函數(shù)分別是判字符和判數(shù)字函數(shù)位于ctype.h中
?{
??alphatp[++i]=buffer;
??infile.get(buffer);
?}
?????alphatp[i+1]=‘\0‘;//在末尾添加字符串結束標志
?????if?(atype=search(alphatp1))
?outfile< ?else
?{
?atype=search(alphatp6);??????????????//標識符
?????????outfile<<“ident????\““< ?}
?????return(buffer);
}

char?digitprocess(char?buffer)??//數(shù)字處理過程
{
?int?i=-1;
?char?digittp[20];
?int?dtype;
??????while?((isdigit(buffer)))
??{
??digittp[++i]=buffer;
??infile.get(buffer);
??}
?digittp[i+1]=‘\0‘;
?dtype=search(digittp5);
?outfile<<“number????\““< ?return(buffer);
}

char?otherprocess(char?buffer)???//分界符、運算符、邏輯運算符、等
{
?int?i=-1;
?char?othertp[20];
?int?otypeotypetp;
?othertp[0]=buffer;
?othertp[1]=‘\0‘;
??????if?(otype=search(othertp3))???//運算符
??{
??????????infile.get(buffer);
????????switch(otype-1){ ??
??case?0:?outfile<<“plus????????????\“?“< ??case?1:?outfile<<“subtract????????\“?“<

評論

共有 條評論