?? 詞法分析程序 - 毒草編程站.htm
字號:
<html>
<head>
<title>詞法分析程序 - 毒草編程站</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="style/green/style.css" type="text/css">
<script language="JavaScript" src="inc/common.js"></script>
</head>
<body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0"><table bgcolor="#FFFFFF" width="100%" cellpadding="0" cellspacing="0"
border="0" align="center">
<tr>
<td width="100%">
<table border="0" cellspacing="0" cellpadding="0" width="99%"
align="center" class="outertxt">
<tr>
<td rowspan="2" valign="top" width="365" ><img src="style/green/image/title.gif"></td><td height="80"
align="right" background="style/green/image/topbg.jpg">
<!-- Position for advertising banner -->
</td>
</tr>
<tr>
<td align="right" class="smalltxt"><span class="bold">»</span>
<span class="bold">歡迎: </span><a href="register.asp">注冊</a> <a href="login.asp">登錄</a> <a href="password.asp">找回密碼</a> <a href="plus/showonline.asp" target="_blank"> 在線</a>
<!--<a href="showtrashbox.asp">回收站</a><a href="plus/userlist.asp" target="_blank">用戶</a> <a href="help.htm" target="_blank">幫助</a>
-->
<a href="index.asp">首頁</a></td>
</tr></table><script language=JavaScript>function goforum(strgoforum) {location.replace(strgoforum)}</script><table border="0" width="100%" id="table1" cellpadding="0" cellspacing="4">
<tr>
<td> <a href=index.asp><b>毒草編程站</b></a> » <a href="showcate.asp?cateid=1"><b>編程交流區</b></a> » <a href="showforum.asp?forumid=2"><b>C 語 言 教 室 </b></a> » <b>詞法分析程序</td>
</tr>
</table></b><a name="top"></a><CENTER><table cellSpacing="1" cellPadding="4" width="98%" align="center" border="0" style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 12px; font-family: Tahoma, Verdana; color: #000000" id="table5"><tr><td bgcolor="#FFFFFF" width="60%" valign="bottom">本主題共被查看797次,共有3篇回復。 </td><td bgcolor="#FFFFFF" align="right"> <a href="newthread.asp?forumid=2"><img src="style/green/image/newthread.gif" border="0" alt="發表新主題"></a> <a href="newvote.asp?forumid=2"><img src="style/green/image/newvote.gif" border=0 alt="發布新投票"></a> <a href="newreply.asp?threadid=344"><img src="style/green/image/newreply.gif" border=0 alt="發表回復"></a></td></tr></table><table width="98%" border="0" cellspacing="1" class="tableborder" cellpadding="4"><tr class="header"><td colspan="2"><table border="0" width="100%" id="table6" cellspacing="1"><tr><td style="color:#FFFFFF"><b>詞法分析程序</b></td><td align="right" style="color:#FFFFFF"><a href="showthread.asp?threadid=344&goto=prev">上一主題</a> | <a href="showthread.asp?threadid=344&goto=next">下一主題</a></td></tr></table></td></tr><tr bgcolor="#3480C6"><td class="thread1" valign="top" width="20%" align="left"><b>龔辟愚</b><br><img src="http://tcwin.yuxa.com/vbs/Upload_Face/sf_2.gif" width="95" height="120"><br>站長<br><img src="image/title/supermoder.gif"><br>發帖: <font color="#E89A9A"><b>259<br></b></font>積分: <font color="#B481C5"><b>1000000005<br></b></font>注冊: <font color="#8AB9D6"><b>2004-5<br></b></font>來自:湖南<br>性別: 美女<br>狀態: <font color="#8BBB9E"><b>offline</b></font></td> <td class="thread1" valign="top" width="80%" rowspan="2" align="right"> <a href="showthread.asp?postid=1350"><img src="style/green/image/tree.gif" border=0 alt="以樹型方式查看"></a> <a href="javascript:onclick=this.location.reload()"><img src="style/green/image/refresh.gif" border=0 alt="刷新"></a> <a href="newfavorite.asp?threadid=344" target=_blank><img src="style/green/image/favorite.gif" border=0 alt="加入到私人收藏"></a> <a href="editpost.asp?postid=1350"><img src="style/green/image/edit.gif" border=0 alt="修改帖子"></a> <a href="delpost.asp?postid=1350"><img src="style/green/image/del.gif" border=0 alt="刪除"></a> <a href="newreply.asp?quote=yes&postid=1350"><img src="style/green/image/quote.gif" border=0 alt="引用回復"></a> <a href="newreply.asp?postid=1350"><img src="style/green/image/reply.gif" border=0 alt="發表回復"></a> <hr noshade size="1" color="#E6E6E6"><table width="100%" border="0" cellspacing="1" cellpadding="4" style="table-layout: fixed;word-wrap: break-word" class="thread1"><tr><td><br><b>詞法分析程序</b><br><br>1.1.1實驗目的 <br>設計,編制并調試一個詞法分析程序。 <br>1.2實驗要求 <br>1. 關鍵字:begin ;if;then;while;do;end 并為小寫。 <br>2. 符號:”+”;”-“;”*”;”/”;”:=”;”:”;”<”;”<=”;”>”;”>=”;”<>”;”=”;”(”;”)”;”;”;”#”。 <br>3. 其他標記 如字符串,以字母開頭的標識符。 <br>4. 空格符跳過。 <br>1.3各符號對應種別碼 <br>單詞符號 種別碼 單詞符號 種別碼 單詞符號 種別碼 單詞符號 種別碼 <br>begin 1 標識符 10 + 13 < 20 <br>if 2 數字 11 - 14 <= 21 <br>then 3 ( 27 * 15 > 23 <br>while 4 ) 28 / 16 >= 24 <br>do 5 ; 26 := 17 <> 22 <br>end 6 # 0 : 18 = 25 <br>1.4詞法分析程序的功能 <br>輸入:所給文法的原程序字符串。 <br>輸出:二元數組。 <br>如: <br>if (a<=0) <br>then b:=1; </p><p>著輸出: <br>(2, if) <br>(27, ( ) <br>(10, a ) <br>(21, <=) <br>(28, ) ) <br>(3, then ) <br>(10, b) <br>(17, :=) <br>(11, 1) <br>2.下面為原程序: <br>#include<stdio.h> <br>#include<string.h> <br>char prog[80],token[8]; char ch; <br>int syn,p,m,n; char *numtab[6]={"begin","if","then","while","do","end"}; <br>int scaner(char prog[]){int m,i,n; for(m=0;m<20;m++) token[m++]=NULL; /*清空token*/ <br>m=0; ch=prog[p++];/*讀入字符*/ <br>while(ch==' ') ch=prog[p++]; /*空格處理*/ <br>if((ch>='a')&&(ch<='z'))/*字符串處理*/ <br>{while(((ch>='a')&&(ch<='z'))||((ch>='0')&&(ch<='9'))) {token[m++]=ch;ch=prog[p++];} token[m++]='\0'; n=0; for(i=0;i<6;i++) <br>if(strcmp(token,numtab[i])==0)/*判斷是不是保留字*/ <br>switch(i){ case 0: syn=1;n++;break; <br>case 1: syn=2;n++;break; <br>case 2: syn=3;n++;break; <br>case 3: syn=4;n++;break; <br>case 4: syn=5;n++;break; <br>case 5: syn=6;n++;break;} <br>if(n==0) syn=10;p--;}/*非保留字*/ <br>else if(ch>='0'&&ch<='9')/*判斷是否為數字*/ <br>{n=0;while(ch>='0'&&ch<='9') {n=n*10+(ch-'0');ch=prog[p++];}syn=11; p--;}else switch(ch) <br>{case'<': token[m++]=ch;ch=prog[p++];/*判斷小于號*/ <br>if(ch=='>')/*是否為不等于*/ <br>{token[++m]=ch;syn=22;} else if(ch=='=')/*是否為小于等于*/ <br>{token[++m]=ch;syn=21;} else{p--;syn=20;} break; <br>case'>': token[m]=ch;/*是否為大于*/ <br>ch=prog[p++]; f(ch=='='){token[++m]=ch;syn=24;}/*是否為大于等于*/ <br>else{p--;syn=23;} break; <br>case'+': token[m]=ch;syn=13;break;/*判斷加號*/ <br>case'-': token[m]=ch;syn=14;break;/*判斷-號*/ <br>case'*': token[m]=ch;syn=15;break;/*判斷”*”號*/ <br>case'/': token[m]=ch;syn=16;break;/*判斷\號*/ <br>case':': token[m++]=ch;ch=prog[p++];/*判斷:號*/ <br>if(ch=='='){token[++m]=ch;syn=17;} else{p--;syn=18;}break; <br>case'=': token[m]=ch;syn=25;break;/*判斷=號*/ <br>case';': token[m]=ch;syn=26;break;/*判斷;號*/ <br>case'(': token[m]=ch;syn=27;break;/*判斷(號*/ <br>case')': token[m]=ch;syn=28;break;/*判斷)號*/ <br>case'#': syn=0;token[0]='#';break;/*判斷#號*/ <br>default: syn=-1;}/*錯誤說明*/ <br>return syn; return n;} <br>void main() <br>{p=0; printf("\n輸入程序,以#結束,可以有begin,if,then,while,do,end,:,;,+,-,*,/,<,>,=,<>,(,):\n"); <br>do{ch=getchar();prog[p++]=ch;}while(ch!='#'); p=0; <br>do{syn=scaner(prog);switch(syn) <br>case 11: printf ("\n(%2d,%8d)",syn,n);;break;/*輸出數字*/ <br>case-1: printf("\n(%2d, error)",syn);break;/*錯誤提示*/ <br>default: printf("\n(%2d,%8s)",syn,token); }/*字符輸出*/ <br>}while(syn!=0);}</p><p><br><br> <br></td></tr><tr bgcolor="#FFFFFF"><td style="height:100px" class="thread1"><br><img src="style/green/image/sign.gif" border=0><br><img src="http://wpa.qq.com/pa?p=1:54501020:10" border="0" onload="if(this.width>screen.width-333) {this.width=screen.width-333;this.title='open new window';}" onmouseover="if(this.title) {this.style.cursor='hand';}"; onclick="if(this.title) {window.open('http://wpa.qq.com/pa?p=1:54501020:10');}"><a href="http://wpa.qq.com/msgrd?V=1&Uin=54501020&Site=http://tcwin.yuxa.com/vbs&Menu=yes" target=_blank>點擊這里給我發消息</a><br></td></tr></table><br><hr noshade size="1" color="#E6E6E6"><table width="100%" border="0" cellspacing="0" cellpadding="0" align="right"><form method="post" action="rate.asp" target="_blank"><tr><td width="70%"> <a href="showuser.asp?userid=2" target=_blank><img src="style/green/image/profile.gif" border=0 alt="查看用戶檔案"></a> <a href="mailto:g_by@qq.com"><img src="style/green/image/email.gif" border=0 alt="email:g_by@qq.com"></a> <a href="http://tcwin.yuxa.com" target=_blank><img src="style/green/image/homepage.gif" border=0 alt="http://tcwin.yuxa.com"></a> <a href="showmessage.asp?action=sendmessage&username=龔辟愚" target=_blank><img src="style/green/image/message.gif" border=0 alt="短消息"></a></td><td width="20%" nowrap><select name="rating" ><option value="5">5</option><option value="4">4</option><option value="3">3</option><option value="2">2</option><option value="1">1</option><option value="0" selected>0</option><option value="-1">-1</option><option value="-2">-2</option><option value="-3">-3</option><option value="-4">-4</option><option value="-5">-5</option></select> <input type="hidden" name="postid" value="1350"><input type="submit" name="submit" value="打分"></td><td width="10%" align="right"><b><font color="#999999">1</font></b> <a href="#top"><img src="style/green/image/gotop.gif" align="absbottom" border=0></a></td></tr></form></table></td></tr><tr bgcolor="#3480C6"><td class="thread1" valign="top" width="20%" align="center"><a href="showposterip.asp?postid=1350" target=_blank><img src="style/green/image/ip.gif" border=0></a>2005-1-4 16:58:55</td> </tr><tr bgcolor="#3480C6"><td class="thread0" valign="top" width="20%" align="left"><b>okayshow</b><br><img src="image/face/25.gif"><br>論壇新成員<br><img src="image/title/1.gif"><br>發帖: <font color="#E89A9A"><b>5<br></b></font>積分: <font color="#B481C5"><b>5<br></b></font>注冊: <font color="#8AB9D6"><b>2005-1<br></b></font>來自:湖南<br>性別: 帥哥<br>狀態: <font color="#8BBB9E"><b>offline</b></font></td> <td class="thread0" valign="top" width="80%" rowspan="2" align="right"> <a href="showthread.asp?postid=1352"><img src="style/green/image/tree.gif" border=0 alt="以樹型方式查看"></a> <a href="javascript:onclick=this.location.reload()"><img src="style/green/image/refresh.gif" border=0 alt="刷新"></a> <a href="newfavorite.asp?threadid=344" target=_blank><img src="style/green/image/favorite.gif" border=0 alt="加入到私人收藏"></a> <a href="editpost.asp?postid=1352"><img src="style/green/image/edit.gif" border=0 alt="修改帖子"></a> <a href="delpost.asp?postid=1352"><img src="style/green/image/del.gif" border=0 alt="刪除"></a> <a href="newreply.asp?quote=yes&postid=1352"><img src="style/green/image/quote.gif" border=0 alt="引用回復"></a> <a href="newreply.asp?postid=1352"><img src="style/green/image/reply.gif" border=0 alt="發表回復"></a> <hr noshade size="1" color="#E6E6E6"><table width="100%" border="0" cellspacing="1" cellpadding="4" style="table-layout: fixed;word-wrap: break-word" class="thread0"><tr><td><br><b>Re:詞法分析程序</b><br><br>★★提示: 對不起!編譯失敗,您還需要檢查錯誤。★★</p><p>怎么回事啊???<br> <br></td></tr><tr bgcolor="#FFFFFF"><td style="height:100px" class="thread0"><br><img src="style/green/image/sign.gif" border=0><br>- - - > > > 有我故我在 < < < - - - <br></td></tr></table><br><hr noshade size="1" color="#E6E6E6"><table width="100%" border="0" cellspacing="0" cellpadding="0" align="right"><form method="post" action="rate.asp" target="_blank"><tr><td width="70%"> <a href="showuser.asp?userid=820" target=_blank><img src="style/green/image/profile.gif" border=0 alt="查看用戶檔案"></a> <a href="mailto:tuoxiu-123@163.com"><img src="style/green/image/email.gif" border=0 alt="email:tuoxiu-123@163.com"></a> <a href="http://tuoxiu.icpcn.com" target=_blank><img src="style/green/image/homepage.gif" border=0 alt="http://tuoxiu.icpcn.com"></a> <a href="showmessage.asp?action=sendmessage&username=okayshow" target=_blank><img src="style/green/image/message.gif" border=0 alt="短消息"></a></td><td width="20%" nowrap><select name="rating" ><option value="5">5</option><option value="4">4</option><option value="3">3</option><option value="2">2</option><option value="1">1</option><option value="0" selected>0</option><option value="-1">-1</option><option value="-2">-2</option><option value="-3">-3</option><option value="-4">-4</option><option value="-5">-5</option></select> <input type="hidden" name="postid" value="1352"><input type="submit" name="submit" value="打分"></td><td width="10%" align="right"><b><font color="#999999">2</font></b> <a href="#top"><img src="style/green/image/gotop.gif" align="absbottom" border=0></a></td></tr></form></table></td></tr><tr bgcolor="#3480C6"><td class="thread0" valign="top" width="20%" align="center"><a href="showposterip.asp?postid=1352" target=_blank><img src="style/green/image/ip.gif" border=0></a>2005-1-4 19:14:22</td> </tr><tr bgcolor="#3480C6"><td class="thread1" valign="top" width="20%" align="left"><b>小松</b><br><img src="image/face/1.gif"><br>論壇新成員<br><img src="image/title/1.gif"><br>發帖: <font color="#E89A9A"><b>1<br></b></font>積分: <font color="#B481C5"><b>1<br></b></font>注冊: <font color="#8AB9D6"><b>2005-4<br></b></font>來自:<br>性別: 帥哥<br>狀態: <font color="#8BBB9E"><b>offline</b></font></td> <td class="thread1" valign="top" width="80%" rowspan="2" align="right"> <a href="showthread.asp?postid=2057"><img src="style/green/image/tree.gif" border=0 alt="以樹型方式查看"></a> <a href="javascript:onclick=this.location.reload()"><img src="style/green/image/refresh.gif" border=0 alt="刷新"></a> <a href="newfavorite.asp?threadid=344" target=_blank><img src="style/green/image/favorite.gif" border=0 alt="加入到私人收藏"></a> <a href="editpost.asp?postid=2057"><img src="style/green/image/edit.gif" border=0 alt="修改帖子"></a> <a href="delpost.asp?postid=2057"><img src="style/green/image/del.gif" border=0 alt="刪除"></a> <a href="newreply.asp?quote=yes&postid=2057"><img src="style/green/image/quote.gif" border=0 alt="引用回復"></a> <a href="newreply.asp?postid=2057"><img src="style/green/image/reply.gif" border=0 alt="發表回復"></a> <hr noshade size="1" color="#E6E6E6"><table width="100%" border="0" cellspacing="1" cellpadding="4" style="table-layout: fixed;word-wrap: break-word" class="thread1"><tr><td><br><b>尋找---詞法分析程序</b><br><br>!!!!!!!!!!!急救急救!!!!!!!!!!各位朋友你們好,能在網絡上和你們相識,我覺得很榮幸!<br> 我現在學習也不是太理想,現在我們老師要我做個詞法分析程序,可是我對c語言不是很精通,在這里</p><p>我先感謝你!真心的祝福你有快樂的生活,學業有成!!<br> 我叫沈萬松,我的qq 16216553 手機號碼:13965140066 <br>E-mail shenwansong@tom.com<br>如果有愿意幫助我的朋友,給我來消息!!<br> <br></td></tr><tr bgcolor="#FFFFFF"><td style="height:100px" class="thread1"><br><img src="style/green/image/sign.gif" border=0><br><br></td></tr></table><br><hr noshade size="1" color="#E6E6E6"><table width="100%" border="0" cellspacing="0" cellpadding="0" align="right"><form method="post" action="rate.asp" target="_blank"><tr><td width="70%"> <a href="showuser.asp?userid=1135" target=_blank><img src="style/green/image/profile.gif" border=0 alt="查看用戶檔案"></a> <a href="mailto:shenwansong@tom.com"><img src="style/green/image/email.gif" border=0 alt="email:shenwansong@tom.com"></a> <a href="" target=_blank><img src="style/green/image/homepage.gif" border=0 alt=""></a> <a href="showmessage.asp?action=sendmessage&username=小松" target=_blank><img src="style/green/image/message.gif" border=0 alt="短消息"></a></td><td width="20%" nowrap><select name="rating" ><option value="5">5</option><option value="4">4</option><option value="3">3</option><option value="2">2</option><option value="1">1</option><option value="0" selected>0</option><option value="-1">-1</option><option value="-2">-2</option><option value="-3">-3</option><option value="-4">-4</option><option value="-5">-5</option></select> <input type="hidden" name="postid" value="2057"><input type="submit" name="submit" value="打分"></td><td width="10%" align="right"><b><font color="#999999">3</font></b> <a href="#top"><img src="style/green/image/gotop.gif" align="absbottom" border=0></a></td></tr></form></table></td></tr><tr bgcolor="#3480C6"><td class="thread1" valign="top" width="20%" align="center"><a href="showposterip.asp?postid=2057" target=_blank><img src="style/green/image/ip.gif" border=0></a>2005-4-28 10:45:34</td> </tr></table></CENTER><table width="98%" border="0" cellspacing="1" cellpadding="4" align="center">
<tr>
<td bgcolor="#FFFFFF" align="center"><img src="style/green/image/page.gif"> 1: <b>1</b> </td>
</tr>
</table>
<br>
<table width="98%" border="0" cellspacing="1" cellpadding="4" align="center">
<tr>
<td bgcolor="#FFFFFF" width="30%"><select onChange=goforum(this.options[this.selectedIndex].value)><option value=#>論壇跳轉</option>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -