Professional Documents
Culture Documents
資工四 49385002 陳俊志
一、介紹
本程式是一個簡單的 C 語言 parser,運行在 Linux 作業系統下。程式使用了 flex 與 bison
此二個工具產生程式碼。執行時,使用者輸入一個.c 檔案,程式會在螢幕上輸出此檔案的語
法錯誤。
二、完成的功能
程式能辨認以下幾種語法結構:
1. 註解
2. 一部分變數宣告功能
3. if-else 敘述
4. 迴圈方面的敘述
5. 一般的算術運算和邏輯運算
三、執行結果
測試以下三個檔案:
1. test1.c(有錯誤)
{
//This is a test file.
int nd2, ad3, bd4, bd5, qwes4;
nd2 = 20563;
ad3 = 1;
bd4 = 0;
0 = bd5;
while( ad3 )
bd4 = bd5 + 1;
2. test2.c(無錯誤)
{
/*This is a test file.*/
double abcdef, ghijk;
abcdef = 0.006;
if ( abcdef == 0.006 )
ghijk = 0.0;
資工四 49385002 陳俊志
3. test3.c(有錯誤)
{
int a, b, c;
a = 1;
b = 2;
c = 0;
while ( a != b ) {
c=c/5
a = a + 1;
return c
執行的結果如下圖:
test1.c
test2.c
test3.c
四、未完成的功能
1. 一般 C 程式蠻常出現的 preprocessor directive、指標、陣列和函數呼叫
2. symbol table 建立
3. syntax tree 建立
五、附錄
A. lexer.l
%{
資工四 49385002 陳俊志
#include "parser.tab.h"
#include <cstdlib>
#include <cstring>
int lineNum = 1;
%}
%option stack
%x comment ocomment
%%
"/*" BEGIN( comment );
<comment>[^*\n]*
<comment>[^*\n]*\n ++lineNum;
<comment>"*"+[^*/\n]*
<comment>"*"+[^*/\n]*\n ++lineNum;
<comment>"*"+"/" BEGIN( INITIAL );
資工四 49385002 陳俊志
"*" return '*';
"/" return '/';
"=" return '=';
">" return '>';
"<" return '<';
";" return ';';
"," return ',';
"(" return '(';
")" return ')';
"{" return '{';
"}" return '}';
\n ++lineNum;
. /*ignore bad characters*/
%%
B. parser.y
%{
#include <iostream>
#include <cstdio>
#include <string>
int numOfErrs = 0;
extern FILE *yyin;
extern int yylex();
void yyerror( char * );
extern int lineNum;
%}
%union {
double val;
char *sVal;
}
%%
program: block
;
資工四 49385002 陳俊志
decls: /*empty*/
| decls decl
;
type: INT
| DOUBLE
;
stmts: /*empty*/
| stmts stmt
;
optassign: /*empty*/
| loc '=' bool
;
loc: ID
;
資工四 49385002 陳俊志
join: join AND_OP equality
| equality
;
%%
void yyerror( char *eMsg )
{
cout << "Error occurred at line " << lineNum << ": " << eMsg << endl;
++numOfErrs;
}
int main()
{
string testFile;
資工四 49385002 陳俊志
getline( cin, testFile );
yyin = tf;
int result = yyparse();
if ( result == 0 )
cout << "Parsing succeeded. " << numOfErrs << " error(s)." << endl;
else
cout << "Parsing failed. " << numOfErrs << " error(s)." << endl;
return 0;
}
else {
cout << "The file can't be opened." << endl;
return 0;
}
資工四 49385002 陳俊志