📄 使用lex&yacc设计一个计算器的例子 - fohoo的专栏 - csdnblog.htm
字号:
function(result) { Anthem_PostProcessCallBack( result, control, eventTarget, null, null, imageUrlDuringCallBack, textDuringCallBack, postCallBackFunction, preProcessOut ); }, null, includeControlValuesWithCallBack, updatePageAfterCallBack ); }}function AnthemListControl_OnClick( e, causesValidation, validationGroup, textDuringCallBack, enabledDuringCallBack, preCallBackFunction, postCallBackFunction, callBackCancelledFunction, includeControlValuesWithCallBack, updatePageAfterCallBack) { var target = e.target || e.srcElement; if (target.nodeName.toUpperCase() == "LABEL" && target.htmlFor != '') return; var eventTarget = target.id.split("_").join("$"); Anthem_FireCallBackEvent( target, e, eventTarget, '', causesValidation, validationGroup, '', textDuringCallBack, enabledDuringCallBack, preCallBackFunction, postCallBackFunction, callBackCancelledFunction, true, true );}function GetLabelText(id) { var labels = document.getElementsByTagName('label'); for (var i = 0; i < labels.length; i++) { if (labels[i].htmlFor == id) { return labels[i].innerHTML; } } return null;}function SetLabelText(id, text) { var labels = document.getElementsByTagName('label'); for (var i = 0; i < labels.length; i++) { if (labels[i].htmlFor == id) { labels[i].innerHTML = text; return; } }}//]]></SCRIPT>
<SCRIPT
src="使用LEX&YACC设计一个计算器的例子 - fohoo的专栏 - CSDNBlog.files/WebResource.axd"
type=text/javascript></SCRIPT>
<SCRIPT type=text/javascript>
<!--
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
return true;
}
// -->
</SCRIPT>
<DIV id=top>
<DIV>
<TABLE>
<TBODY>
<TR>
<TD class=HeaderTitles>
<H1><A class=headermaintitle id=Header1_HeaderTitle
href="http://blog.csdn.net/fohoo/">fohoo的专栏</A></H1>
<P id=tagline></P></TD></TR></TBODY></TABLE></DIV>
<DIV class=HeaderBar>
<TABLE class=HeaderBar id=HeaderBar>
<TBODY>
<TR>
<TD class=HeaderBarTab noWrap> <A href="http://www.csdn.net/"
target=_blank>CSDN</A> | <A href="http://community.csdn.net/"
target=_blank>社区</A> | <A href="http://dev.csdn.net/"
target=_blank>技术中心</A> | <A id=Header1_MyLinks1_HomeLink
title=到聚合站点 href="http://blog.csdn.net/"
target=_blank>BLOG首页</A> | <A id=Header1_MyLinks1_PersonalHome
title="访问 fohoo的专栏" href="http://blog.csdn.net/fohoo/"
target=_blank>我的首页</A> | <A id=Header1_MyLinks1_PersonalResume
href="http://job.csdn.net/resumes/fohoo.aspx">个人档案</A> | <A
id=Header1_MyLinks1_ContactLink accessKey=9
href="http://blog.csdn.net/fohoo/contact.aspx">联系作者</A> | <A
id=Header1_MyLinks1_Syndication
href="http://blog.csdn.net/fohoo/Rss.aspx">聚合</A> <A class=XMLLink
id=Header1_MyLinks1_XMLLink
href="http://blog.csdn.net/fohoo/Rss.aspx"><IMG
src="使用LEX&YACC设计一个计算器的例子 - fohoo的专栏 - CSDNBlog.files/xml.gif"
border=0></A> | <A id=Header1_MyLinks1_HyperLink1
href="http://search.csdn.net/search_blog.asp"
target=_blank>搜索</A> | <A id=Header1_MyLinks1_Admin
href="http://writeblog.csdn.net/">登录</A> <IMG id=Header1_BlueTab
src="使用LEX&YACC设计一个计算器的例子 - fohoo的专栏 - CSDNBlog.files/BlueTabRight.jpg"
align=absMiddle border=0> </TD>
<TD class=HeaderBarTabBack noWrap width="100%">
<DIV class=BlogStatsBar>
<TABLE class=BlogStatsBar>
<TBODY>
<TR>
<TD width="100%"></TD>
<TD class=BlogStatsBar noWrap> 6篇原创: 0篇翻译: 3篇转载: 1989次点击:
24个评论: 0个Trackbacks
</TD></TR></TBODY></TABLE></DIV></TD></TR></TBODY></TABLE></DIV></DIV>
<DIV id=leftmenu>
<H3 class=listtitle>文章</H3>
<UL class=list>
<LI class=listitem><A
href="http://blog.csdn.net/fohoo/category/183332.aspx">C/C++</A><A
href="http://blog.csdn.net/fohoo/category/183332.aspx/rss">(RSS)</A>
<LI class=listitem><A
href="http://blog.csdn.net/fohoo/category/183333.aspx">Java</A><A
href="http://blog.csdn.net/fohoo/category/183333.aspx/rss">(RSS)</A>
<LI class=listitem><A
href="http://blog.csdn.net/fohoo/category/80141.aspx">编译原理</A><A
href="http://blog.csdn.net/fohoo/category/80141.aspx/rss">(RSS)</A>
<LI class=listitem><A
href="http://blog.csdn.net/fohoo/category/80140.aspx">操作系统</A><A
href="http://blog.csdn.net/fohoo/category/80140.aspx/rss">(RSS)</A></LI></UL>
<H3 class=listtitle>收藏</H3>
<UL class=list>
<LI class=listitem><A
href="http://blog.csdn.net/fohoo/category/80139.aspx">C/C++</A>
<LI class=listitem><A
href="http://blog.csdn.net/fohoo/category/80138.aspx">JAVA</A>
<LI class=listitem><A
href="http://blog.csdn.net/fohoo/category/92799.aspx">Math</A></LI></UL>
<H3 class=listtitle>相册</H3><!--category title-->
<UL class=list></UL>
<H3 class=listtitle>存档</H3>
<UL class=list>
<LI><A href="http://blog.csdn.net/fohoo/archive/2007/01.aspx">2007年01月(1)</A>
<LI><A href="http://blog.csdn.net/fohoo/archive/2006/12.aspx">2006年12月(1)</A>
<LI><A href="http://blog.csdn.net/fohoo/archive/2006/10.aspx">2006年10月(1)</A>
<LI><A href="http://blog.csdn.net/fohoo/archive/2006/03.aspx">2006年03月(1)</A>
<LI><A href="http://blog.csdn.net/fohoo/archive/2004/12.aspx">2004年12月(2)</A>
<LI><A
href="http://blog.csdn.net/fohoo/archive/2004/06.aspx">2004年06月(1)</A></LI></UL><SPAN
id=Anthem_RecentComments_ltlComments__><SPAN
id=RecentComments_ltlComments></SPAN></SPAN><BR><BR></DIV>
<DIV id=main>
<DIV class=Tag>
<SCRIPT language=javascript
src="使用LEX&YACC设计一个计算器的例子 - fohoo的专栏 - CSDNBlog.files/urltag.aspx"></SCRIPT>
<DIV style="CLEAR: both"></DIV></DIV>
<SCRIPT>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</SCRIPT>
<DIV class=post>
<DIV class=postTitle><A
href="http://blog.csdn.net/fohoo/archive/2006/10/05/1321144.aspx"><IMG height=13
src="使用LEX&YACC设计一个计算器的例子 - fohoo的专栏 - CSDNBlog.files/authorship.gif"
width=15 border=0> 使用LEX&YACC设计一个计算器的例子</A> </DIV>
<DIV class=postText>
<P><STRONG>calc.l</STRONG></P>
<P>%{<BR>#include <math.h><BR>#include <stdlib.h><BR>#include
<stdio.h><BR>#include "calc.tab.h"<BR>%}</P>
<P>%option noyywrap</P>
<P>%%<BR>([0-9])+|([0-9])*"."([0-9])+ { yylval = atof( yytext
); /*printf("yytext:%s, yylval:%f\n", yytext, yylval);*/ return NUM; }<BR>[
\t]+<BR>"\n"
{ return '\n';
}<BR>.
{ return *yytext; }</P>
<P>%%<BR></P>
<P><STRONG> calc.y</STRONG></P>
<P>%{<BR> #define YYSTYPE double<BR> #include
<math.h><BR> #include <stdio.h><BR> int yylex
(void);<BR> void yyerror (char const *);<BR>%}</P>
<P>/* Bison declarations. */<BR>%token NUM<BR>%left '-' '+'<BR>%left '*'
'/'<BR>%left NEG /* negation--unary minus */<BR>%right
'^' /* exponentiation */</P>
<P>%% /* The grammar follows. */<BR>input: /* empty
*/<BR> | input line<BR>;</P>
<P>line: '\n'<BR> |
exp '\n' { printf ("%f\n", $1); }<BR>;</P>
<P>exp:
NUM
{ $$ = $1; /*printf("%f =
%f\n", $$,
$1);*/
}<BR> | exp '+'
exp { $$ = $1 +
$3; /*printf("%f = %f + %f\n", $$, $1,
$3);*/ }<BR> | exp
'-' exp { $$ = $1 -
$3; /*printf("%f = %f - %f\n", $$, $1,
$3);*/ }<BR> | exp
'*' exp { $$ = $1 *
$3; /*printf("%f = %f * %f\n", $$, $1,
$3);*/ }<BR> | exp
'/' exp { $$ = $1 /
$3; /*printf("%f = %f / %f\n", $$, $1,
$3);*/ }<BR> | '-'
exp %prec NEG { $$ =
-$2; /*printf("%f =
-%f\n",
$2);*/
}<BR> | exp '^'
exp { $$ = pow ($1, $3); /*printf("%f
= pow(%f, %f)\n", $$, $1, $3);*/ }<BR> | '('
exp ')' { $$ =
$2; /*printf("%f =
(%f)\n", $$,
$2);*/
}<BR>;<BR>%%</P>
<P><BR>然后使用<A href="http://gnuwin32.sourceforge.net/packages/flex.htm">FLEX</A>,
<A href="http://gnuwin32.sourceforge.net/packages/bison.htm">BISON</A>生成C源程序</P>
<P>flex calc.l -->lex.yy.c<BR>bison -d calc.y
-->calc.tab.c(-d生成头文件calc.tab.h给calc.l使用)</P>
<P>然后在calc.tab.c加入:</P>
<P>int main()<BR>{<BR> return yyparse(); //调用语法分析yyparse<BR>}</P>
<P>void yyerror(char const *s)<BR>{<BR> fprintf(stderr, "%s\n", s);
//语法分析出错显示<BR>}</P>
<P>使用VC编译生成calc.exe运行。</P><BR><BR>
<P id=TBPingURL>Trackback:
http://tb.blog.csdn.net/TrackBack.aspx?PostId=1321144</P><BR></DIV>
<DIV class=postFoot>
<SCRIPT src=""></SCRIPT>
[<A title=功能强大的网络收藏夹,一秒钟操作就可以轻松实现保存带来的价值、分享带来的快乐
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(saveit=window.open('http://wz.csdn.net/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=590,height=300,left=75,top=20,status=no,resizable=yes'));saveit.focus();">收藏到我的网摘</A>]
fohoo发表于 2006年10月05日 01:51:00 </DIV></DIV><LINK
href="http://blog.csdn.net/fohoo/Services/Pingback.aspx" rel=pingback><!--<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"xmlns:dc="http://purl.org/dc/elements/1.1/"xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"><rdf:Descriptionrdf:about="http://blog.csdn.net/fohoo/archive/2006/10/05/1321144.aspx"dc:identifier="http://blog.csdn.net/fohoo/archive/2006/10/05/1321144.aspx"dc:title="使用LEX&YACC设计一个计算器的例子"trackback:ping="http://tb.blog.csdn.net/TrackBack.aspx?PostId=1321144" /></rdf:RDF>-->
<SCRIPT>function hide(){showComment();}</SCRIPT>
<BR>
<DIV class=post>
<DIV class=postTitle>相关文章:</DIV>
<UL class=postText>
<LI><A href="http://blog.csdn.net/csumck/archive/2006/02/22/606362.aspx"
target=_blank>使用flex和bison编写logo语言的编译器</A> 2006-02-22 <A
href="http://blog.csdn.net/csumck/" target=_blank>csumck</A>
<LI><A href="http://blog.csdn.net/idler/archive/2002/03/25/4847.aspx"
target=_blank>AOV工程网络图的计算</A> 2002-03-25 <A href="http://blog.csdn.net/idler/"
target=_blank>idler</A>
<LI><A href="http://blog.csdn.net/njhhack/archive/2001/06/27/6618.aspx"
target=_blank>表达式运算器的实现原理(快速优先法)</A> 2001-06-27 <A
href="http://blog.csdn.net/njhhack/"
target=_blank>njhhack</A></LI></UL></DIV><BR><BR>
<DIV class=post id=csdn_zhaig_ad_yahoo></DIV><SPAN
id=Anthem_Comments.ascx_ltlComments__><SPAN id=Comments.ascx_ltlComments><BR>
<DIV id=comments>
<H3></H3>没有评论。 </DIV></SPAN></SPAN>
<SCRIPT language=javascript>
ad_width=468;
ad_height=60;
adcss=2;
unionuser=19;
ad_type='j';
count=5;
</SCRIPT>
<SCRIPT language=javascript
src="使用LEX&YACC设计一个计算器的例子 - fohoo的专栏 - CSDNBlog.files/showads.js"
type=text/javascript></SCRIPT>
<SCRIPT language=javascript
src="使用LEX&YACC设计一个计算器的例子 - fohoo的专栏 - CSDNBlog.files/showgm.htm"
type=text/javascript></SCRIPT>
<SCRIPT type=text/javascript>document.write("<img src=http://counter.csdn.net/pv.aspx?id=24 border=0 width=0 height=0>");</SCRIPT>
<DIV class=CommentForm id=commentform>
<H3>发表评论</H3>
<DIV id=Anthem_PostComment.ascx_UpdatePanel1__>
<DIV id=PostComment.ascx_UpdatePanel1>
<TABLE class=CommentForm>
<TBODY>
<TR>
<TD width=69 height=0></TD>
<TD></TD></TR>
<TR>
<TD width=70>大名:</TD>
<TD align=left><INPUT id=PostComment.ascx_tbName style="WIDTH: 300px"
disabled maxLength=32 size=40 name=PostComment.ascx:tbName> <SPAN
id=PostComment.ascx_RequiredFieldValidator2
style="DISPLAY: none; COLOR: red" initialvalue=""
evaluationfunction="RequiredFieldValidatorEvaluateIsValid"
display="Dynamic" errormessage="<br>请输入尊姓大名"
controltovalidate="PostComment.ascx_tbName"><BR>请输入尊姓大名</SPAN> </TD></TR>
<TR>
<TD width=70>网址:</TD>
<TD align=left><INPUT id=PostComment.ascx_tbUrl style="WIDTH: 300px"
disabled maxLength=256 size=40 name=PostComment.ascx:tbUrl> </TD></TR>
<TR>
<TD colSpan=3>评论 <SPAN id=PostComment.ascx_RequiredFieldValidator3
style="DISPLAY: none; COLOR: red" initialvalue=""
evaluationfunction="RequiredFieldValidatorEvaluateIsValid"
display="Dynamic" errormessage="<br>请输入评论"
controltovalidate="PostComment.ascx_tbComment"><BR>请输入评论</SPAN> <BR><TEXTAREA id=PostComment.ascx_tbComment style="WIDTH: 381px; HEIGHT: 193px" disabled name=PostComment.ascx:tbComment rows=10 cols=50></TEXTAREA>
</TD></TR>
<TR>
<TD colSpan=3><SPAN
id=Anthem_PostComment.ascx_btnSubmit__></SPAN> </TD></TR>
<TR>
<TD colSpan=3><SPAN id=PostComment.ascx_Message
style="COLOR: red">注册用户才能发表评论。如果你没有登录,请点击<A
href="http://passport.csdn.net/member/UserLogin.aspx?from=http://blog.csdn.net/fohoo/archive/2006/10/05/1321144.aspx">登录</A></SPAN>
</TD></TR></TBODY></TABLE></DIV></DIV></DIV></DIV>
<P id=footer>Powered by: <BR><A id=Footer1_Hyperlink2
href="http://scottwater.com/blog" name=Hyperlink1><IMG
src="使用LEX&YACC设计一个计算器的例子 - fohoo的专栏 - CSDNBlog.files/100x30_Logo.gif"
border=0></A> <A id=Footer1_Hyperlink3 href="http://asp.net/"
name=Hyperlink1><IMG
src="使用LEX&YACC设计一个计算器的例子 - fohoo的专栏 - CSDNBlog.files/PoweredByAsp.Net.gif"
border=0></A> <BR>Copyright © fohoo </P>
<SCRIPT
src="使用LEX&YACC设计一个计算器的例子 - fohoo的专栏 - CSDNBlog.files/counter.js"></SCRIPT>
<SCRIPT type=text/javascript>
<!--
var Page_Validators = new Array(document.getElementById("PostComment.ascx_RequiredFieldValidator2"), document.getElementById("PostComment.ascx_RequiredFieldValidator3"));
// -->
</SCRIPT>
<INPUT id=__EVENTVALIDATION type=hidden
value=/wEWBAL+raDpAgKqtOi0AwLAsuLbDAKi687YCaEdUuRgIvhS8PYPDsd5LslVNRL0
name=__EVENTVALIDATION>
<SCRIPT type=text/javascript>
<!--
var Page_ValidationActive = false;
if (typeof(ValidatorOnLoad) == "function") {
ValidatorOnLoad();
}
function ValidatorOnSubmit() {
if (Page_ValidationActive) {
return ValidatorCommonOnSubmit();
}
else {
return true;
}
}
// -->
</SCRIPT>
</FORM>
<SCRIPT language=javascript type=text/javascript>
<!--
try{
hide();
}
catch(e){
}
//-->
</SCRIPT>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -