⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 一个基于ll(1)文法的语法分析程序.htm

📁 LL(1)分析文法的一种
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0059)http://blog.csdn.net/happyjyr/archive/2004/07/11/39184.aspx -->
<HTML xmlns:i<<"]Vn><HEAD><TITLE>一个基于LL(1)文法的语法分析程序</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<META content="MSHTML 6.00.2600.0" name=GENERATOR><LINK media=all 
href="一个基于LL(1)文法的语法分析程序.files/style.css" type=text/css rel=stylesheet><LINK 
media=print href="一个基于LL(1)文法的语法分析程序.files/print.css" type=text/css 
rel=stylesheet><LINK title=RSS href="http://blog.csdn.net/happyjyr/rss.aspx" 
type=application/rss+xml rel=alternate></HEAD>
<BODY>
<FORM language=javascript id=Form1 name=Form1 
onsubmit="if (!ValidatorOnSubmit()) return false;" action=39184.aspx 
method=post><INPUT type=hidden name=__EVENTTARGET> <INPUT type=hidden 
name=__EVENTARGUMENT> <INPUT type=hidden name=__VIEWSTATE>
<SCRIPT language=javascript type=text/javascript>
<!--
	function __doPostBack(eventTarget, eventArgument) {
		var theform;
		if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
			theform = document.Form1;
		}
		else {
			theform = document.forms["Form1"];
		}
		theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
		theform.__EVENTARGUMENT.value = eventArgument;
		theform.submit();
	}
// -->
</SCRIPT>

<SCRIPT language=javascript src="一个基于LL(1)文法的语法分析程序.files/WebUIValidation.js" 
type=text/javascript></SCRIPT>
 
<DIV id=top>
<DIV>
<TABLE>
  <TBODY>
  <TR>
    <TD class=HeaderTitles>
      <H1><A class=headermaintitle id=Header1_HeaderTitle 
      href="http://blog.csdn.net/happyjyr/">happyjyr的专栏</A></H1>
      <P id=tagline></P></TD></TR></TBODY></TABLE></DIV>
<DIV class=HeaderBar>
<TABLE class=HeaderBar id=HeaderBar>
  <TBODY>
  <TR>
    <TD class=HeaderBarTab noWrap>&nbsp; <A id=Header1_MyLinks1_HomeLink 
      title=到聚合站点 href="http://blog.csdn.net/">CSDNBlog</A>&nbsp;&nbsp;|&nbsp; 
      <A id=Header1_MyLinks1_ContactLink accessKey=9 
      href="http://blog.csdn.net/happyjyr/contact.aspx">联络</A>&nbsp;&nbsp;|&nbsp; 
      <A id=Header1_MyLinks1_Syndication 
      href="http://blog.csdn.net/happyjyr/Rss.aspx">聚合</A> <A class=XMLLink 
      id=Header1_MyLinks1_XMLLink 
      href="http://blog.csdn.net/happyjyr/Rss.aspx"><IMG alt="" 
      src="一个基于LL(1)文法的语法分析程序.files/xml.gif" border=0></A>&nbsp;&nbsp;|&nbsp; <A 
      id=Header1_MyLinks1_Admin 
      href="http://blog.csdn.net/happyjyr/login.aspx">登录</A> <IMG 
      id=Header1_BlueTab alt="" src="一个基于LL(1)文法的语法分析程序.files/BlueTabRight.jpg" 
      align=absMiddle border=0> </TD>
    <TD class=HeaderBarTabBack noWrap width="100%">
      <DIV class=BlogStatsBar>
      <TABLE class=BlogStatsBar>
        <TBODY>
        <TR>
          <TD width="100%"></TD>
          <TD class=BlogStatsBar noWrap>&nbsp; 9 篇文章&nbsp;&nbsp;|&nbsp;&nbsp; 
            2 篇收藏&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp; 3 
            个评论&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp; 0 个Trackbacks 
      </TD></TR></TBODY></TABLE></DIV></TD></TR></TBODY></TABLE></DIV></DIV>
