编译原理实现一个简单的计算器程序.doc

上传人:飞****2 文档编号:78930997 上传时间:2023-03-19 格式:DOC 页数:8 大小:123.50KB
返回 下载 相关 举报
编译原理实现一个简单的计算器程序.doc_第1页
第1页 / 共8页
编译原理实现一个简单的计算器程序.doc_第2页
第2页 / 共8页
点击查看更多>>
资源描述

《编译原理实现一个简单的计算器程序.doc》由会员分享,可在线阅读,更多相关《编译原理实现一个简单的计算器程序.doc(8页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、编译原理实验报告专 业:学 号:班 级:姓 名:实验题目:设计,实现一个简单的计算器程序 实验时间: 指导教师: 实验成绩:1 实验目的1.1 实现一个简单计算器2 实验要求2.1 单词的构词规则有明确的定义;2.2 编写的分析程序能够正确识别源程序中的语法符号;2.3 计算器源程序的以.c格式输出2.4 对于源程序中的词法错误,能够做出简单的错误处理,给出简单的错误提示,保证顺利完成整个源程序的词法分析,语法分析;2.5 计算器能够实现加,减,乘,除,混合运算,多项式运算。3 实验环境3.1 Windows XP3.2 Flex词法分析工具3.3 Visual Studio C+ 6.04

2、实验原理4.1 多项式计算器的实现,采用后缀表达式来对输入的计算表达式进行计算4.2 对后缀表达式进行符号识别,词法分析,语法分析4.3 编写计算器的源程序,使用Flex工具生成计算器的C语言程序4.4 对生成的程序进行相应的修改,再进行编译,连接,运行,测试得到可以用于进行多项式运算的源程序。5 软件设计与编程5.1 程序源代码:5.2 新建文件夹5.3 将Flex工具复制到该文件夹下5.4 在DOS环境学生成目标源程序5.5 在VisualStudio C+ 6.0环境下进行相应的修改,添加main方法和打开文件的方法6 程序测试结果6.1 编写源代码6.2 使用Flex生成C代码: 6.

3、3 在Visual Studio C+6.0上运行生成的”.c”文件文件:y.ta.h#ifndef YYTOKENTYPE# define YYTOKENTYPE enum yytokentype CLEAR = 258, EXIT = 259, LIST = 260, ERASE = 261, DEC = 262, HEX = 263, OCT = 264, HELP = 265, NUM = 266, REG = 267, ADD = 268, SUB = 269, MUL = 270, DIV = 271, MOD = 272, LSHIFT = 273, RSHIFT = 274,

4、AND = 275, OR = 276, NOT = 277, LESS = 278, MORE = 279, BITAND = 280, BITOR = 281, BITXOR = 282, BITREV = 283, SIN = 284, COS = 285, TAN = 286, ABS = 287, SQRT = 288, LOG = 289, RMINUS = 290 ;/前面定义过,此处省略宏定义#if ! defined (YYSTYPE) & ! defined (YYSTYPE_IS_DECLARED)typedef int YYSTYPE;# define yystype

5、YYSTYPE /* obsolescent; will be withdrawn */# define YYSTYPE_IS_DECLARED 1# define YYSTYPE_IS_TRIVIAL 1#endifextern YYSTYPE yylval;文件:calc.l核心程序%#define YYSTYPE double#define BIGINT long#include #include y.tab.h%digit0-9xdigit0-9a-fA-Fodigit0-7decnum(0(.digit+)?)|(1-9digit*(.digit+)?)octnum0odigit+h

6、exnum0(x|X)xdigit+rega-zA-Zopt1+|-|*|/|&|%|!|opt2(&)|(|)|()exit(E|e)(X|x)(I|i)(T|t)|(Q|q)(U|u)(I|i)(T|t)clear(C|c)(L|l)(E|e)(A|a)(R|r)list(L|l)(I|i)(S|s)(T|t)erase (E|e)(R|r)(A|a)(S|s)(E|e)hex(H|h)(E|e)(X|x)oct(O|o)(C|c)(T|t)dec(D|d)(E|e)(C|c)help(H|h)(E|e)(L|l)(P|p)sin (S|s)(I|i)(N|n)cos (C|c)(O|o)

7、(S|s)log (L|l)(O|o)(G|g)tan (T|t)(A|a)(N|n)abs (A|a)(B|b)(S|s)sqrt (S|s)(Q|q)(R|r)(T|t)%int i ;BIGINT val; ; t decnumsscanf(yytext,%lf,&yylval);return(NUM); octnumi=1;val=0;while(iyyleng)val=(val3)+yytexti-0;i+;yylval=val;return(NUM);hexnumi=2;val=0;while(iyyleng)if(islower(yytexti) val=(val4)+yytex

8、ti-a+10;else if(isupper(yytexti) val=(val4)+yytexti-A+10;else val=(val4)+yytexti-0;i+;yylval=val;return(NUM);regif(islower(yytext0)yylval=yytext0-a;else yylval=yytext0-A;return(REG);opt1switch(yytext0)case +:return ADD;break;case -:return SUB;break;case *:return MUL;break;case /:return DIV;break;cas

9、e %:return MOD;break;case :return BITXOR;break;case &:return BITAND;break;case |:return BITOR;break;case :return MORE;break;case !:return NOT;break;case :return BITREV;break;opt2switch(yytext0)case &:return AND;break;case |:return OR;break;case :return RSHIFT;break;sin return (SIN);cos return (COS);

10、tan return (TAN);log return (LOG);abs return (ABS);sqrt return (SQRT);clearreturn(CLEAR);exitreturn(EXIT);listreturn(LIST);erasereturn(ERASE);hexreturn(HEX);octreturn(OCT);decreturn(DEC);helpreturn(HELP);.|nreturn(yytext0);文件:y.tab.c部分代码:#define YYBISON 1/* Skeleton name. */#define YYSKELETON_NAME y

11、acc.c/* Pure parsers. */#define YYPURE 0/* Using locations. */#define YYLSP_NEEDED 0/* Tokens. */#ifndef YYTOKENTYPE# define YYTOKENTYPE /* Put the tokens into the symbol table, so that GDB and other debuggers know about them. */ enum yytokentype 前面定义过,此处省略 ;/* Copy the first part of user declaratio

12、ns. */#line 1 calc.y/前面定义过,此处省略宏定义#include #include #include #include #include lex.yy.c /这是词法分析器生成的文件,必须包含!YYSTYPE reg26=0;BIGINT ival;enum DisplayDEC_ON, HEX_ON, OCT_ON;enum Display dflag=DEC_ON;int i;/* Enabling traces. */#ifndef YYDEBUG# define YYDEBUG 0#endif/* Enabling verbose error messages. *

13、/#ifdef YYERROR_VERBOSE# undef YYERROR_VERBOSE# define YYERROR_VERBOSE 1#else# define YYERROR_VERBOSE 0#endif#if ! defined (YYSTYPE) & ! defined (YYSTYPE_IS_DECLARED)typedef int YYSTYPE;# define yystype YYSTYPE /* obsolescent; will be withdrawn */# define YYSTYPE_IS_DECLARED 1# define YYSTYPE_IS_TRI

14、VIAL 1#endif/* Copy the second part of user declarations. */* Line 214 of yacc.c. */#line 173 y.tab.c#if ! defined (yyoverflow) | YYERROR_VERBOSE/* The parser invokes alloca or malloc; define the necessary symbols. */# if YYSTACK_USE_ALLOCA# define YYSTACK_ALLOC alloca# else# ifndef YYSTACK_USE_ALLO

15、CA# if defined (alloca) | defined (_ALLOCA_H)# define YYSTACK_ALLOC alloca# else# ifdef _GNUC_# define YYSTACK_ALLOC _builtin_alloca# endif# endif# endif# endif# ifdef YYSTACK_ALLOC /* Pacify GCCs empty if-body warning. */# define YYSTACK_FREE(Ptr) do /* empty */; while (0)# else# if defined (_STDC_

16、) | defined (_cplusplus)# include /* INFRINGES ON USER NAME SPACE */# define YYSIZE_T size_t# endif# define YYSTACK_ALLOC malloc# define YYSTACK_FREE free# endif#endif /* ! defined (yyoverflow) | YYERROR_VERBOSE */#if (! defined (yyoverflow) & (! defined (_cplusplus) | (YYSTYPE_IS_TRIVIAL)/* A type

17、that is properly aligned for any stack member. */union yyalloc short yyss; YYSTYPE yyvs; ;/* The size of the maximum gap between one aligned stack and the next. */# define YYSTACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1)/* The size of an array large to enough to hold all stacks, each with N elements

18、. */# define YYSTACK_BYTES(N) (N) * (sizeof (short) + sizeof (YYSTYPE) + YYSTACK_GAP_MAXIMUM)/* Copy COUNT objects from FROM to TO. The source and destination do not overlap. */# ifndef YYCOPY# if 1 _GNUC_# define YYCOPY(To, From, Count) _builtin_memcpy (To, From, (Count) * sizeof (*(From)# else# de

19、fine YYCOPY(To, From, Count) do register YYSIZE_T yyi; for (yyi = 0; yyi Stack, Stack, yysize);Stack = &yyptr-Stack;yynewbytes = yystacksize * sizeof (*Stack) + YYSTACK_GAP_MAXIMUM; yyptr += yynewbytes / sizeof (*yyptr); while (0)#endif#if defined (_STDC_) | defined (_cplusplus) typedef signed char

20、yysigned_char;#else typedef short yysigned_char;#endif/* YYFINAL - State number of the termination state. */#define YYFINAL 2/* YYLAST - Last index in YYTABLE. */#define YYLAST 188/* YYNTOKENS - Number of terminals. */#define YYNTOKENS 40/* YYNNTS - Number of nonterminals. */#define YYNNTS 5/* YYNRU

21、LES - Number of rules. */#define YYNRULES 42/* YYNRULES - Number of states. */#define YYNSTATES 73/* YYTRANSLATE(YYLEX) - Bison symbol number corresponding to YYLEX. */#define YYUNDEFTOK 2#define YYMAXUTOK 290#define YYTRANSLATE(YYX) (unsigned int) (YYX) = YYMAXUTOK ? yytranslateYYX : YYUNDEFTOK)/*

22、YYTRANSLATEYYLEX - Bison symbol number corresponding to YYLEX. */static const unsigned char yytranslate =省略数据信息;/* YYRHS - A -1-separated list of the rules RHS. */static const yysigned_char yyrhs =省略数据信息;/* YYRLINEYYN - source line where rule number YYN was defined. */static const unsigned char yyrl

23、ine =省略数据信息;#endif#if YYDEBUG | YYERROR_VERBOSE/* YYTNMESYMBOL-NUM - String name of the symbol SYMBOL-NUM. First, the terminals, then, starting at YYNTOKENS, nonterminals. */static const char *const yytname = $end, error, $undefined, CLEAR, EXIT, LIST, ERASE, DEC, HEX, OCT, HELP, NUM, REG, ADD, SUB,

24、 MUL, DIV, MOD, LSHIFT, RSHIFT, AND, OR, NOT, LESS, MORE, BITAND, BITOR, BITXOR, BITREV, SIN, COS, TAN, ABS, SQRT, LOG, RMINUS, n, =, (, ), $accept, lines, statement, expr, cmdline, 0;#endif# ifdef YYPRINT/* YYTOKNUMYYLEX-NUM - Internal token number corresponding to token YYLEX-NUM. */static const u

25、nsigned short yytoknum = 省略数据信息;# endif/* YYR1YYN - Symbol number of symbol that rule YYN derives. */static const unsigned char yyr1 = 省略数据信息;/* YYDEFACTSTATE-NAME - Default rule to reduce with in state STATE-NUM when YYTABLE doesnt specify something else to do. Zero means the default is an error. *

26、/static const unsigned char yydefact = 省略数据信息;/* YYDEFGOTONTERM-NUM. */static const yysigned_char yydefgoto =省略数据;/* YYPACTSTATE-NUM - Index in YYTABLE of the portion describing STATE-NUM. */#define YYPACT_NINF -28static const short yypact = 省略数据信息;/* YYPGOTONTERM-NUM. */static const yysigned_char y

27、ypgoto =省略数据信息;/* YYTABLEYYPACTSTATE-NUM. What to do in state STATE-NUM. If positive, shift that token. If negative, reduce the rule which number is the opposite. If zero, do what YYDEFACT says. If YYTABLE_NINF, syntax error. */#define YYTABLE_NINF -1static const unsigned char yytable = 省略数据信息;#if !

28、 defined (YYSIZE_T) & defined (_SIZE_TYPE_)# define YYSIZE_T _SIZE_TYPE_#endif#if ! defined (YYSIZE_T) & defined (size_t)# define YYSIZE_T size_t#endif#if ! defined (YYSIZE_T)# if defined (_STDC_) | defined (_cplusplus)# include /* INFRINGES ON USER NAME SPACE */# define YYSIZE_T size_t# endif#endif

29、#if ! defined (YYSIZE_T)# define YYSIZE_T unsigned int#endif#define yyerrok(yyerrstatus = 0)#define yyclearin(yychar = YYEMPTY)#define YYEMPTY(-2)#define YYEOF0#define YYACCEPTgoto yyacceptlab#define YYABORTgoto yyabortlab#define YYERRORgoto yyerrlab1/* Like YYERROR except do call yyerror. This rema

30、ins here temporarily to ease the transition to the new meaning of YYERROR, for GCC. Once GCC version 2 has supplanted version 1, this can go. */#define YYFAILgoto yyerrlab#define YYRECOVERING() (!yyerrstatus)#define YYBACKUP(Token, Value)do if (yychar = YYEMPTY & yylen = 1) yychar = (Token); yylval

31、= (Value); yytoken = YYTRANSLATE (yychar); YYPOPSTACK; goto yybackup; else yyerror (syntax error: cannot back up); YYERROR; while (0)#define YYTERROR1#define YYERRCODE256/* YYLLOC_DEFAULT - Compute the default location (before the actions are run). */#ifndef YYLLOC_DEFAULT# define YYLLOC_DEFAULT(Cur

32、rent, Rhs, N) Current.first_line = Rhs1.first_line; Current.first_column = Rhs1.first_column; Current.last_line = RhsN.last_line; Current.last_column = RhsN.last_column;#endif/* YYLEX - calling yylex with the right arguments. */#ifdef YYLEX_PARAM# define YYLEX yylex (YYLEX_PARAM)#else# define YYLEX

33、yylex ()#endif/* Enable debugging if requested. */#if YYDEBUG# ifndef YYFPRINTF# include /* INFRINGES ON USER NAME SPACE */# define YYFPRINTF fprintf# endif# define YYDPRINTF(Args)do if (yydebug) YYFPRINTF Args; while (0)# define YYDSYMPRINT(Args)do if (yydebug) yysymprint Args; while (0)# define YY

34、DSYMPRINTF(Title, Token, Value, Location)do if (yydebug) YYFPRINTF (stderr, %s , Title); yysymprint (stderr, Token, Value); YYFPRINTF (stderr, n); while (0)/*-.| yy_stack_print - Print the state stack from its BOTTOM up to its | TOP (cinluded). |-*/#if defined (_STDC_) | defined (_cplusplus)static v

35、oidyy_stack_print (short *bottom, short *top)#elsestatic voidyy_stack_print (bottom, top) short *bottom; short *top;#endif YYFPRINTF (stderr, Stack now); for (/* Nothing. */; bottom = top; +bottom) YYFPRINTF (stderr, %d, *bottom); YYFPRINTF (stderr, n);# define YY_STACK_PRINT(Bottom, Top)do if (yyde

36、bug) yy_stack_print (Bottom), (Top); while (0)#if defined (_STDC_) | defined (_cplusplus)static voidyy_reduce_print (int yyrule)#elsestatic voidyy_reduce_print (yyrule) int yyrule;#endif int yyi; unsigned int yylno = yyrlineyyrule; YYFPRINTF (stderr, Reducing stack by rule %d (line %u), , yyrule - 1, yylno); /* Print the symbols being reduced, and th

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