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

📄 中间代码生成.html

📁 编译原理的几个课堂实验报告,极力推荐--对广大在校学生是个很好的借鉴材料,读别人东西也是学习嘛
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script language="javascript">        

var  top=0 ;            // 栈指针
var  tempIndex =1;      // 临时变量计数
var  sIndex=0;
var  tIndex=1; 
var  tempVar ;            // 临时变量字符串

sym=new Array();          //存储输入串的数组
stack=new Array();

function newTemp() 
{
   tempVar='t'+tempIndex;
   //document.write(tempVar) ;
   tempIndex++ ;
}
function statement()   //S → i := E	
{//document.write('S/'+sym[sIndex]);
   if( sym[sIndex]>='a'&&sym[sIndex]<='z')
   {
    // 标识符压栈
      stack[top]=sym[sIndex] ;
      top++ ;
      sIndex++;
	  if(sym[sIndex]==':')  
	  {
	     sIndex++;
		 if(sym[sIndex]=='=')  
		 {
		    sIndex++;
			expr();
			document.write( '<tr><td> '+(tIndex++)+' <td>(:=,'+ stack[top-1]+', _,'+stack[top-2]+')' );
		 }
	  }
   }
}
function expr()        //E → T E1
{//document.write('E/'+sym[sIndex]);
   term();
   expr_1();
}
function expr_1()      //E1 → +T E1 | -T E1 | ε
{//document.write('E1/'+sym[sIndex]);
   if( sym[sIndex]=='+')
   {
      sIndex++;
	  term() ;
	  newTemp() ;    // 生成新的临时变量名  
	  document.write( '<tr><td> '+(tIndex++)+' <td>(+,'+ stack[top-2]+','+ 
	                   stack[top-1]+','+tempVar+')' ) ;  
      stack[ top-2 ]=tempVar ;
      top-- ;        // 调整栈指针
	  expr_1() ;
   }
   else  if( sym[sIndex]=='-' )
   {
      sIndex++;
	  term() ;
	  newTemp() ;    // 生成新的临时变量名  
	  document.write( '<tr><td> '+(tIndex++)+' <td>(-,'+ stack[top-2]+','+ 
	                   stack[top-1]+','+tempVar+')' ) ;  
      stack[ top-2 ]=tempVar ;
      top-- ;       // 调整栈指针
	  expr_1() ;
   }		

}
function term()       //T → F T1
{//document.write('T/'+sym[sIndex]);
   factor();
   term_1();
}
function  term_1()    //T1 → *T T1 | /T T1 | ε
{//document.write('T1/'+sym[sIndex]);
   if( sym[sIndex]=='*')
   {
      sIndex++;
	  term() ;
	  newTemp() ;    // 生成新的临时变量名  
	  document.write( '<tr><td> '+(tIndex++)+' <td>(*,' + stack[top-2]+','+
	                   stack[top-1]+','+tempVar+')' ) ;  
      stack[ top-2 ]=tempVar ;
      top-- ;        // 调整栈指针
	  term_1() ;
   }
   else  if( sym[sIndex]=='/' )
   {
      sIndex++;
	  term() ;
	  newTemp() ;    // 生成新的临时变量名  
	  document.write( '<tr><td> '+(tIndex++)+' <td>(/,'+ stack[top-2]+','+
	                   stack[top-1]+','+tempVar+')' ) ;  
      stack[ top-2 ]=tempVar ;
      top-- ;       // 调整栈指针
	  term_1() ;
   }		
}
function factor()     //F → P F1
{//document.write('F/'+sym[sIndex]);
   power();
   factor_1();
}
function  factor_1()  //F1 → ^F F1 |  ε
{//document.write('F1/'+sym[sIndex]);
   if( sym[sIndex]=='^')
   {
      sIndex++;
	  factor() ;
	  newTemp() ;    // 生成新的临时变量名  
	  document.write( '<tr><td> '+(tIndex++)+' <td>(^,'+ stack[top-2]+','+ 
	                   stack[top-1]+','+tempVar+')' ) ;  
      stack[ top-2 ]=tempVar ;
      top-- ;        // 调整栈指针
	  factor_1() ;
   }
}
function  power()     //P → n|(E)	
{//document.write('P/'+sym[sIndex]);
   if( sym[sIndex]>='a'&&sym[sIndex]<='z')
   {
    // 标识符压栈
      stack[top]=sym[sIndex] ;
      top++ ;
      sIndex++;
   }
   else if( sym[sIndex] == '(' )
   {
      sIndex++;
      expr() ;
      sIndex++;
   } 		
}

function execute()
{
   t=document.form.sym.value;
   sym=t.split('');   
   document.write('<table border=1>');
   statement();
   document.write('</table>');
}

</script>

</head>

<body>

<p>中间代码生成</p>
<form name="form"   >

请输入待分析串
<input name='sym' type="text"  />

<input type="button"  value="确定" onclick="execute()" />

</form>
</body>
</html>

⌨️ 快捷键说明

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