本程式將對使用者輸入的字串進行判斷,分別輸出每個字串所屬的型別。其中的判斷標準如下:
無符號整數:《整數》
識別符號:《字母》
數字:0|1|2|......|8|9
字母:a|b|......x|y|z
保留字:const|var|procedur|begin|end|odd|if|then|call|while|do|read|write
運算子:+|-|*|/|=|#||>=|:=
界符:(|)|,|;|.
用文法描述詞法規則如下:
無符號整數=a,a->1|2|......8|9|a0|a1|......a8|a9
識別符號=@#,@->a|b|......y|z|a|b|......x|y|z|@a|@b......|@y|@z|@a|@b|......|@x|@y|@z,#->null|0|1|2|......|8|9|#0|#1|......#9
數字=0|1|2|......|8|9
字母=a|b|......x|y|z
保留字=const|var|procedur|begin|end|odd|if|then|call|while|do|read|write
運算子=+|-|*|/|=|#||>=|:=
界符=(|)|,|;|.
詞法分析的**:
1 #include2程式執行截圖:char const_temp[6]=;
3char var_temp[4]=;
4char procedur_temp[9]=;
5char begin_temp[6]=;
6char end_temp[4]=;
7char odd_temp[4]=;
8char if_temp[3]=;
9char then_temp[5]=;
10char call_temp[5]=;
11char while_temp[6]=;
12char do_temp[3]=;
13char read_temp[5]=;
14char write_temp[6]=;
15char keyworld_first(char temp,char
temppp);
16char keyworld_double(char temp,char
temppp);
17main()
1831
else
3240 printf("\n"
);41}42
}43else
if(ch>='
a'&&ch<='
z'||ch>='
a'&&ch<='z'
)4451else
5267 printf("\n"
);68}69
}70}71
else
if(ch=='
+'||ch=='
-'||ch=='
*'||ch=='
='||ch=='
<
'||ch=='
>
'||ch=='
:'||ch=='
/'||ch=='#'
)7279else
8084}85
else
if(ch=='
('||ch=='
)'||ch=='
,'||ch=='
;'||ch=='.'
)8690else
91 ch=getchar();92}
93}94char keyworld_first(char temp,char
temppp)
95119
for(i=0;i<9;i++)
120124 number=0
;125
while(temp==p[number])
126134
if(number==length+1&&(!(temp>='
a'&&temp<='
z'||temp>='
a'&&temp<='
z'||temp>='
0'&&temp<='9'
)))135
141else
142151 printf("\n"
);152
}153
return
temp;
154}
155char keyworld_double(char temp,char
temppp)
156175 printf("\n"
);176
return
temp;
177}
178break
;179
case'w
': if(temppp=='h'
)180 p=while_temp;
181else
if(temppp=='r'
)182 p=write_temp;
183else
184194 printf("\n"
);195
return
temp;
196}
197break
;198
}199
for(i=0;i<9;i++)
200204 number=1
;205 temp=temppp;
206while(temp==p[number])
207212
if(number==length+1&&(!(temp>='
a'&&temp<='
z'||temp>='
a'&&temp<='
z'||temp>='
0'&&temp<='9'
)))213
219else
220229 printf("\n"
);230
}231
return
temp;
232 }
詞法分析程式
上課沒聽啥,一邊看書,一邊做的。基本上,老師的要求是實現了,能把乙個句子 也可以是一整個程式,但還不完善,由於時間急,很多情況來不及考慮了 中的單詞項分析出它是保留字,還是識別符號,運算子等等。開始是在控制台下做得,也比較純正 lex.h ifndef lex h define lex h incl...
詞法分析程式
include include char scanin 300 scanout 300 extern int testscan char scanin 300 scanout 300 file fin,fout 指向輸入輸出檔案的指標 int main include include include...
詞法分析程式
include include void fenxi char c,char b void word char a void number char a int i 定義全域性變數i int s 1 用來記錄是否存在非法字元 main printf n void number char a 對數字字...