<DIV id=leftmenu>
<H3>文章</H3>
<UL>
  <LI><A id=SingleColumn1_Categories_CatList__ctl0_LinkList__ctl1_Link 
  href="http://blog.csdn.net/happyjyr/category/23586.aspx">About Me</A> <A 
  id=SingleColumn1_Categories_CatList__ctl0_LinkList__ctl1_RssLink 
  title="订阅了About Me" 
  href="http://blog.csdn.net/happyjyr/category/23586.aspx/rss">(rss)</A> 
  <LI><A id=SingleColumn1_Categories_CatList__ctl0_LinkList__ctl2_Link 
  href="http://blog.csdn.net/happyjyr/category/23303.aspx">web编程学习中</A> <A 
  id=SingleColumn1_Categories_CatList__ctl0_LinkList__ctl2_RssLink 
  title=订阅了web编程学习中 
  href="http://blog.csdn.net/happyjyr/category/23303.aspx/rss">(rss)</A> 
  <LI><A id=SingleColumn1_Categories_CatList__ctl0_LinkList__ctl3_Link 
  href="http://blog.csdn.net/happyjyr/category/23300.aspx">大学生活</A> <A 
  id=SingleColumn1_Categories_CatList__ctl0_LinkList__ctl3_RssLink title=订阅了大学生活 
  href="http://blog.csdn.net/happyjyr/category/23300.aspx/rss">(rss)</A> 
  <LI><A id=SingleColumn1_Categories_CatList__ctl0_LinkList__ctl4_Link 
  href="http://blog.csdn.net/happyjyr/category/23313.aspx">我的程序</A> <A 
  id=SingleColumn1_Categories_CatList__ctl0_LinkList__ctl4_RssLink title=订阅了我的程序 
  href="http://blog.csdn.net/happyjyr/category/23313.aspx/rss">(rss)</A> 
  <LI><A id=SingleColumn1_Categories_CatList__ctl0_LinkList__ctl5_Link 
  href="http://blog.csdn.net/happyjyr/category/23299.aspx">挚爱的vc</A> <A 
  id=SingleColumn1_Categories_CatList__ctl0_LinkList__ctl5_RssLink 
  title=订阅了挚爱的vc 
  href="http://blog.csdn.net/happyjyr/category/23299.aspx/rss">(rss)</A> 
  <LI><A id=SingleColumn1_Categories_CatList__ctl0_LinkList__ctl6_Link 
  href="http://blog.csdn.net/happyjyr/category/23302.aspx">最后的暑假</A> <A 
  id=SingleColumn1_Categories_CatList__ctl0_LinkList__ctl6_RssLink 
  title=订阅了最后的暑假 
  href="http://blog.csdn.net/happyjyr/category/23302.aspx/rss">(rss)</A> 
</LI></UL>
<H3>收藏</H3>
<UL>
  <LI><A id=SingleColumn1_Categories_CatList__ctl1_LinkList__ctl1_Link 
  href="http://blog.csdn.net/happyjyr/category/23309.aspx">01的世界</A> 
  <LI><A id=SingleColumn1_Categories_CatList__ctl1_LinkList__ctl2_Link 
  href="http://blog.csdn.net/happyjyr/category/23307.aspx">操作系统</A> 
  <LI><A id=SingleColumn1_Categories_CatList__ctl1_LinkList__ctl3_Link 
  href="http://blog.csdn.net/happyjyr/category/23308.aspx">精彩美文</A> </LI></UL>
<H3>存档</H3>
<UL>
  <LI><A id=SingleColumn1_Categories_CatList__ctl2_LinkList__ctl1_Link 
  href="http://blog.csdn.net/happyjyr/archive/2004/10.aspx">2004年10月 (1)</A> 
  <LI><A id=SingleColumn1_Categories_CatList__ctl2_LinkList__ctl2_Link 
  href="http://blog.csdn.net/happyjyr/archive/2004/09.aspx">2004年09月 (1)</A> 
  <LI><A id=SingleColumn1_Categories_CatList__ctl2_LinkList__ctl3_Link 
  href="http://blog.csdn.net/happyjyr/archive/2004/08.aspx">2004年08月 (1)</A> 
  <LI><A id=SingleColumn1_Categories_CatList__ctl2_LinkList__ctl4_Link 
  href="http://blog.csdn.net/happyjyr/archive/2004/07.aspx">2004年07月 (6)</A> 
  </LI></UL><BR><BR></DIV>
