資源簡介
設計內容及要求:設計一個語法制導翻譯器,將算術表達式翻譯成四元式。要求:
先確定一個定義算術表達式的文法,為其設計一個語法分析程序,為每條產生式配備一個語義子程序,按照一遍掃描的語法制導翻譯方法,實現翻譯程序。對用戶輸入的任意一個正確的算術表達式,程序將其轉換成四元式輸出(可按一定格式輸出到指定文件中)。
代碼片段和文件信息
#include?
#include?
#include?
char?w;
int?j=1;
struct?TOKEN
{
??char?t;
??int?i;
}?;
struct?TOKEN?word?sem[10];
??int?i_sem;
struct?QT
{
??char?w;
??struct?TOKEN?word1;
??struct?TOKEN?word2;
??struct?TOKEN?temp;
}?;
char?exp[50];
?int?i=0;
struct?QT?qt[30];
?int?q=0;
int?D();
int?E();
int?T();
int?F();
void?next();
void?newt();
void?quat(char);
int?main()
{
??printf(“please?input?your?expression:????“);
??scanf(“????%s“exp);
??next();
??D();
??if?(w==‘\0‘)
??{
????printf(“\n“);
????for?(i=0;i????{
??????printf(“??(%d)?“i+1);
if(qt[i].w!=‘=‘){
??????printf(“?(?%c“qt[i].w);
??????if?(qt[i].word1.t!=‘t‘)
printf(“??%c“qt[i].word1.t);
??????else
printf(“??%c%d“qt[i].word1.tqt[i].word1.i);
??????if?(qt[i].word2.t!=‘t‘)
printf(“??%c“qt[i].word2.t);
??????else
printf(“??%c%d“qt[i].word2.tqt[i].word2.i);
??????printf(“??%c%d?)\n“qt[i].temp.tqt[i].temp.i);
}
else
{
?printf(“?(?%c“qt[i].w);
?printf(“??%c%d?“qt[i-1].temp.tqt[i-1].temp.i);
printf(“??_“);
printf(“??%c)“qt[
評論
共有 條評論