資源簡介
實(shí)現(xiàn)了一個可用的C++語法分析器,從文件讀取源代碼,從左到右掃描每行該語言源程序的符號,拼成單詞,換成統(tǒng)一的內(nèi)部表示(token)。識別標(biāo)識符、數(shù)字、特殊字符、關(guān)鍵字等類別,刪除注釋行和空白符,打印出每行包含的記號的二元形式到目標(biāo)文件中去

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Threading.Tasks;
using?System.IO;
using?System.Windows.Forms;
using?System.Collections;
namespace?詞法分析
{
????class?analyseToken
????{
????????protected?string?sourceFileName;
????????protected?string?builtFileName;
????????//Stack?st?=?new?Stack();?????????//用于括號匹配的棧
????????char[]?prog?=?new?char?[1000];??//暫存輸入字符
????????char[]?token?=?new?char[20];
????????char?ch;
????????int?p?=?0;
????????int?sym?=?0;
????????int?n;
????????int?errorLine;????//發(fā)生錯誤的行號
????????string[]?keyword?=?{?“if“?“else“?“while“?“for“?“do“?“return“
?????????“int“?“main““void““double““float““case““for““do““short““static“
????????“true““false““try““delete““class““break““bool““goto““default““using“
????????“new““continue““switch““throw““unsigned““signed““sizeof“};??//關(guān)鍵詞數(shù)組
????????public?analyseToken()?????//構(gòu)造函數(shù)
????????{
????????????Array.Clear(prog(char)01000);
????????}
????????public?void?setSourceFileName(string?file)
????????{
????????????sourceFileName?=?file;
????????}
????????public?void?setBuiltFileName(string?file)
????????{
????????????builtFileName?=?file;
????????}
????????protected?void?GetToken()
????????{
????????????ch?=?prog[p++];
????????????while?(ch?==?‘?‘?||?ch?==?‘\n‘?||?ch?==?‘\t‘?||(int)ch?==?13||(int)ch==9)?{?ch?=?prog[p++];?}
????????????for?(n?=?0;?n?20;?n++)
????????????{
????????????????token[n]?=?‘\0‘;
????????????}
????????????n?=?0;
????????????if?((int)ch?==?0)
????????????{
????????????????return;
????????????}
????????????if?((ch?>=?‘a(chǎn)‘?&&?ch?<=?‘z‘)?||?(ch?>=?‘A‘?&&?ch?<=?‘Z‘)||ch?==?‘_‘)
????????????{
????????????????sym?=?1;
????????????????do
????????????????{
????????????????????token[n++]?=?ch;
????????????????????ch?=?prog[p++];
????????????????}?while?((ch?>=?‘a(chǎn)‘?&&?ch?<=?‘z‘)?||?(ch?>=?‘A‘?&&?ch?<=?‘Z‘)||?(ch?>=?‘0‘?&&?ch?<=?‘9‘)||ch?==?‘_‘);
????????????????sym?=?2;
????????????????string?str1?=?null;
????????????????for?(int?i?=?0;?token[i]?!=?‘\0‘;?i++)
????????????????{
????????????????????str1?+=?token[i];
????????????????}
????????????????for?(n?=?0;?n<33;n++)
????????????????{
????????????????????if?(String.Compare(str1?keyword[n])?==?0)?????????????????????//關(guān)鍵詞匹配
????????????????????{
????????????????????????sym?=?n?+?23;
????????????????????}
????????????????}
????????????????p--;
????????????????
????????????}
????????????//else?if?(ch?==?‘{‘||ch?==?‘}‘?||?ch?==?‘(‘?||?ch?==?‘)‘?||?ch?==?‘[‘?||?ch?==?‘]‘)?????????????????????????????//括號處理
????????????//{
????????????//????if?(ch?==?‘{‘)
????????????//????{
????????????//????????sym?=?3;
????????????//????????//st.Push(ch);
????????????//????????token[0]?=?ch;
????????????//????}
????????????//????else?if?(ch?==?‘}‘)
????????????//????{
????????????//????????//if?((char)st.Peek()?==?‘{‘)
????????????//????????//{
????????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????????34??2018-09-27?20:32??詞法分析\.git\COMMIT_EDITMSG
?????文件????????966??2018-09-27?20:47??詞法分析\.git\config
?????文件?????????73??2018-09-26?20:23??詞法分析\.git\desc
?????文件?????????95??2018-10-27?21:29??詞法分析\.git\FETCH_HEAD
?????文件?????????23??2018-09-26?20:23??詞法分析\.git\HEAD
?????文件????????478??2018-09-26?20:23??詞法分析\.git\hooks\applypatch-msg.sample
?????文件????????896??2018-09-26?20:23??詞法分析\.git\hooks\commit-msg.sample
?????文件????????189??2018-09-26?20:23??詞法分析\.git\hooks\post-update.sample
?????文件????????424??2018-09-26?20:23??詞法分析\.git\hooks\pre-applypatch.sample
?????文件???????1642??2018-09-26?20:23??詞法分析\.git\hooks\pre-commit.sample
?????文件???????1348??2018-09-26?20:23??詞法分析\.git\hooks\pre-push.sample
?????文件???????4951??2018-09-26?20:23??詞法分析\.git\hooks\pre-reba
?????文件????????544??2018-09-26?20:23??詞法分析\.git\hooks\pre-receive.sample
?????文件???????1239??2018-09-26?20:23??詞法分析\.git\hooks\prepare-commit-msg.sample
?????文件???????3610??2018-09-26?20:23??詞法分析\.git\hooks\update.sample
?????文件???????1651??2018-09-27?20:32??詞法分析\.git\index
?????文件????????240??2018-09-26?20:23??詞法分析\.git\info\exclude
?????文件????????705??2018-09-27?20:32??詞法分析\.git\logs\HEAD
?????文件????????705??2018-09-27?20:32??詞法分析\.git\logs\refs\heads\master
?????文件????????291??2018-09-27?20:48??詞法分析\.git\logs\refs\remotes\origin\master
?????文件????????151??2018-09-27?20:49??詞法分析\.git\ms-persist.xm
?????文件????????159??2018-09-27?20:32??詞法分析\.git\ob
?????文件????????211??2018-09-27?20:32??詞法分析\.git\ob
?????文件????????336??2018-09-26?20:23??詞法分析\.git\ob
?????文件???????1191??2018-09-27?15:57??詞法分析\.git\ob
?????文件???????1665??2018-09-26?20:23??詞法分析\.git\ob
?????文件????????751??2018-09-26?20:23??詞法分析\.git\ob
?????文件???????2497??2018-09-26?20:23??詞法分析\.git\ob
?????文件???????2697??2018-09-27?20:32??詞法分析\.git\ob
?????文件????????186??2018-09-26?20:23??詞法分析\.git\ob
............此處省略158個文件信息
評論
共有 條評論