📄 4.9.2.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.2.0.htm'"></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='4.9.2.1.htm'"></img></td>
</tr>
</table>
<br><br>
<font class="title2"><b>4.9.2 用Yacc处理二义文法
</b></font>
<table><tr><td> </td>
<td class="content">
<table border=0>
<tr>
<td width=100> </td>
<td>
<font size="3"><font color="#0000FF">%{</font><br>
<font color="#0000FF">
#include</font> <ctype.h><br>
<font color="#0000FF">
#include</font> <stdio.h><br>
<font color="#0000FF">
#define</font> YYSTYPE double <font color="#008000">/*Yacc栈定义为double类型*/</font><br>
<font color="#0000FF">
%}</font><br>
<font color="#0000FF">%</font>token NUMBER<br>
<font color="#0000FF">%</font>left '+' '-'<br>
<font color="#0000FF">%</font>left '*' '/'<br>
<font color="#0000FF">%</font>right UMINUS<br>
<font color="#0000FF">
%%</font><br>
lines :lines expr '\n' {printf("%g\n",$2);}<br>
|lines '\n'<br>
|/*<span style="mso-bidi-font-size: 12.0pt; font-family: 宋体; mso-bidi-font-family: Times New Roman; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA">ε*/<br>
;<br>
expr :expr '+' expr {$$=$1+$3;}<br>
|expr '-' expr
{$$=$1-$3;}<br>
|expr '*' expr
{$$=$1*$3;}<br>
|expr '/' expr
{$$=$1/$3;}<br>
|'(' expr %
')'{$$=$2;}<br>
| '-' expr %prec
UMINUS {$$=-$2;}<br>
| NUMBER<br>
;<br>
<font color="#0000FF">
%%</font><br>
</span><span style="mso-bidi-font-size: 12.0pt; font-family: 宋体; mso-bidi-font-family: Times New Roman; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA">yylec(){<br>
<font color="#0000FF">int</font> c;<br>
<font color="#0000FF">while</font> ((c=getchar())==' ');<br>
<font color="#0000FF">if</font>((c=='.')||(isdigit(c))){<br>
ungetc(c,stdin);<br>
scanf("%1f",&yylval);<br>
<font color="#0000FF">return</font> NMUBER;<br>
}<br>
<font color="#0000FF">return</font> c:</span></font>
<p><span style="mso-bidi-font-size: 12.0pt; font-family: 宋体; mso-bidi-font-family: Times New Roman; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"><font size="3">}</font></span>
</p>
<p> 图4.30 更高级的台式计算器的Yacc源程序 </p>
</td>
</tr>
</table>
<p>因为图4.30的Yacc说明的文法是二义的,LALR算法将产生有冲突的分析动作。Yacc会报告产生地分析动作的冲突数。项目集和分析动作冲突的描述可以在调用Yacc时加-V选择得到.这个选择产生一个附加的文件y.output,它包含分析时发现的项目集的核,由LALR算法产生的分析动作冲突的描述,以及LR分析表的可读表示,它给出分析动作冲突是怎样解决的。当Yacc报告它发现分析动作冲突时,明智的做法是建立和查阅文件y.output,以明白为什么会出现分析动作的冲突和它们是否已经正确解决。
</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.0.htm'"></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='4.9.2.1.htm'"></img></td>
</tr>
</table>
</BODY>
<html><script language="JavaScript">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -