題目要求:對給定的c語言程式,識別出關鍵字、整型常量、實型常量(包括1.23e-23這樣的科學計數法)、識別符號、分隔符、運算子。
語言:c++
整合環境:vs 2017
演算法:使用狀態轉換圖實現
程式**:
/*
關鍵字:;標示符:;整型常量<200,數值》;實型常量<300,數值》;分隔符:<400-408, >;運算子:<500-, >
*/#include
#include
#include
#include
#include
#include
#include
using
namespace std;
struct tuple
;vector constant =
;vector result;
vector<
char
>file_content;
vector keyword =
;vector<
char
>separater =',
'[',
']',
'(',
')'}
;vector<
char
>operators =
;void
getfile
(string file_name)
in_file.
close()
;}bool
isletter
(char c)
bool
isblank
(char c)
bool
isnumber
(char c)
intiskeyword
(string str)
bool
isinteger
(string str)
}int
isseparater
(char c)
bool
isoperator
(char c)
intmatchoperator
(char c)
void
lexicalanalyzer()
if(isletter
(temp_ch)
)int index =
iskeyword
(temp_word)
; tuple temp_tuple;
if(index!=0)
else
continue;}
if(isnumber
(temp_ch)
) tuple temp_tuple;
temp_tuple.content = temp_word;if(
isinteger
(temp_word)
) temp_tuple.val =
"200"
;else
temp_tuple.val =
"300"
; result.
push_back
(temp_tuple)
;continue;}
if(isseparater
(temp_ch)!=0
)if(isoperator
(temp_ch)
) tuple temp_tuple;
if(temp_word ==
"++")if
(temp_word ==
"--")if
(temp_word ==
"&&")if
(temp_word ==
"||"
)else
else
} result.
push_back
(temp_tuple)
;continue;}
i++;}
}void
printresult()
}int
main()
被讀取的text_c.txt檔案:
int main()
return 0;
}
執行結果:
編譯原理詞法分析程式
include include char prog 80 token 8 char ch int syn,p,m,n,sum char rwtab 6 void scaner token m 0 p 回退乙個字元 syn 10 for n 0 n 6 n if strcmp token,rwtab ...
編譯原理詞法分析程式
include include char scanin 300 scanout 300 extern int testscan char scanin 300 scanout 300 file fin,fout 指向輸入輸出檔案的指標 int main include include include...
編譯原理c 實現詞法分析器
根據設計的dfa完成手工詞法分析器的c 實現 共有四組測試樣例,要求格式完全一致。int main int main float i 1 1.005e 3 this is remark line1 type,int line1 keyword,main line1 bracket,line1 bra...