<DIV id=main>
<DIV class=post>
<DIV class=postTitle><A id=viewpost.ascx_TitleUrl 
href="http://blog.csdn.net/happyjyr/archive/2004/07/11/39184.aspx">一个基于LL(1)文法的语法分析程序</A> 
</DIV>
<DIV class=postText>
<P>这是编译课程设计时做的一个小小的语法分析程序,程序有些冗余.实现的功能还不全,只能通过输入文法的分析表才能给出分析过程.对于求任意文法的first集和follow集还没实现.</P>
<P>/*<BR>&nbsp;* 文件名称: 语法分析.h<BR>&nbsp;* 摘&nbsp;&nbsp;&nbsp; 要: 
对任意输入LL(1)文法的分析表及字符串,本程序能自动判断所给字符串是<BR>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
否为该文法的句子,并能给出分析过程。<BR>*/<BR>#include<IOSTREAM.H><BR>#include<STRING.H><BR>#include<STDIO.H><BR>#include"ctype.h"<BR>class 
string;<BR>class stack<BR>{<BR>&nbsp;char stck[100];<BR>&nbsp;int&nbsp; 
top;<BR>public:<BR>&nbsp;void init()<BR>&nbsp;{top=0;}<BR>&nbsp;void push(char 
ch);<BR>&nbsp;char pop();<BR>&nbsp;&nbsp;&nbsp; void 
showstck();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
//显示栈中元素<BR>};<BR>class string{<BR>&nbsp;int length;<BR>&nbsp;char 
*ptr;<BR>public: <BR>&nbsp;string();<BR>&nbsp;~string()<BR>&nbsp;{ 
}<BR>&nbsp;string(char *s);<BR>&nbsp;int getlength();<BR>&nbsp;&nbsp;&nbsp; int 
getlength(char *s);<BR>&nbsp;&nbsp;&nbsp; char 
&amp;operator[](int);<BR>&nbsp;&nbsp;&nbsp; string &amp;operator=(const string 
&amp;ob);<BR>&nbsp;&nbsp;&nbsp; bool operator== (const string&amp; 
pw);<BR>&nbsp;&nbsp;&nbsp; friend ostream&amp; 
operator&lt;&lt;(ostream&amp;,const string&amp;);<BR>&nbsp;friend istream&amp; 
operator&gt;&gt;(istream&amp;,string&amp;);<BR>};<BR>void stack::push(char 
ch)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
//入栈<BR>{ <BR>&nbsp;stck[top]=ch;<BR>&nbsp;top++;<BR>}<BR>char 
stack::pop()<BR>{&nbsp;&nbsp; <BR>&nbsp;if(top==0)<BR>&nbsp;{<BR>&nbsp;&nbsp; 
cout&lt;&lt;"stack is empty!";<BR>&nbsp;&nbsp; return 
0;<BR>&nbsp;}<BR>&nbsp;top--;<BR>&nbsp;return stck[top];<BR>}<BR>void 
stack::showstck()<BR>{<BR>&nbsp;int i;<BR>&nbsp;for(i=0;i<TOP;I++)<BR 
/>&nbsp;cout&lt;<STCK[I];<BR 
/>}<BR>string::string()<BR>{&nbsp;&nbsp;&nbsp;&nbsp; 
length=1;<BR>&nbsp;&nbsp;&nbsp;&nbsp; ptr=new 
char[1];<BR>&nbsp;&nbsp;&nbsp;&nbsp; *ptr='\0';<BR>}<BR>string::string(char 
*s)<BR>{&nbsp;&nbsp; ptr=new char[getlength(s)+1];<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
strcpy(ptr,s);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
length=getlength(s)+1;<BR>}<BR>int string::getlength()<BR>{<BR>&nbsp;int 
len;<BR>&nbsp;len=getlength(ptr);<BR>&nbsp;return len;<BR>}<BR>int 
string::getlength(char *s)<BR>{ int i=0;<BR>&nbsp; while(s[i]!='\0')<BR>&nbsp; 
{i++;<BR>&nbsp; }<BR>&nbsp; return i;<BR>}<BR>char &amp;string::operator[](int 
j)<BR>{<BR>&nbsp;&nbsp; return ptr[j];<BR>} <BR>string &amp;string::operator 
=(const string &amp;ob)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
//重载‘=’运算符<BR>{if(this==&amp;ob)<BR>&nbsp; return *this;<BR>&nbsp;&nbsp; delete 
ptr;<BR>&nbsp;&nbsp; ptr=new char[getlength(ob.ptr)+1];<BR>&nbsp;&nbsp; 
length=getlength(ob.ptr)+1;<BR>&nbsp; strcpy(ptr,ob.ptr);<BR>&nbsp;&nbsp; return 
*this;<BR>}<BR>bool string::operator== (const string &amp;pw) 
<BR>{<BR>&nbsp;if(*ptr==*pw.ptr)<BR>&nbsp;&nbsp;return 
true;<BR>&nbsp;else<BR>&nbsp;&nbsp;return false;<BR>} <BR>ostream&amp; 
operator&lt;&lt;(ostream&amp;s,const 
string&amp;ob)<BR>{<BR>&nbsp;s&lt;<OB.PTR;<BR />&nbsp;&nbsp;&nbsp; return 
s;<BR>}<BR>istream&amp; operator&gt;&gt;(istream&amp;in,string&amp;ob)<BR>{ 
<BR>&nbsp;in&gt;&gt;ob.ptr ;<BR>&nbsp;&nbsp;&nbsp; return in; <BR>}<BR>bool 
find(char c,char array[] 
)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
//查找函数,返回布尔值<BR>{ <BR>&nbsp;int i;<BR>&nbsp;&nbsp;&nbsp; bool 
flag=false;<BR>&nbsp;for(i=0;i&lt;100;i++)<BR>&nbsp;{<BR>&nbsp;&nbsp;if(c==array[i])<BR>&nbsp;&nbsp; 
flag=true;<BR>&nbsp;}<BR>&nbsp;return flag;<BR>}<BR>int location(char c,char 
array[])&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
//定位函数,指出字符所在位置<BR>{<BR>&nbsp;int 
i;<BR>&nbsp;for(i=0;i&lt;100;i++)<BR>&nbsp;{<BR>&nbsp;&nbsp;if(c==array[i])<BR>&nbsp;&nbsp;&nbsp;return 
i;<BR>&nbsp;}<BR>}<BR>void error()<BR>{<BR>&nbsp;&nbsp; 
cout&lt;&lt;"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
出错!"&lt;<ENDL;<BR />}<BR>void analyse(char Vn[],char Vt[],string 
M[100][100])<BR>{<BR>&nbsp;&nbsp; int i,j,m,p,q,length,t,h;<BR>&nbsp;&nbsp; char 
w,X;<BR>&nbsp;&nbsp; string str;<BR>opt0:<BR>&nbsp;&nbsp; 
cin&gt;&gt;str;<BR>&nbsp;&nbsp; for(i=0;i<STR.GETLENGTH();I++)<BR />&nbsp;&nbsp; 
{<BR>&nbsp;&nbsp;&nbsp; if(!find(str[i],Vt))<BR>&nbsp;&nbsp;&nbsp; {&nbsp; 
cout&lt;&lt;"输入字符串有误!请重新输入!"&lt;<ENDL;<BR />&nbsp;&nbsp;&nbsp; goto 
opt0;<BR>&nbsp;&nbsp;&nbsp; break;}<BR>&nbsp;&nbsp; }<BR>&nbsp;&nbsp; stack 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -