云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 编译原理实现一个简单的计算器程序

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

  • 62 次阅读
  • 3 次下载
  • 2025/12/11 18:29:21

《编译原理》实验报告

专 业: 学 号: 班 级: 姓 名:

实验题目:设计,实现一个简单的计算器程序 实验时间: 指导教师: 实验成绩: 1 2

实验目的

1.1 实现一个简单计算器 实验要求

2.1 单词的构词规则有明确的定义;

2.2 编写的分析程序能够正确识别源程序中的语法符号; 2.3 计算器源程序的以.c格式输出

2.4 对于源程序中的词法错误,能够做出简单的错误处理,给出简单的错误

提示,保证顺利完成整个源程序的词法分析,语法分析; 2.5 计算器能够实现加,减,乘,除,混合运算,多项式运算。

3

实验环境

3.1 Windows XP

3.2 Flex词法分析工具

3.3 Visual Studio C++ 6.0 实验原理

4.1 多项式计算器的实现,采用后缀表达式来对输入的计算表达式进行计算 4.2 对后缀表达式进行符号识别,词法分析,语法分析

4.3 编写计算器的源程序,使用Flex工具生成计算器的C语言程序

4.4 对生成的程序进行相应的修改,再进行编译,连接,运行,测试得到可以用于进行多项式运算的源程序。

5 软件设计与编程

5.1 程序源代码: 5.2 新建文件夹

5.3 将Flex工具复制到该文件夹下 5.4 在DOS环境学生成目标源程序

5.5 在Visual Studio C++ 6.0环境下进行相应的修改,添加main方法和打开文件的方

6 程序测试结果

6.1 编写源代码

6.2 使用Flex生成C代码:

6.3 在Visual Studio C++6.0上运行生成的”.c”文件

4

cos 文件:y.ta.h

(C|c)(O|o)(S|s)

log (L|l)(O|o)(G|g)

#ifndef YYTOKENTYPE tan # define YYTOKENTYPE (T|t)(A|a)(N|n)

