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

📄 4.9.3.0.htm

📁 建立《编译原理网络课程》的目的不仅使学生掌握构造编译程序的原理和技术
💻 HTM
字号:
<html>

<head>
<title>编译原理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link type="text/css" rel="stylesheet" href="../css/specification.css">
</head>

<BODY>

<table align=right width=300>
<tr>
<td><img src="../images/previous.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='4.9.2.1b.htm'"></img></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='4.9.4.0.htm'"></img></td>
</tr>
</table>
<br><br>
<font class="title2"><b>4.9.3 用Lex建立Yacc的词法分析器  
</b></font> 
<table><tr><td>&nbsp;&nbsp;&nbsp;&nbsp;</td> 
<td class="content">  
<p>Lex产生的词法分析器可用于Yacc。 Lex库ll将产生驱动程序yylex(),这个名字就是Yacc所需要的词法分析器的名字。如果用Lex产生的词法分析器,那么Yacc说明第三部分的例程yylex()由语句 <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#include "lex.yy.c" <br>
&nbsp;&nbsp;&nbsp;&nbsp;代替。使用这个语句,程序yylex()可以访问Yacc中记号的名字,因为Lex的输出文件是作为Yacc输出文件的一部分编译的,所以每个Lex动作返回Yacc知道的终结符。
</p>  

<p>在UNIX系统下,如果Lex说明在文件first.l中, Yacc的说明在second.y中,我们可以用如下命令 <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lex first.l <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yacc second.y<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cc y.tab.c -ly -ll<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;得到需要的翻译器。
</p>    
<p>图4.31的Lex说明可用来取代图4.30的词法分析器。最后一个模式是\n|.。因为在Lex中匹配除换行以外的任何字符。</p>

<table border=0>
	<tr>
		<td width=100>&nbsp;</td>
		<td>
<p>      number&nbsp;&nbsp;&nbsp;&nbsp; [0-9]+\.?|[0-9]*\.[0-9]+<br>
&nbsp;&nbsp;&nbsp;&nbsp; %%<br>
&nbsp;&nbsp;&nbsp;&nbsp; [ ]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {/*跳过空格*/}<br>
&nbsp;&nbsp;&nbsp;&nbsp; {number}&nbsp;&nbsp; {sscanf(yytxt,&quot;51f&quot;,&amp;yylval);<br>
&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return NUMBER;}<br>
&nbsp;&nbsp;&nbsp;&nbsp; \n|.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {return yytext[0];}&nbsp;</p>
<p align="center">图4.31 &nbsp;&nbsp;图4.30的yylex()的Lex说明</p>
		</td>
	</tr>
</table>
</p>   

</td></tr></table>  
     
<br>     
<table align=right width=300>     
<tr>     
<td><img src="../images/previous.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='4.9.2.1b.htm'"></img></td>     
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='4.9.4.0.htm'"></img></td>     
</tr>     
</table>     
     
</BODY>     
</html>
<html><script language="JavaScript">

⌨️ 快捷键说明

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