A 小C語言 詞法分析程式

2021-10-09 14:43:04 字數 3364 閱讀 8780

小c語言文法 

1. 《程式》→|<|>=|<=|==|!=

15. 《算數表示式》→《算數表示式》+《項》|《算數表示式》-《項》|《項》

16. 《項》→《項》*《因子》|《項》/《因子》|《因子》

17. 《因子》→《識別符號》|《無符號整數》|(《算數表示式》)

18. 《識別符號》→《字母》|《識別符號》《字母》|《識別符號》《數字》

19. 《無符號整數》→《數字》|《無符號整數》《數字》

20. 《字母》→a|b|…|z|a|b|…|z

21. 《數字》→0|1|2|3|4|5|6|7|8|9

22. < main關鍵字》→main

23. < if關鍵字》→if

24. < else關鍵字》→else

25. < for關鍵字》→for

26. < while關鍵字》→while

27. < int關鍵字》→int

每行單詞數不超過10個

小c語言文法如上,現在我們對小c語言寫的乙個源程式進行詞法分析,分析出關鍵字、自定義識別符號、整數、界符

和運算子。

關鍵字:main if else for while int

自定義識別符號:除關鍵字外的識別符號

整數:無符號整數

界符: ( ) , ;

運算子:= + - * / < <= > >= == !=

輸入乙個小c語言源程式,源程式長度不超過2000個字元,保證輸入合法。

按照源程式中單詞出現順序輸出,輸出二元組形式的單詞串。

(單詞種類,單詞值)

單詞一共5個種類:

關鍵字:用keyword表示

自定義識別符號:用identifier表示

整數:用integer表示

界符:用boundary表示

運算子:用operator表示

每種單詞值用該單詞的符號串表示。

input 

main() 

}

output 

(keyword,main)

(boundary,()

(boundary,))

(boundary,)

(boundary,})

注意下面還有乙個版本??

#include #include #include int main()

else

}s[j] = '\0';

if(strcmp(s,"main") == 0 || strcmp(s,"if") == 0 || strcmp(s,"else") == 0 || strcmp(s,"for") == 0 || strcmp(s,"while") == 0 || strcmp(s,"int") == 0)

else

memset(s,0,sizeof(s));

j = 0;

i = i - 1;

}else if(str[i] == '(' || str[i] == ')' || str[i] == '' || str[i] == ',' || str[i] == ';')

else if(str[i] >= '0' && str[i] <= '9')

else

}s[j] = '\0';

printf("(integer,%s)\n",s);

memset(s,0,sizeof(s));

j = 0;

i = i - 1;

}else if(str[i] == '=' || str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/' || str[i] == '<' || str[i] == '>' || str[i] == '!')

else

else}}

}memset(str,0,sizeof(str));

}return 0;

}

注釋 ?! 

#include using namespace std;

char str[85]; // 這是讀入的字串

char s[85]; // 判斷的臨時字串

int main()

else break;

}s[j] = '\0'; // 字串的終止

if(strcmp(s,"int") == 0 || strcmp(s,"main") == 0 || strcmp(s,"for") == 0 || strcmp(s,"if") == 0 || strcmp(s,"else") == 0

|| strcmp(s,"while") == 0)

else

i --; // 如果當前的是下乙個分解符,則需要對其進行判斷

// 比如 int main() 在讀入到 ( 時跳出迴圈,如果不進行 i -- 操作, 則下次直接進行判斷 ) ,就少判斷了乙個邊界符

}else if(str[i] == '(' || str[i] == ')' || str[i] == '}' || str[i] == '

else if(str[i] >= '0' && str[i] <='9') // 判斷數字,和判斷關鍵字一樣

else break;

}s[j] = '\0';

printf("(integer,%s)\n",s);

i -- ; // 進行必要的回退

}else if(str[i] == '=' || str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/'

|| str[i] == '>' || str[i] == '<' || str[i] == '!')

else

else printf("(operator,%c)\n",str[i]);}}

}}

return 0;

}

也有可能ac不了?

#include using namespace std;

string s[5] = ;

string t[6] = ;

string s;

void panduan(string s)

temp = "";

if(i+1else

}else if(s[i]==';' || s[i]==',' || s[i]=='(' || s[i]==')' || s[i]=='')

temp = "";

cout<<"("<}

else

}if(temp.length())

}return 0;

}

小C語言 詞法分析程式

time limit 1000ms memory limit 5000kb submit statistic problem description 小c語言文法 1.程式 2.宣告序列 宣告序列 宣告語句 宣告語句 空 3.宣告語句 識別符號表 4.識別符號表 識別符號 識別符號表 識別符號 5....

小C語言 詞法分析程式

time limit 1000ms memory limit 5000kb submit statistic problem description 小c語言文法 1.程式 2.宣告序列 宣告序列 宣告語句 宣告語句 空 3.宣告語句 識別符號表 4.識別符號表 識別符號 識別符號表 識別符號 5....

小C語言 詞法分析程式

time limit 1000ms memory limit 5000kb submit statistic problem description 小c語言文法 1.程式 2.宣告序列 宣告序列 宣告語句 宣告語句 空 3.宣告語句 識別符號表 4.識別符號表 識別符號 識別符號表 識別符號 5....