資源簡介
華南師范大學 本資源包括TINY擴充語言的語法分析的實驗報告,編譯原理附錄B和TINY擴充語言的語法分析代碼.實驗報告中說明了該實驗的完成步驟.
擴充的語法規則有:實現while、do while、for語句和求余計算式子,具體文法規則自行構造。
可參考:P97及P136的文法規則。
(1) While-stmt --> while exp do stmt-sequence endwhile
(2) Dowhile-stmt-->do stmt-sequence while exp
(3) for-stmt-->for identifier:=simple-exp to simple-exp do stmt-sequence enddo 步長遞增1
(4) for-stmt-->for identifier:=simple-exp downto simple-exp do stmt-sequence enddo 步長遞減1
要求:
(1)要提供一個源程序編輯界面,以讓用戶輸入源程序(可保存、打開源程序)
(2)可由用戶選擇是否生成語法樹,并可查看所生成的語法樹。
(3)應該書寫完善的軟件文檔

代碼片段和文件信息
/****************************************************/
/*?File:?analyze.c??????????????????????????????????*/
/*?Semantic?analyzer?implementation?????????????????*/
/*?for?the?TINY?compiler????????????????????????????*/
/*?Compiler?Construction:?Principles?and?Practice???*/
/*?Kenneth?C.?Louden????????????????????????????????*/
/****************************************************/
#include?“globals.h“
#include?“symtab.h“
#include?“analyze.h“
/*?counter?for?variable?memory?locations?*/
static?int?location?=?0;
/*?Procedure?traverse?is?a?generic?recursive?
?*?syntax?tree?traversal?routine:
?*?it?applies?preProc?in?preorder?and?postProc?
?*?in?postorder?to?tree?pointed?to?by?t
?*/
static?void?traverse(?TreeNode?*?t
???????????????void?(*?preProc)?(TreeNode?*)
???????????????void?(*?postProc)?(TreeNode?*)?)
{?if?(t?!=?NULL)
??{?preProc(t);
????{?int?i;
??????for?(i=0;?i?????????traverse(t->child[i]preProcpostProc);
????}
????postProc(t);
????traverse(t->siblingpreProcpostProc);
??}
}
/*?nullProc?is?a?do-nothing?procedure?to?
?*?generate?preorder-only?or?postorder-only
?*?traversals?from?traverse
?*/
static?void?nullProc(TreeNode?*?t)
{?if?(t==NULL)?return;
??else?return;
}
/*?Procedure?insertNode?inserts?
?*?identifiers?stored?in?t?into?
?*?the?symbol?table?
?*/
static?void?insertNode(?TreeNode?*?t)
{?switch?(t->nodekind)
??{?case?StmtK:
??????switch?(t->kind.stmt)
??????{?case?AssignK:
????????case?ReadK:
??????????if?(st_lookup(t->attr.name)?==?-1)
??????????/*?not?yet?in?table?so?treat?as?new?definition?*/
????????????st_insert(t->attr.namet->linenolocation++);
??????????else
??????????/*?already?in?table?so?ignore?location?
?????????????add?line?number?of?use?only?*/?
????????????st_insert(t->attr.namet->lineno0);
??????????break;
????????default:
??????????break;
??????}
??????break;
????case?ExpK:
??????switch?(t->kind.exp)
??????{?case?IdK:
??????????if?(st_lookup(t->attr.name)?==?-1)
??????????/*?not?yet?in?table?so?treat?as?new?definition?*/
????????????st_insert(t->attr.namet->linenolocation++);
??????????else
??????????/*?already?in?table?so?ignore?location?
?????????????add?line?number?of?use?only?*/?
????????????st_insert(t->attr.namet->lineno0);
??????????break;
????????default:
??????????break;
??????}
??????break;
????default:
??????break;
??}
}
/*?Function?buildSymtab?constructs?the?symbol?
?*?table?by?preorder?traversal?of?the?syntax?tree
?*/
void?buildSymtab(TreeNode?*?syntaxTree)
{?traverse(syntaxTreeinsertNodenullProc);
??if?(TraceAnalyze)
??{?fprintf(listing“\nSymbol?table:\n\n“);
????printSymTab(listing);
??}
}
static?void?typeError(TreeNode?*?t?char?*?message)
{?fprintf(listing“Type?error?at?line?%d:?%s\n“t->linenomessage);
??Error?=?TRUE;
}
/*?Procedure?checkNode?performs
?*?type?checking?at?a?single?tree?node
?*/
static?void?checkNode(TreeNode?*?t)
{?switch?(t->nodek
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????462848??2013-01-02?21:56??TINY?擴充語言的語法分析??-?副本.doc
?????文件????????5161??1997-02-01?09:33??編譯原理附錄B\YACC\TINY.Y
?????文件????????3338??1997-01-31?22:48??編譯原理附錄B\YACC\GLOBALS.H
?????目錄???????????0??2013-01-02?19:48??編譯原理附錄B\YACC\
?????文件????????1037??1998-08-01?14:01??編譯原理附錄B\UTIL.H
?????文件????????4848??1998-08-01?14:02??編譯原理附錄B\UTIL.C
?????文件???????14104??1998-03-20?13:40??編譯原理附錄B\TM.EXE
?????文件???????16753??1998-08-01?14:02??編譯原理附錄B\TM.C
?????文件???????40736??1998-04-26?21:47??編譯原理附錄B\TINY.EXE
?????文件?????????959??1998-08-01?14:01??編譯原理附錄B\SYMTAB.H
?????文件????????3564??1998-08-01?14:02??編譯原理附錄B\SYMTAB.C
?????文件?????????659??1998-08-01?14:01??編譯原理附錄B\SCAN.H
?????文件????????5733??1999-08-04?16:05??編譯原理附錄B\SCAN.C
?????文件?????????263??1996-08-25?15:33??編譯原理附錄B\SAMPLE.TNY
?????文件?????????920??1998-07-31?16:47??編譯原理附錄B\SAMPLE.TM
?????文件????????1962??1998-07-31?15:15??編譯原理附錄B\README.DOS
?????文件?????????484??1998-08-01?14:01??編譯原理附錄B\PARSE.H
?????文件????????5173??1998-08-01?14:02??編譯原理附錄B\PARSE.C
?????文件????????1129??1998-02-03?22:29??編譯原理附錄B\MAKEFILE
?????文件????????2535??1998-08-01?14:02??編譯原理附錄B\MAIN.C
?????文件????????1995??1998-07-31?14:45??編譯原理附錄B\LEX\TINY.L
?????目錄???????????0??2013-01-02?19:48??編譯原理附錄B\LEX\
?????文件????????2955??1998-08-01?14:01??編譯原理附錄B\GLOBALS.H
?????文件????????2234??1998-08-01?14:01??編譯原理附錄B\CODE.H
?????文件????????3039??1998-08-01?14:02??編譯原理附錄B\CODE.C
?????文件?????????679??1998-08-01?14:01??編譯原理附錄B\CGEN.H
?????文件????????6971??1998-08-01?14:02??編譯原理附錄B\CGEN.C
?????文件?????????652??1998-08-01?14:01??編譯原理附錄B\ANALYZE.H
?????文件????????4452??1998-08-01?14:02??編譯原理附錄B\ANALYZE.C
?????目錄???????????0??2013-01-02?19:48??編譯原理附錄B\
?????目錄???????????0??2013-01-02?20:34??實驗二\
............此處省略69個文件信息
評論
共有 條評論