enum yytokentype { abs CLEAR = 258, (A|a)(B|b)(S|s)

EXIT = 259, sqrt LIST = 260, (S|s)(Q|q)(R|r)(T|t) ERASE = 261, DEC = 262, %% HEX = 263, int i ; OCT = 264, BIGINT val; HELP = 265, NUM = 266, [\ REG = 267, {decnum} ADD = 268, {sscanf(yytext,\ SUB = 269, val);return(NUM); } MUL = 270, {octnum} { DIV = 271, i=1;val=0; MOD = 272, LSHIFT = 273, while(i

SIN = 284, {hexnum} { COS = 285, i=2;val=0; TAN = 286, ABS = 287, while(i

val=(val<<4)+yytext[i]-'a'+10; //前面定义过,此处省略宏定义 else #if ! defined (YYSTYPE) && ! if(isupper(yytext[i]))

defined

val=(val<<4)+yytext[i]-'A'+10; (YYSTYPE_IS_DECLARED) else typedef int YYSTYPE;

val=(val<<4)+yytext[i]-'0'; # define yystype YYSTYPE /* i++; obsolescent; will be withdrawn */ } # define yylval=val; YYSTYPE_IS_DECLARED 1 # define YYSTYPE_IS_TRIVIAL 1 return(NUM); #endif

} extern YYSTYPE yylval;

文件:calc.l {reg} { if(islower(yytext[0]))yylval核心程序

=yytext[0]-'a'; else yylval=yytext[0]-'A'; %{

#define YYSTYPE double #define BIGINT long return(REG); #include } #include \ %} {opt1} { digit [0-9] xdigit [0-9a-fA-F] switch(yytext[0]) odigit [0-7] { decnum case (0(\\.{digit}+)?)|([1-9]{digit}'+': *(\\.{digit}+)?) octnum0{odigit}+ return ADD; hexnum 0(x|X){xdigit}+ reg [a-zA-Z] break; opt1 case '-': \ %\\ return SUB; opt2 (&&)|(\\|\\|)|(\\<\\<)|(\\>\\>) break; exit case ((E|e)(X|x)(I|i)(T|t))|((Q|q)('*': U|u)(I|i)(T|t)) clear return MUL; (C|c)(L|l)(E|e)(A|a)(R|r) list (L|l)(I|i)(S|s)(T|t) break; erase case (E|e)(R|r)(A|a)(S|s)(E|e) '/': hex (H|h)(E|e)(X|x) oct (O|o)(C|c)(T|t) return DIV; dec (D|d)(E|e)(C|c) help (H|h)(E|e)(L|l)(P|p) sin break; (S|s)(I|i)(N|n)

case '%': {hex} {return(HEX);} return MOD; {oct} {return(OCT);} {dec} {return(DEC);} break; {help} {return(HELP);} case '^': .|\\n

{return(yytext[0]);}

return BITXOR; 文件:y.tab.c break; case '&': 部分代码:

return BITAND; #define YYBISON 1 /* Skeleton name. */

break; #define YYSKELETON_NAME case \

'|': /* Pure parsers. */ #define YYPURE 0 return BITOR;

/* Using locations. */

break; #define YYLSP_NEEDED 0 case /* Tokens. */

'<': #ifndef YYTOKENTYPE # define YYTOKENTYPE

return LESS; /* Put the tokens into the symbol table, so that GDB and other break; debuggers

case know about them. */ '>': enum yytokentype {

前面定义过,此处省略 return MORE; }; break;

case /* Copy the first part of user '!': declarations. */ #line 1 \y\

return NOT; //前面定义过,此处省略宏定义

break; #include case #include '~': #include #include

return BITREV; #include \lex.yy.c\ //这是词法分 析器生成的文件,必须包含! break; YYSTYPE reg[26]={0}; } BIGINT ival;

} enum Display{DEC_ON, HEX_ON,

OCT_ON};

{opt2} { enum Display dflag=DEC_ON; switch(yytext[0]) int i;

{ /* Enabling traces. */ case #ifndef YYDEBUG '&': # define YYDEBUG 0 #endif return AND;

/* Enabling verbose error messages. break; */

case #ifdef YYERROR_VERBOSE '|': # undef YYERROR_VERBOSE # define YYERROR_VERBOSE 1 return OR; #else

# define YYERROR_VERBOSE 0 break; #endif case

'<': #if ! defined (YYSTYPE) && ! defined

return LSHIFT; (YYSTYPE_IS_DECLARED) typedef int YYSTYPE;

break; # define yystype YYSTYPE /* case obsolescent; will be withdrawn */ '>': # define YYSTYPE_IS_DECLARED 1 return RSHIFT; # define YYSTYPE_IS_TRIVIAL 1 #endif

break; /* Copy the second part of user } declarations. */

/* Line 214 of yacc.c. */ #line 173 \

} #if ! defined (yyoverflow) ||

YYERROR_VERBOSE {sin} {return (SIN);}

{cos} {return (COS);} /* The parser invokes alloca or {tan} {return (TAN);} malloc; define the necessary {log} {return (LOG);} symbols. */ {abs} {return (ABS);}

{sqrt} {return (SQRT);}# if YYSTACK_USE_ALLOCA

# define YYSTACK_ALLOC {clear} {return(CLEAR);} alloca {exit} {return(EXIT);} # else {list} {return(LIST);} # ifndef {erase} {return(ERASE);}

YYSTACK_USE_ALLOCA

# 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 GCC's `empty if-body' warning. */

# define YYSTACK_FREE(Ptr) do { /* empty */; } while (0) # else

# if defined (__STDC__) || 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 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. */

# 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

# define YYCOPY(To, From, Count) \\ do \\ { \\ register YYSIZE_T yyi; \\ for (yyi = 0; yyi < (Count); yyi++) \\ (To)[yyi] = (From)[yyi]; \\ } \\ while (0) # endif # endif /* Relocate STACK from its old location to the new one. The

local variables YYSIZE and YYSTACKSIZE give the old and new number of

elements in the stack, and YYPTR gives the new location of the

stack. Advance YYPTR to a properly aligned location for the next

stack. */ # define YYSTACK_RELOCATE(Stack) \\ do { \\ YYSIZE_T yynewbytes; YYCOPY (&yyptr->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 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

/* YYNRULES -- 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 ? yytranslate[YYX] : YYUNDEFTOK)

/* YYTRANSLATE[YYLEX] -- 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[] = { 省略数据信息 };

/* YYRLINE[YYN] -- source line where rule number YYN was defined. */

static const unsigned char yyrline[] =

{省略数据信息 }; #endif #if YYDEBUG || YYERROR_VERBOSE

/* YYTNME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.

First, the terminals, then, starting at YYNTOKENS, nonterminals. */

static const char *const yytname[] = {

\\\\\\\

\\\\\\

\\\\\\\\AND\ \\\\\\AN\\

\\ \}; #endif

# ifdef YYPRINT

/* YYTOKNUM[YYLEX-NUM] -- Internal token number corresponding to

token YYLEX-NUM. */

static const unsigned short yytoknum[] = {

省略数据信息 }; # endif

/* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */

static const unsigned char yyr1[] = {

省略数据信息 };

/* YYDEFACT[STATE-NAME] -- Default rule to reduce with in state STATE-NUM when YYTABLE doesn't specify something else to do. Zero

means the default is an error. */

static const unsigned char yydefact[] =

{ 省略数据信息 };

/* YYDEFGOTO[NTERM-NUM]. */ static const yysigned_char yydefgoto[] = {省略数据};

/* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing

STATE-NUM. */

#define YYPACT_NINF -28 static const short yypact[] = {

省略数据信息 };

/* YYPGOTO[NTERM-NUM]. */ static const yysigned_char yypgoto[] =

{省略数据信息 }; /* YYTABLE[YYPACT[STATE-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 -1

static const unsigned char yytable[] = {

省略数据信息 };

#if ! 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

#if ! defined (YYSIZE_T)

# define YYSIZE_T unsigned int #endif

#define yyerrok (yyerrstatus = 0)

#define yyclearin (yychar = YYEMPTY)

#define YYEMPTY (-2) #define YYEOF 0

#define YYACCEPT goto yyacceptlab

#define YYABORT goto yyabortlab

#define YYERROR goto yyerrlab1

/* Like YYERROR except do call yyerror. This remains 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 YYFAIL goto yyerrlab

#define YYRECOVERING() (!!yyerrstatus)

#define YYBACKUP(Token, Value) \\ do \\ if (yychar == YYEMPTY && yylen == 1) \\ { \\ yychar = (Token); \\

yylval = (Value); \\

yytoken = YYTRANSLATE (yychar); \\

YYPOPSTACK; \\

goto yybackup;

搜索更多关于: 编译原理实现一个简单的计算器程序 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

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

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com