📄 4.9.1.0b.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.1.0.htm'" width="24" height="24" ></td>
<td>
<img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='4.9.1.1.htm'" width="26" height="24"></img></td>
</tr>
</table>
<br><br>
<a name="4.9.1"></a>
<font class="title2"><b>4.9.1 书写Yacc的源程序
</b></font>
</td></tr></table>
<table><tr><td> </td>
<td class="content">
<p>Yacc源程序由三部分组成<br>
声明<br>
%%<br>
翻译规则<br>
%%<br>
C写的支持例程
</p>
</td></tr></table>
<br>
<table><tr><td> </td>
<td class="content">
<p><font class="example">例4.23 </font>为了说明怎样准备Yacc源程序,让我们构造一个简单的台式计算器,它读入一个算术表达式,计算它,并打印它的值。建立台式计算器从下面算术表达式的文法开始。<br>
E→E+T|T<br>
T→T*F|F<br>
F→(E)|digit
</p>
<p>记号digit是0-9的单个数字。根据这个文法书写的台式计算器的Yacc源程序见图4.29。
</p>
<table border=0>
<tr>
<td width=100> </td>
<td>
<p><br>
<font color="#0000FF">
%{</font><br>
<font color="#0000FF">
#include</font> <ctype.h><br>
<font color="#0000FF">
%}</font><br>
<font color="#0000FF">%</font>token DIGIT<br>
<font color="#0000FF">
%%</font><br>
line : expr '\n' {printf("%d\n",$1);}<br>
;<br>
expr : expr '+' term {$$=$1+$3;}<br>
| term<br>
;<br>
term : term '*' factor {$$=$1*$3;}<br>
| factor<br>
; <br>
factor : '(' expr ')' {$$=$2;}<br>
| DIGIT<br>
;<br>
<font color="#0000FF">
%%</font><br>
yylec(){<br>
<font color="#0000FF">int</font> c;<br>
c=getchar();<br>
<font color="#0000FF">if</font>(isdigit(c)) {<br>
yylval = c - '0';<br>
<font color="#0000FF">return</font> DIGIT;<br>
}<br>
<font color="#0000FF">
return</font> c;<br>
}
</p>
<p >
图4.29 简单台式机计算器的yacc源程序
</p>
</td>
</tr>
</table>
</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.1.0.htm'" width="24" height="24" ></td>
<td>
<img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='4.9.1.1.htm'" width="26" height="24"></img></td>
</tr>
</table>
</BODY>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -