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

📄 编译原理的算符优先文法[网页版]).htm

📁 编译原理的算符优先文法[网页版]).htm 源程序对该算法的实现
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0063)http://bsonline.cvv.cn/lxl/bsarticle/printpage.asp?ArticleID=63 -->
<HTML><HEAD><TITLE>编译原理的算符优先文法[网页版]:)</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312"><LINK 
href="编译原理的算符优先文法[网页版]).files/STYLE.css" type=text/css rel=stylesheet>
<META content="MSHTML 6.00.2900.2604" name=GENERATOR></HEAD>
<BODY leftMargin=2 topMargin=0 marginheight="0" marginwidth="0">
<TABLE cellSpacing=0 cellPadding=0 width="90%" border=0>
  <TBODY>
  <TR>
    <TD>
      <P align=center><FONT 
      size=6>编译原理的算符优先文法[网页版]:)</FONT><BR>作者:bs&nbsp;&nbsp;&nbsp;&nbsp;转贴自:本站原创&nbsp;&nbsp;&nbsp;&nbsp;点击数:431</P>
      <P><FONT color=#ff0000><STRONG>c.asp</STRONG></FONT></P>
      <P>&lt;% Option Explicit 
      %&gt;<BR>&lt;html&gt;<BR>&lt;title&gt;算符优先关系表生成程序&lt;/title&gt;<BR>&lt;style&gt;<BR>body&nbsp;{ 
      font-size: 14px }<BR>input&nbsp;{ font-size: 14px; height: 20px; border: 
      1px, solid; border-color: #666666; background: #ffffff }<BR>table&nbsp;{ 
      border: solid 1 #999999; }<BR>tr,td&nbsp;{ border: solid 1 #999999; 
      }<BR>&lt;/style&gt;<BR>&lt;body&gt;<BR>&lt;% 
      <BR>'*********************************************************************************************<BR>'==============================<BR>Dim 
      P(), a()&nbsp;'动态数组<BR>Dim stack(), tempStack, 
      tabStr&nbsp;'动态数组前两个,tabstr为字符串<BR>ReDim stack(1, 
      0)&nbsp;'初始化数组<BR>stack(0, 0) = 0<BR>stack(1, 0) = 0<BR>Dim 
      F()&nbsp;'动态数组<BR>Dim str, strArray&nbsp;'前者为字符串,后者为字符数组<BR>Dim tempStrP, 
      tempStra, countP, counta, m, n, tempm, tempn, TEMP, ERR<BR>Dim i, j, 
      k<BR>Dim Firstvt, strFirstvt, Lastvt, 
      strLastvt<BR>'==============================<BR>str = 
      Trim(Request.Form(str))<BR>Response.write &lt;li&gt;您输入的文法为: &lt;font 
      color=blue&gt;&lt;b&gt; &amp; str &amp; 
      &lt;/b&gt;&lt;/font&gt;&lt;/li&gt;&lt;br&gt;<BR>strArray = split(str, 
      )<BR>If Not checkInput() or str = Then&nbsp;'第一次检查输入文法是否符合要求,防止P-&gt;E 
      E|F类型错误<BR>&nbsp; Response.Write(&lt;li&gt;文法输入错误!&lt;a 
      href=a.htm&gt;返回&lt;/a&gt;&lt;/li&gt;)<BR>&nbsp; Response.End<BR>End 
      If<BR>Call chStr&nbsp;'改变有“|”的文法为两个式子<BR>str = Join(strArray, )<BR>If Not 
      checkInput() or str = Then&nbsp;'第二次检查输入文法是否符合要求,防止P-&gt; 
      P-&gt;E类型错误<BR>&nbsp; Response.Write(&lt;li&gt;文法输入错误!&lt;a 
      href=a.htm&gt;返回&lt;/a&gt;&lt;/li&gt;)<BR>&nbsp; Response.End<BR>End 
      If<BR>'--------------------------求P(m)数组的最大维数,并给P(m)赋值<BR>tempStrP = 
      Left(str, 1)<BR>For i = 1 to Len(str)-1&nbsp;'非终结符组成字符串<BR>&nbsp; If 
      Mid(str, i, 1) = then&nbsp;'以空格来隔开各个文法,如果空格存在,说明有一个非终结符<BR>&nbsp;tempStrP 
      = tempStrP &amp; Mid(str, Instr(i, str, )+1, 1)<BR>&nbsp; End 
      IF<BR>Next<BR>For i = 0 to Len(tempStrP)-2&nbsp;'去掉重复<BR>&nbsp; tempStrP = 
      Left(tempStrP,i+1) &amp; Replace(Mid(tempStrP, i+2), Mid(tempStrP, i+1, 
      1), )<BR>Next<BR>tempStrP = Replace(tempStrP, , )<BR>countP = 
      Len(tempStrP)&nbsp;'得到非终结符的个数<BR>m = countP - 1<BR>ReDim 
      P(m)&nbsp;&nbsp;&nbsp;&nbsp;'定义非终结符数组的维数<BR>For i = 0 to 
      m&nbsp;&nbsp;&nbsp;'给非终结符数组赋值<BR>&nbsp; P(i) = Mid(tempStrP, i+1, 
      1)<BR>Next<BR>'--------------------------求a(n)数组的最大维数,并给a(n)赋值<BR>tempStra 
      = Replace(Replace(str, ,), -&gt;, )&nbsp;'得到除去 和文法表达的-&gt;的字符串<BR>For i = 
      0 to m&nbsp;&nbsp;&nbsp;'得到除去非终结符的字符串<BR>&nbsp; tempStra = 
      Replace(tempStra, P(i), )<BR>Next<BR>For i = 0 to 
      Len(tempStra)-2&nbsp;'去掉重复<BR>&nbsp; tempStra = Left(tempStra,i+1) &amp; 
      Replace(Mid(tempStra, i+2), Mid(tempStra, i+1, 1), )<BR>Next<BR>tempStra = 
      Replace(tempStra, , )<BR>counta = Len(tempStra)&nbsp;'得到终结符的个数<BR>n = 
      counta - 1<BR>ReDim a(n)<BR>For i = 0 to 
      n&nbsp;&nbsp;&nbsp;'给终结符数组赋值<BR>&nbsp; a(i) = Mid(tempStra, i+1, 
      1)<BR>Next<BR>'--------------------------------------------------------<BR>ReDim 
      F(m, n)&nbsp;&nbsp;&nbsp;'动态生成数组F(m, 
      n)<BR>'--------------------------按规则(1)对F(m, n)赋初值,并将F(m, 
      n)为真的符号对放入栈stack中<BR>For i = 0 to m<BR>&nbsp; For j = 0 to n<BR>&nbsp;If 
      pToa(P(i), a(j)) or PToQa(P(i), a(j)) Then&nbsp;'如果p-&gt;a 或 
      p-&gt;Qa<BR>&nbsp;&nbsp; F(i, j) = 1<BR>&nbsp;&nbsp; pushStack P(i), 
      a(j)&nbsp;'放入栈 Stack 中<BR>&nbsp;Else<BR>&nbsp;&nbsp; F(i, j) = 
      0<BR>&nbsp;End If<BR>&nbsp; 
      Next<BR>Next<BR>'--------------------------输出F(m, 
      n)数组的初值<BR>'Response.write(&lt;li&gt;F( &amp; m+1 &amp; , &amp; n+1 &amp; 
      )数组初值如下表&lt;/li&gt;)<BR>'outForL()<BR>'--------------------------下面执行(2)<BR>'Response.write 
      tempstrp<BR>While Ubound(stack, 2) &gt; 0<BR>&nbsp; tempStack = 
      Split(popStack(), )<BR>&nbsp; For i = 0 to 
      Ubound(strArray)<BR>&nbsp;&nbsp;&nbsp; If Mid(strArray(i), 4, 1) = 
      tempStack(0) Then&nbsp;'如果p-&gt;Q…<BR>&nbsp;&nbsp; tempm = Instr(tempStrP, 
      Left(strArray(i), 1)) - 1&nbsp;'F(m, n)的 m 
      值**************************<BR>&nbsp;&nbsp; tempn = Instr(tempStra, 
      tempStack(1)) - 1&nbsp;&nbsp;&nbsp;'F(m, n)的 n 
      值**************************<BR>&nbsp;&nbsp; If F(tempm, tempn) = 0 
      Then<BR>&nbsp;&nbsp;&nbsp;&nbsp; F(tempm, tempn) = 
      1<BR>&nbsp;&nbsp;&nbsp;&nbsp; pushStack Left(strArray(i), 1), 
      tempStack(1)<BR>&nbsp;&nbsp; End If<BR>&nbsp;End If<BR>&nbsp; 
      Next<BR>Wend<BR>'--------------------------输出F(m, 
      n)数组的终值<BR>'Response.write(&lt;li&gt;F( &amp; m+1 &amp; , &amp; n+1 &amp; 
      )数组终值如下表&lt;/li&gt;)<BR>'outForL()<BR>'--------------------------开始生成Firstvt集合<BR>ReDim 
      strFirstvt(m+1)<BR>strFirstvt(0) = 0<BR>For i = 0 to m<BR>&nbsp; FirstVt = 
      ,<BR>&nbsp; For j = 0 to n<BR>&nbsp;&nbsp;&nbsp; If F(i, j) and Not 
      Instr(Firstvt, a(j)) Then<BR>&nbsp;&nbsp; Firstvt = Firstvt &amp; a(j) 
      &amp; ,<BR>&nbsp;Else<BR>&nbsp;&nbsp; Firstvt = Firstvt<BR>&nbsp;End 
      If<BR>&nbsp; Next<BR>&nbsp; strFirstvt(i+1) = Replace(FirstVt, ,, 
      )<BR>&nbsp; Response.write &lt;li&gt; &amp; P(i)&nbsp; &amp; 的FirstVt集合为: 
      { &lt;b&gt;&lt;font color=red&gt;<BR>&nbsp; If Len(FirstVt) &gt;= 2 
      Then<BR>&nbsp;Response.write Replace(Mid(FirstVt, 2, Len(FirstVt) - 2 ), 
      ,, &lt;/font&gt;&lt;/b&gt;, &lt;b&gt;&lt;font color=red&gt;)<BR>&nbsp; End 
      If<BR>&nbsp; Response.write &lt;/font&gt;&lt;/b&gt; 
      }&lt;/li&gt;&lt;br&gt;<BR>Next<BR>'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<BR>'ReDim 
      F(m, n)&nbsp;&nbsp;&nbsp;'动态生成数组F(m, 
      n)<BR>'--------------------------按规则(1)对F(m, n)赋初值,并将F(m, 
      n)为真的符号对放入栈stack中<BR>For i = 0 to m<BR>&nbsp; For j = 0 to n<BR>&nbsp;If 
      pTo_a(P(i), a(j)) or PToaQ(P(i), a(j)) Then&nbsp;'如果p-&gt;..a 或 
      p-&gt;..aQ<BR>&nbsp;&nbsp; F(i, j) = 1<BR>&nbsp;&nbsp; pushStack P(i), 
      a(j)&nbsp;'放入栈 Stack 中<BR>&nbsp;Else<BR>&nbsp;&nbsp; F(i, j) = 
      0<BR>&nbsp;End If<BR>&nbsp; 
      Next<BR>Next<BR>'--------------------------输出F(m, 
      n)数组的初值<BR>'Response.write(&lt;li&gt;F( &amp; m+1 &amp; , &amp; n+1 &amp; 
      )数组初值如下表&lt;/li&gt;)<BR>'outForL()<BR>'--------------------------下面执行(2)<BR>'Response.write 
      tempstrp<BR>While Ubound(stack, 2) &gt; 0<BR>&nbsp; tempStack = 
      Split(popStack(), )<BR>&nbsp; For i = 0 to 
      Ubound(strArray)<BR>&nbsp;&nbsp;&nbsp; If Mid(strArray(i), 
      Len(strArray(i)), 1) = tempStack(0) 
      Then&nbsp;'如果p-&gt;..Q&nbsp;&nbsp;&nbsp;&nbsp;'++++++<BR>&nbsp;&nbsp; 
      tempm = Instr(tempStrP, Left(strArray(i), 1)) - 1&nbsp;'F(m, n)的 m 
      值**************************<BR>&nbsp;&nbsp; tempn = Instr(tempStra, 
      tempStack(1)) - 1&nbsp;&nbsp;&nbsp;'F(m, n)的 n 
      值**************************<BR>&nbsp;&nbsp; If F(tempm, tempn) = 0 
      Then<BR>&nbsp;&nbsp;&nbsp;&nbsp; F(tempm, tempn) = 
      1<BR>&nbsp;&nbsp;&nbsp;&nbsp; pushStack Left(strArray(i), 1), 
      tempStack(1)<BR>&nbsp;&nbsp; End If<BR>&nbsp;End If<BR>&nbsp; 
      Next<BR>Wend<BR>'--------------------------输出F(m, 
      n)数组的终值<BR>'Response.write(&lt;li&gt;F( &amp; m+1 &amp; , &amp; n+1 &amp; 
      )数组终值如下表&lt;/li&gt;)<BR>'outForL()<BR>'--------------------------开始生成Lastvt集合<BR>ReDim 
      strLastvt(m+1)<BR>strLastvt(0) = 0<BR>For i = 0 to m<BR>&nbsp; LastVt = 
      ,<BR>&nbsp; For j = 0 to n<BR>&nbsp;&nbsp;&nbsp; If F(i, j) and Not 
      Instr(Lastvt, a(j)) Then<BR>&nbsp;&nbsp; Lastvt = Lastvt &amp; a(j) &amp; 
      ,<BR>&nbsp;Else<BR>&nbsp;&nbsp; Lastvt = Lastvt<BR>&nbsp;End If<BR>&nbsp; 
      Next<BR>&nbsp; strLastvt(i+1) = Replace(LastVt, ,, )<BR>&nbsp; 
      Response.write &lt;li&gt; &amp; P(i) &amp; 的LastVt集合为: { &lt;b&gt;&lt;font 
      color=red&gt; <BR>&nbsp; If Len(LastVt) &gt;= 2 
      Then<BR>&nbsp;Response.write Replace(Mid(LastVt, 2, Len(LastVt) - 2 ), ,, 
      &lt;/font&gt;&lt;/b&gt;, &lt;b&gt;&lt;font color=red&gt;)<BR>&nbsp; End 
      If<BR>&nbsp; Response.write &lt;/font&gt;&lt;/b&gt; 
      }&lt;/li&gt;&lt;br&gt;<BR>Next<BR>'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<BR>'--------------------------此处生成优先关系表<BR>tabStr 
      = <BR>For i = 0 to Ubound(strArray)<BR>&nbsp; For j = 4 to 
      Len(strArray(i)) - 1<BR>&nbsp;&nbsp;&nbsp; If Instr(tempStra, 
      Mid(strArray(i), j, 1)) &gt; 0 And Instr(tempStra, Mid(strArray(i), j + 1, 
      1)) &gt; 0 Then<BR>&nbsp;&nbsp; tabStr = tabStr &amp; a(Instr(tempStra, 

⌨️ 快捷键说明

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