编译原理课程设计简单计算器实现
c简单计算器课程设计

c 简单计算器课程设计一、课程目标知识目标:1. 让学生掌握简单计算器的基本操作,包括开机、关机、清除屏幕、输入数字和运算符号。
2. 使学生理解加、减、乘、除四则运算的基本概念,并能够在简单计算器上进行正确的运算操作。
3. 帮助学生识别计算器上各个按键的功能,了解其对应的数学意义。
技能目标:1. 培养学生运用简单计算器解决实际问题的能力,如购物找零、计算面积等。
2. 提高学生进行四则运算的速度和准确性,提高计算效率。
3. 引导学生掌握计算器使用技巧,如连续计算、顺序计算等。
情感态度价值观目标:1. 培养学生对计算器的正确态度,认识到计算器是学习数学的工具,而非替代品。
2. 激发学生学习数学的兴趣,让学生在轻松愉快的氛围中体验数学计算的乐趣。
3. 培养学生的合作意识,使学生在小组讨论和互助学习中提高沟通能力,培养团队精神。
本课程针对小学三年级学生设计,结合学生年龄特点,注重激发兴趣和培养动手操作能力。
课程内容紧密联系教材,将目标分解为具体的学习成果,便于后续教学设计和评估。
通过本课程的学习,学生将能够熟练运用简单计算器,提高数学计算能力,培养良好的学习习惯和团队协作能力。
二、教学内容1. 计算器的基本结构:引导学生认识计算器的外观、各部分名称及其功能。
- 教材章节:第三章第1节《认识计算器》- 内容列举:计算器按键布局、显示屏、开机与关机、清除键等。
2. 四则运算操作:讲解加、减、乘、除四则运算在计算器上的操作方法。
- 教材章节:第三章第2节《计算器的四则运算》- 内容列举:按键输入、运算顺序、连续运算、计算结果读取等。
3. 计算器使用技巧:教授计算器的高级使用技巧,提高计算效率。
- 教材章节:第三章第3节《计算器使用技巧》- 内容列举:百分比计算、乘方运算、记忆功能等。
4. 实践应用:结合实际生活场景,让学生运用计算器解决实际问题。
- 教材章节:第三章第4节《计算器在实际生活中的应用》- 内容列举:购物找零、计算面积、体积、速度等。
简易计算器设计的课程设计

简易计算器设计的课程设计一、课程目标知识目标:1. 学生能够理解简易计算器的基本原理,掌握其设计流程。
2. 学生能够运用所学编程知识,实现简易计算器的功能。
3. 学生能够了解并掌握计算器界面设计的基本原则。
技能目标:1. 学生能够运用所学的编程技能,独立完成简易计算器的设计与开发。
2. 学生能够通过编写代码,实现计算器的加、减、乘、除等基本运算功能。
3. 学生能够运用调试工具,对计算器程序进行测试和优化。
情感态度价值观目标:1. 学生在课程学习中,培养对编程的兴趣和热情,增强信息素养。
2. 学生通过团队协作,培养沟通能力和团队精神,学会共同解决问题。
3. 学生在设计与开发简易计算器的过程中,体验创新实践的过程,培养创新思维和动手能力。
课程性质:本课程为信息技术课程,旨在帮助学生将所学的编程知识运用到实际项目中,提高学生的编程实践能力和创新能力。
学生特点:六年级学生具备一定的编程基础,对新鲜事物充满好奇心,喜欢动手实践。
教学要求:结合学生特点和课程性质,注重理论与实践相结合,强调动手实践,培养创新思维。
在教学过程中,关注学生的个体差异,鼓励学生相互交流、合作,共同完成学习任务。
通过本课程的学习,使学生能够将所学知识运用到实际项目中,提高解决问题的能力。
二、教学内容1. 计算器原理:介绍计算器的基本工作原理,包括算术运算逻辑、数据存储与处理等。
教材章节:第三章 计算机硬件基础2. 编程语言基础:回顾所学的编程语言(如Python),重点掌握变量定义、数据类型、运算符、条件语句和循环语句等。
教材章节:第二章 编程语言基础3. 界面设计:讲解计算器界面设计的原则和技巧,如布局、颜色、字体等。
教材章节:第四章 用户界面设计4. 编程实现计算器功能:引导学生运用所学知识,编写程序实现计算器的加、减、乘、除等基本运算功能。
教材章节:第五章 算法与程序设计5. 程序调试与优化:教授学生如何使用调试工具,对计算器程序进行测试、调试和优化。
C语言实现简单的计算器(加、减、乘、除)

C语⾔实现简单的计算器(加、减、乘、除) 利⽤运算符做为swich case 语句条件,实现简单程序的编写;并且对输⼊的运算做判断,除数为零也需做判断;1 #include<stdio.h>23int add(int a, int b);4int sub(int a, int b);5int mul(int a, int b);6int div(int a, int b);789int main(int argc, const char *argv[])10 {11/*12 * 定义接收变量,运算字符;13*/14int a,b,d;15char ch;1617/*18 * 赋初值19*/20 a = 0;21 b = 0;22 ch = '0';232425/*26 * 控制台输⼊运算变量,运算符;27*/28 printf("请输⼊需要计算的数及运算符;格式如:'num1 +-x/ num2' \n");29 scanf("%d%c%d",&a,&ch,&b);3031/*32 * 输⼊数据判断33*/34while((ch != '+')&&(ch != '-')&&(ch != '*')&&(ch != '/'))35 {36//fflush(stdin);此种清空缓冲区⽆效37 setbuf(stdin,NULL);38 printf("输⼊运算符有误,请重新输⼊:\n");39 scanf("%c",&ch);40 }4142/*43 * 除数为0做判断44*/45if((ch == '/')&&(b==0))46 {47 setbuf(stdin,NULL);48 printf("除数不能为0,请重新输⼊:\n");49 scanf("%d",&b);50 }515253/*54 * 识别不同运算字符,做不同的运算;55*/56switch(ch)57 {58case'+':59 printf("%d+%d=%d\n",a,b,add(a,b));60break;61case'-':62 printf("%d-%d=%d\n",a,b,sub(a,b));63break;64case'*':65 printf("%d*%d=%d\n",a,b,mul(a,b));66break;67case'/':68 printf("%d/%d=%d\n",a,b,div(a,b));69break;70default:71 printf("Error\n");72 }7374return0;75 }7677/*78* 定义运算函数79*/80int add(int a, int b) 81 {82return a+b;83 }8485int sub(int a, int b) 86 {87return a-b;88 }8990int mul(int a, int b) 91 {92return a*b;93 }9495int div(int a, int b) 96 {97return a/b;98 }。
(完整word版)计算器的设计与实现 编译原理实验报告 (2)(word文档良心出品)

实验报告
专业软件工程
课程名称编译原理
学号
姓名
辅导教师张静成绩
实验日期
2015.5.19
实验时间
14:00~18:00
1实验名称计算器的设计与实现
2、实验目的
掌握自上而下语法分析方法、自下而上语法分析方法
3、实验要求
(1)实验内容
设计及实现计算表达式的计算器。
表达式中可包含+、-、*、/、(、)等运算符。
}op;
char ch;
int i = 0,t = 0;
op.top = -1;
ch = str[i];
i++;
while(ch != '\0')
{
switch(ch)
{
case '(':
op.top++;op.data[op.top]=ch;
break;
case ')':
while(op.data[op.top] != '(')
scanf("%c",&ch);
if(ch=='Y' || ch=='y')
{
gets(str);
}
else
{
break;
}
}
//system("pause");
return 0;
}
7、测试及结果
1、加减法运算
2、乘除法运算
3、带‘(’‘)’运算
4、四则运算
5、容错处理
8、心得
本次课程设计是实现一个简易的计算器,这次课程设计采用逆波兰式的知识,这可以很好的表示简单算术表达式,其优点在于易于计算机处理表达式。本次设计使我进一步端正了我的学习态度,学会了实事求是,对自己要严格要求。我觉得动手做设计之前,头脑里必须清楚该怎么做,这一点是很重要的。就目前来说,我的动手能力虽然差一点,但我想,通过我的不懈努力,在这方面,我总会得到提高。在此次的课程设计中我最大的体会就是进一步认识到了理论联系实践的重要性。总而言之,此次课程设计让我学到了好多平时在课堂上学不到的东西,增加了我的知识运用能力,增强我的实际操作能力。我相信经过实践后的学习我可以将编译原理这门课程掌握的更好。
py课程设计计算器

py课程设计计算器一、教学目标本课程的学习目标包括以下三个方面:1.知识目标:学生需要掌握Python编程语言的基本语法和结构,理解计算器的基本原理和功能。
2.技能目标:学生能够运用Python编程语言编写简单的计算器程序,进行基本的数学运算,并能对程序进行调试和优化。
3.情感态度价值观目标:学生通过学习计算器程序的设计与实现,培养逻辑思维能力、创新意识和团队协作精神。
在教学过程中,我们将根据学生的实际情况,有针对性地进行教学,确保学生达到上述学习目标。
二、教学内容本课程的教学内容主要包括以下几个部分:1.Python编程语言的基本语法和结构:包括变量、数据类型、运算符、控制结构等。
2.计算器的基本原理和功能:包括加、减、乘、除等基本运算,以及科学计算、统计等功能。
3.Python编程语言在计算器程序设计中的应用:包括编写、调试和优化计算器程序。
教学内容将按照教材的章节进行安排,确保学生能够系统地学习和掌握相关知识。
三、教学方法为了提高教学效果,我们将采用以下几种教学方法:1.讲授法:通过讲解Python编程语言的基本语法和结构,使学生掌握相关知识。
2.案例分析法:通过分析计算器程序的案例,使学生了解计算器的基本原理和功能。
3.实验法:让学生动手编写、调试和优化计算器程序,提高学生的实际操作能力。
4.讨论法:在课堂上引导学生进行讨论,培养学生的逻辑思维能力和团队协作精神。
四、教学资源为了支持教学内容和教学方法的实施,我们将准备以下教学资源:1.教材:选用国内权威的Python编程语言教材,确保学生掌握正确的知识体系。
2.参考书:提供相关的计算器程序设计书籍,丰富学生的知识储备。
3.多媒体资料:制作精美的PPT,直观地展示计算器程序设计的过程和效果。
4.实验设备:提供计算机实验室,让学生进行实际操作和实验。
通过以上教学资源的支持,我们将努力提高教学质量,丰富学生的学习体验。
五、教学评估本课程的评估方式包括以下几个方面:1.平时表现:通过观察学生在课堂上的参与程度、提问回答等情况,评估学生的学习态度和积极性。
C语言简易计算器的实现

C语言简易计算器的实现C语言简易计算器是一种用于进行基本数学运算的程序。
实现一个简易计算器的关键是要能够解析用户输入的数学表达式,并将其转化为计算机可以理解的形式,然后进行计算,并输出结果。
下面是一个大约1200字以上的示例实现。
```c#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#include <ctype.h>#define MAX_SIZE 100//定义操作符的优先级int getPriority(char op)if (op == '+' , op == '-')return 1;else if (op == '*' , op == '/')return 2;elsereturn 0;//进行四则运算int calculate(int a, int b, char op)switch (op)case '+': return a + b;case '-': return a - b;case '*': return a * b;case '/': return a / b;default: return 0;}//将中缀表达式转换为后缀表达式void infixToPostfix(char* infixExp, char* postfixExp) char stack[MAX_SIZE];int top = -1;int j = 0;for (int i = 0; infixExp[i] != '\0'; i++)if (isdigit(infixExp[i])) { // 数字直接输出到后缀表达式while (isdigit(infixExp[i]))postfixExp[j++] = infixExp[i++];}postfixExp[j++] = ' ';i--;}else if (infixExp[i] == '(') { // 左括号压入栈stack[++top] = infixExp[i];}else if (infixExp[i] == ')') { // 右括号弹出栈内所有操作符并输出到后缀表达式,直到遇到左括号while (top != -1 && stack[top] != '(')postfixExp[j++] = stack[top--];postfixExp[j++] = ' ';}top--; // 弹出栈顶的左括号}else { // 操作符while (top != -1 && getPriority(stack[top]) >=getPriority(infixExp[i]))postfixExp[j++] = stack[top--];postfixExp[j++] = ' ';stack[++top] = infixExp[i];}}while (top != -1) { // 将栈内剩余操作符弹出并输出到后缀表达式postfixExp[j++] = stack[top--];postfixExp[j++] = ' ';}postfixExp[j] = '\0';//计算后缀表达式的值int evaluatePostfix(char* postfixExp)char stack[MAX_SIZE];int top = -1;for (int i = 0; postfixExp[i] != '\0'; i++)if (isdigit(postfixExp[i])) { // 数字压入栈int num = 0;while (isdigit(postfixExp[i]))num = num * 10 + (postfixExp[i++] - '0');stack[++top] = num;i--;}else if (postfixExp[i] == ' ')continue;}else { // 操作符,弹出栈顶的两个数进行计算,并将结果压入栈int b = stack[top--];int a = stack[top--];int result = calculate(a, b, postfixExp[i]);stack[++top] = result;}}return stack[top];int maichar infixExp[MAX_SIZE];printf("请输入中缀表达式:");fgets(infixExp, sizeof(infixExp), stdin); // 读取用户输入//将中缀表达式转换为后缀表达式char postfixExp[MAX_SIZE];infixToPostfix(infixExp, postfixExp);printf("后缀表达式为:%s\n", postfixExp);//计算后缀表达式的值并输出int result = evaluatePostfix(postfixExp);printf("计算结果为:%d\n", result);return 0;```这个简易计算器的实现基于栈的数据结构。
基于编译原理的简单计算器的设计与实现

基于编译原理的简单计算器的设计与实现
设计计算器的主要任务是定义运算符和相应的优先级,并为它
们编写按优先级计算表达式的算法。
以下是基于编译原理的计算器
的设计和实现步骤:
1. 词法分析:将用户输入的算术表达式分解成一个个词法单元(token),比如加号、数字、括号、减号等。
2. 语法分析:通过使用递归下降分析法,将词法单元按照特定
的规则进行组合,形成一个抽象语法树(AST),表示表达式的结构
和含义。
3. 语义分析:在AST的基础上进行语义分析,检查表达式中是
否存在语义错误,比如除数为零、变量未定义等。
4. 优化处理:对表达式进行优化处理,比如常量折叠、变量合并、代数简化等,以提高计算效率。
5. 生成中间代码:将AST转换成一种中间表示形式,比如三地
址码(three-address code),这种形式可以方便地生成计算机底
层代码。
6. 代码生成:根据中间代码生成目标机器的汇编代码或机器码,执行计算操作。
7. 输出结果:将计算结果显示给用户。
以上是基于编译原理的计算器的设计和实现步骤,其中重点是
语法分析和代码生成,这些步骤需要仔细考虑各种运算符的优先级
和结合性,以保证表达式的计算结果正确无误。
编译原理实现一个简单的计算器程序

《编译原理》实验报告专业:学号:班级:姓名:实验题目:设计,实现一个简单的计算器程序实验时间:指导教师:实验成绩:1实验目的1.1实现一个简单计算器2实验要求2.1单词的构词规则有明确的定义;2.2编写的分析程序能够正确识别源程序中的语法符号;2.3计算器源程序的以.c格式输出2.4对于源程序中的词法错误,能够做出简单的错误处理,给出简单的错误提示,保证顺利完成整个源程序的词法分析,语法分析;2.5计算器能够实现加,减,乘,除,混合运算,多项式运算。
3实验环境3.1Windows XP3.2Flex词法分析工具3.3Visual Studio C++ 6.04实验原理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 在V isual Studio C++6.0上运行生成的”.c”文件文件:y.ta.h#ifndef YYTOKENT YPE# define YYT OKENT YPEenum 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,AND = 275,OR = 276,NOT = 277,LESS = 278,MORE = 279,BIT AND = 280,BITOR = 281,BITXOR = 282,BITREV = 283,SIN = 284,COS = 285,T AN = 286,ABS = 287,SQRT = 288,LOG = 289,RMINUS = 290};//前面定义过,此处省略宏定义#if ! defined (YYST YPE) && ! defined(YYSTYPE_IS_DECLARED) typedef int YYST YPE;# define yystype YYSTYPE /* obsolescent; will be withdrawn */# define YYSTYPE_IS_DECLARED 1# define YYSTYPE_IS_TRIVIAL 1 #endifextern YYST YPE yylval;文件:calc.l 核心程序%{#define YYSTYPE double#define BIGINT long#include <ctype.h>#include "y.tab.h"%}digit [0-9]xdigit [0-9a-fA-F]odigit [0-7]decnum(0(\.{digit}+)?)|([1-9]{digit} *(\.{digit}+)?)octnum0{odigit}+hexnum 0(x|X){xdigit}+ reg [a-zA-Z]opt1"+"|"-"|"*"|"/"|"&"|"|"|" %"|"^"|"~"|"!"|"<"|">"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)(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]{ }{decnum}{sscanf(yytext,"%lf",&yylval);return(NUM); }{octnum} {i=1;v al=0;while(i<yyleng){val=(val<<3)+yytext[i]-'0';i++;}yylval=val;return(NUM);}{hexnum} {i=2;v al=0;while(i<yyleng){if(islower(yytext[i]))val=(val<<4)+yytext[i]-'a'+10;elseif(isupper(yytext[i]))val=(val<<4)+yytext[i]-'A'+10;elseval=(val<<4)+yytext[i]-'0';i++;}yylval=val;return(NUM);}{reg} {if(islower(yytext[0]))yylval=yytext[0]-'a';elseyylval=yytext[0]-'A';return(REG);}{opt1} {switch(yytext[0]){case'+':return ADD;break;case'-':return SUB;break;case'*':return MUL;break;case'/':return DIV;break;case'%':return MOD;break;case'^':return BITXOR;break;case'&':return BITAND;break;case'|':return BITOR;break;case'<':return LESS;break;case'>':return MORE;break;case'!':return NOT;break;case'~':return BITREV;break;}}{opt2} {switch(yytext[0]){case'&':return AND;break;case'|':return OR;break;case'<':return LSHIFT;break;case'>':return RSHIFT;break;}}{sin} {return (SIN);}{cos} {return (COS);}{tan} {return (TAN);}{log} {return (LOG);}{abs} {return (ABS);}{sqrt} {return (SQRT);}{clear} {return(CLEAR);}{exit} {return(EXIT);}{list} {return(LIST);}{erase} {return(ERASE);}{hex} {return(HEX);}{oct} {return(OCT);}{dec} {return(DEC);}{help} {return(HELP);}.|\n {return(yytex t[0]);}文件:y.tab.c部分代码:#define YYBISON 1/* Skeleton name. */#define YYSKELETON_NAME"yacc.c"/* Pure parsers. */#define YYPURE 0/* Using locations. */#define YYLSP_NEEDED 0/* T okens. */#ifndef YYTOKENT YPE# define YYT OKENT YPE/* Put the tokens into the symboltable, so that GDB and otherdebuggersknow about them. */enum yytokentype {前面定义过,此处省略};/* Copy the first part of userdeclarations. */#line 1 "calc.y"//前面定义过,此处省略宏定义#include <ctype.h>#include <stdio.h>#include <stdlib.h>#include <math.h>#include "lex.yy.c" //这是词法分析器生成的文件,必须包含!YYSTYPE reg[26]={0};BIGINT ival;enum Display{DEC_ON, HEX_ON,OCT_ON};enum Display dflag=DEC_ON;int i;/* Enabling traces. */#ifndef YYDEBUG# define YYDEBUG 0#endif/* Enabling verbose error messages.*/#ifdef YYERROR_VERBOSE# undef YYERROR_V ERBOSE# define YYERROR_V ERBOSE 1#else# define YYERROR_V ERBOSE 0#endif#if ! defined (YYST YPE) && !defined(YYSTYPE_IS_DECLARED)typedef int YYST YPE;# define yystype YYSTYPE /*obsolescent; will be withdrawn */# defineYYSTYPE_IS_DECLARED 1# define YYSTYPE_IS_TRIVIAL 1#endif/* Copy the second part of userdeclarations. *//* Line 214 of yacc.c. */#line 173 "y.tab.c"#if ! defined (yyoverflow) ||YYERROR_V ERBOSE/* The parser invokes alloca ormalloc; define the necessarysymbols. */# if YYST ACK_USE_ALLOCA# define YYST ACK_ALLOCalloca# else# ifndefYYST ACK_USE_ALLOCA# if defined (alloca) || defined (_ALLOCA_H)# define YYST ACK_ALLOC alloca# else# ifdef __GNUC__# define YYST ACK_ALLOC __builtin_alloca# endif# endif# endif# endif# ifdef YYST ACK_ALLOC/* Pacify GCC's `empty if-body' warning. */# define YYST ACK_FREE(Ptr) do { /* empty */; } while (0)# else# if defined (__STDC__) || defined (__cplusplus)# include <stdlib.h> /* INFRINGES ON USER NAME SPACE */# define YYSIZE_T size_t# endif# define YYST ACK_ALLOC malloc# define YYST ACK_FREE free # endif#endif /* ! defined (yyoverflow) || YYERROR_V ERBOSE */#if (! defined (yyoverflow) \&& (! defined (__cplusplus) \||(YYSTYPE_IS_TRIVIAL)))/* A type that is properly aligned for any stack member. */union yyalloc{short yyss;YYSTYPE yyvs;};/* T he size of the maximum gap between one aligned stack and the next. */# define YYST ACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1)/* The size of an array large to enough to hold all stacks, each with N elements. */# define YYST ACK_BYT ES(N) \ ((N) * (sizeof (short) + sizeof (YYSTYPE))\+YYST ACK_GAP_MAXIMUM)/* Copy COUNT objects from FROM to T O. T he source and destination donot overlap. */# ifndef YYCOPY# if 1 < __GNUC__# define YYCOPY(T o, From, Count) \__builtin_memcpy (T o, From, (Count) * sizeof (*(From))) # else# define YYCOPY(T o, From, Count) \do\{\register YYSIZ E_T yyi;\for (yyi = 0; yyi < (Count); yyi++) \(T o)[yyi] = (From)[yyi];\}\while (0)# endif# endif /* Relocate ST ACK from its oldlocation to the new one. Thelocal variables YYSIZE andYYST ACKSIZE give the old andnew number ofelements in the stack, andYYPT R gives the new location ofthestack. Advance YYPTR to aproperly aligned location for thenextstack. */# defineYYST ACK_RELOCATE(Stack)\do{\YYSIZE_T yynewbytes;YYCOPY (&yyptr->Stack,Stack, yysize);Stack = &yyptr->Stack;\yynewbytes = yystacksize *sizeof (*Stack) +YYST ACK_GAP_MAXIMUM; \yyptr += yynewbytes /sizeof (*yyptr);\}\while (0)#endif#if defined (__ST DC__) || defined(__cplusplus)typedef signed charyysigned_char;#elsetypedef short yysigned_char;#endif/* YYFINAL -- State number of thetermination state. */#define YYFINAL 2/* YYLAST -- Last index inYYT ABLE. */#define YYLAST 188/* YYNTOKENS -- Number ofterminals. */#define YYNT OKENS 40/* YYNNTS -- Number ofnonterminals. */#define YYNNTS 5/* YYNRULES -- Number of rules.*/#define YYNRULES 42/* YYNRULES -- Number of states.*/#define YYNST ATES 73/* YYTRANSLATE(YYLEX) --Bison symbol numbercorresponding to YYLEX. */#define YYUNDEFTOK 2#define YYMAXUT OK 290#define YYTRANSLAT E(YYX)\((unsigned int) (YYX) <=YYMAXUT OK ?yytranslate[YYX] :YYUNDEFTOK)/* YYTRANSLATE[YYLEX] --Bison symbol numbercorresponding to YYLEX. */static const unsigned charyytranslate[] ={省略数据信息};/* YYRHS -- A `-1'-separated list ofthe rules' RHS. */static const yysigned_char yyrhs[] ={省略数据信息};/* YYRLINE[YYN] -- source linewhere rule number YYN wasdefined. */static const unsigned char yyrline[]={省略数据信息};#endif#if YYDEBUG ||YYERROR_V ERBOSE/* YYT NME[SYMBOL-NUM] --String name of the symbolSYMBOL-NUM.First, the terminals, then,starting at YYNT OKENS,nonterminals. */static const char *const yytname[] ={"$end", "error", "$undefined","CLEAR", "EXIT", "LIST","ERASE", "DEC","HEX", "OCT", "HELP", "NUM","REG", "ADD", "SUB", "MUL","DIV", "MOD","LSHIFT", "RSHIFT", "AND","OR", "NOT", "LESS", "MORE","BIT AND","BITOR", "BITXOR","BITREV", "SIN", "COS", "T AN","ABS", "SQRT", "LOG","RMINUS", "'\\n'", "'='", "'('", "')'","$accept", "lines", "statement","expr", "cmdline", 0};#endif# ifdef YYPRINT/* YYT OKNUM[YYLEX-NUM] --Internal token numbercorresponding totoken YYLEX-NUM. */static const unsigned shortyytoknum[] ={省略数据信息};# endif/* YYR1[YYN] -- Symbol numberof symbol that rule YYN derives.*/static const unsigned char yyr1[] ={省略数据信息};/* YYDEF ACT[ST ATE-NAME] --Default rule to reduce with in stateST ATE-NUM when YYT ABLEdoesn't specify something else to do.Zeromeans the default is an error.*/static const unsigned char yydefact[]={ 省略数据信息};/* YYDEFGOT O[NT ERM-NUM].*/static const yysigned_charyydefgoto[] ={省略数据};/* YYPACT[ST AT E-NUM] -- Indexin YYT ABLE of the portiondescribingST ATE-NUM. */#define YYPACT_NINF -28static const short yypact[] ={省略数据信息};/* YYPGOTO[NTERM-NUM]. */static const yysigned_char yypgoto[]={省略数据信息};/*YYT ABLE[YYPACT[ST ATE-NUM]]. What to do in stateST ATE-NUM. Ifpositive, shift that token. Ifnegative, reduce the rule whichnumber is the opposite. If zero,do what YYDEF ACT says.If YYT ABLE_NINF, syntaxerror. */#define YYT ABLE_NINF -1static const unsigned char yytable[]={省略数据信息};#if ! defined (YYSIZ E_T) &&defined (__SIZE_TYPE__)# define YYSIZ E_T__SIZE_TYPE__#endif#if ! defined (YYSIZ E_T) &&defined (size_t)# define YYSIZE_T size_t#endif#if ! defined (YYSIZE_T)# if defined (__STDC__) || defined(__cplusplus)# include <stddef.h> /*INFRINGES ON USER NAMESPACE */# define YYSIZ E_T size_t# endif#endif#if ! defined (YYSIZE_T)# define YYSIZE_T unsigned int#endif#define yyerrok (yyerrstatus= 0)#define yyclearin (yychar =YYEMPT Y)#define YYEMPTY (-2)#define YYEOF 0#define YYACCEPT gotoyyacceptlab#define YYABORT gotoyyabortlab#define YYERROR gotoyyerrlab1/* Like YYERROR except do callyyerror. T his remains heretemporarilyto ease the transition to the newmeaning of YYERROR, for GCC.Once GCC version 2 hassupplanted version 1, this can go.*/#define YYFAIL gotoyyerrlab#define YYRECOVERING()(!!yyerrstatus)#define YYBACKUP(T oken, V alue)\do\if (yychar == YYEMPT Y &&yylen == 1)\{\yychar = (T oken);\yylval = (V alue);\yytoken = YYTRANSLATE(yychar);\YYPOPST ACK;\goto yybackup;\ }\ else \{ \yyerror ("syntax error: cannot back up");\YYERROR;}\ while (0)#define YYTERROR 1#define YYERRCODE 256/* YYLLOC_DEF AULT -- Compute the default location (before the actionsare run). */#ifndef YYLLOC_DEF AULT# define YYLLOC_DEF AULT(Current, Rhs, N) \Current.first_line = Rhs[1].first_line; \ Current.first_column = Rhs[1].first_column; \ st_line = Rhs[N].last_line; \ st_column = Rhs[N].last_column;#endif/* YYLEX -- calling `yylex' with the right arguments. */#ifdef YYLEX_PARAM# define YYLEX yylex (YYLEX_PARAM)#else# define YYLEX yylex ()#endif/* Enable debugging if requested. */#if YYDEBUG# ifndef YYFPRINTF# include <stdio.h> /* 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 YYDSYMPRINTF(T itle, T oken, V alue, Location) \do {\if (yydebug)\ {\YYFPRINTF (stderr, "%s ", Title); \yysymprint (stderr,\T oken, V alue);\YYFPRINTF (stderr, "\n");\}\} while (0)/*------------------------------------------------------------------.| yy_stack_print -- Print the statestack from its BOTTOM up to its || TOP (cinluded).|`------------------------------------------------------------------*/#if defined (__ST DC__) || defined(__cplusplus)static voidyy_stack_print (short *bottom, short*top)#elsestatic voidyy_stack_print (bottom, top)short *bottom;short *top;#endif{YYFPRINTF (stderr, "Stacknow");for (/* Nothing. */; bottom <= top;++bottom)YYFPRINTF (stderr, " %d",*bottom);YYFPRINTF (stderr, "\n");}# defineYY_ST ACK_PRINT(Bottom, T op)\do {\if (yydebug)\yy_stack_print ((Bottom),(T op)); \} while (0)#if defined (__ST DC__) || defined(__cplusplus)static voidyy_reduce_print (int yyrule)#elsestatic voidyy_reduce_print (yyrule)int yyrule;#endif{int yyi;unsigned int yylno =yyrline[yyrule];YYFPRINTF (stderr, "Reducingstack by rule %d (line %u), ",yyrule - 1, yylno);/* Print the symbols beingreduced, and their result. */for (yyi = yyprhs[yyrule]; 0 <=yyrhs[yyi]; yyi++)YYFPRINTF (stderr, "%s ",yytname [yyrhs[yyi]]);YYFPRINTF (stderr, "-> %s\n",yytname [yyr1[yyrule]]);}# defineYY_REDUCE_PRINT(Rule)\do {\if (yydebug)\yy_reduce_print (Rule);\} while (0)/* Nonzero means print parse trace.It is left uninitialized so thatmultiple parsers can coexist. */int yydebug;#else /* !YYDEBUG */# define YYDPRINTF(Args)# define YYDSYMPRINT(Args)# define YYDSYMPRINTF(T itle,T oken, V alue, Location)# defineYY_ST ACK_PRINT(Bottom, T op)# defineYY_REDUCE_PRINT(Rule)#endif /* !YYDEBUG *//* YYINIT DEPTH -- initial size ofthe parser's stacks. */#ifndef YYINIT DEPTH# define YYINIT DEPTH 200#endif/* YYMAX DEPT H -- maximumsize the stacks can grow to(effective onlyif the built-in stack extensionmethod is used).Do not make this value too large;the results are undefined ifSIZE_MAX <YYST ACK_BYT ES(YYMAXDEPTH)evaluated with infinite-precisioninteger arithmetic. */#if YYMAX DEPT H == 0# undef YYMAX DEPTH#endif#ifndef YYMAXDEPTH# define YYMAX DEPTH 10000#endif#if YYERROR_VERBOSE# ifndef yystrlen# if defined (__GLIBC__) &&defined (_STRING_H)# define yystrlen strlen# else/* Return the length of YYSTR. */static YYSIZE_T# if defined (__STDC__) ||defined (__cplusplus)yystrlen (const char *yystr)# elseyystrlen (yystr)const char *yystr;# endif{register const char *yys = yystr;while (*yys++ != '\0')continue;return yys - yystr - 1;}# endif# endif# ifndef yystpcpy# if defined (__GLIBC__) &&defined (_STRING_H) && defined(_GNU_SOURCE)# define yystpcpy stpcpy# elsestatic char *# if defined (__STDC__) ||defined (__cplusplus)yystpcpy (char *yydest, const char*yysrc)# elseyystpcpy (yydest, yysrc)char *yydest;const char *yysrc;# endif{register char *yyd = yydest;register const char *yys = yysrc;while ((*yyd++ = *yys++) != '\0')continue;return yyd - 1;}# endif# endif#endif /* !YYERROR_VERBOSE*/#if YYDEBUG#if defined (__ST DC__) || defined(__cplusplus)static voidyysymprint (FILE *yyoutput, intyytype, YYSTYPE *yyvaluep)#elsestatic voidyysymprint (yyoutput, yytype,yyvaluep)FILE *yyoutput;int yytype;YYSTYPE *yyvaluep;#endif{/* Pacify ``unused variable''warnings. */(void) yyvaluep;if (yytype < YYNTOKENS){YYFPRINTF (yyoutput,"token %s (", yytname[yytype]);# ifdef YYPRINTYYPRINT (yyoutput,yytoknum[yytype], *yyvaluep);# endif}elseYYFPRINTF (yyoutput,"nterm %s (", yytname[yytype]);switch (yytype){default:break;}YYFPRINTF (yyoutput, ")");}#endif /* ! YYDEBUG *//*-----------------------------------------------.| Release the memory associated tothis symbol. |`-----------------------------------------------*/#if defined (__ST DC__) || defined(__cplusplus)static voidyydestruct (int yytype, YYSTYPE*yyvaluep)#elsestatic voidyydestruct (yytype, yyvaluep)int yytype;YYSTYPE *yyvaluep;#endif{(void) yyvaluep;switch (yytype){default:break;}}#ifdef YYPARSE_PARAM# if defined (__STDC__) || defined(__cplusplus)int yyparse (void*YYPARSE_PARAM);# elseint yyparse ();# endif#else /* ! YYPARSE_PARAM */#if defined (__ST DC__) || defined(__cplusplus)int yyparse (void);#elseint yyparse ();#endif#endif /* ! YYPARSE_PARAM *//* T he lookahead symbol. */int yychar;/* T he semantic value of thelookahead symbol. */YYSTYPE yylval;/* Number of syntax errors so far.*/int yynerrs;/*----------.| yyparse. |`----------*/#ifdef YYPARSE_PARAM# if defined (__STDC__) || defined (__cplusplus)int yyparse (void *YYPARSE_PARAM)# elseint yyparse (YYPARSE_PARAM) void *YYPARSE_PARAM;# endif#else /* ! YYPARSE_PARAM */#if defined (__ST DC__) || defined (__cplusplus)intyyparse (void)#elseintyyparse ()#endif#endif{register int yystate;register int yyn;int yyresult;/* Number of tokens to shift before error messages enabled. */ int yyerrstatus;/* Lookahead token as an internal (translated) token number. */int yytoken = 0;/* Three stacks and their tools:`yyss': related to states,`yyvs': related to semantic values,`yyls': related to locations.Refer to the stacks thru separate pointers, to allow yyoverflowto reallocate them elsewhere. *//* The state stack. */short yyssa[YYINIT DEPT H]; short *yyss = yyssa;register short *yyssp;/* The semantic value stack. */ YYSTYPEyyvsa[YYINIT DEPT H]; YYSTYPE *yyvs = yyvsa; register YYST YPE *yyvsp;#define YYPOPST ACK (yyvsp--, yyssp--)YYSIZ E_T yystacksize = YYINIT DEPTH;/* The variables used to return semantic value and location from theaction routines. */ YYSTYPE yyval;/* When reducing, the number of symbols on the RHS of the reduced rule. */int yylen;YYDPRINTF ((stderr, "Starting parse\n"));yystate = 0;yyerrstatus = 0;yynerrs = 0;yychar = YYEMPT Y;yyssp = yyss;yyvsp = yyvs;goto yysetstate; yynewstate:/* In all cases, when you get here,the value and location stackshave just been pushed. sopushing a state here evens thestacks.*/yyssp++;yysetstate:*yyssp = yystate;if (yyss + yystacksize - 1 <=yyssp){/* Get the current used sizeof the three stacks, in elements. */YYSIZE_T yysize = yyssp -yyss + 1;#ifdef yyoverflow{/* Give user a chance toreallocate the stack. Use copies ofthese so that the &'sdon't force the real ones intomemory. */YYSTYPE *yyvs1 = yyvs;short *yyss1 = yyss;/* Each stack pointeraddress is followed by the size ofthedata in use in that stack,in bytes. This used to be aconditional around justthe two extra args, but that mightbe undefined ifyyoverflow is a macro. */yyoverflow ("parser stackoverflow",&yyss1, yysize* sizeof (*yyssp),&yyvs1, yysize* sizeof (*yyvsp),&yystacksize);yyss = yyss1;yyvs = yyvs1;}#else /* no yyoverflow */# ifndef YYST ACK_RELOCATEgoto yyoverflowlab;# else/* Extend the stack our ownway. */if (YYMAXDEPT H <=yystacksize)goto yyoverflowlab;yystacksize *= 2;if (YYMAX DEPT H <yystacksize)yystacksize =YYMAXDEPTH;{short *yyss1 = yyss;union yyalloc *yyptr =(union yyalloc *)YYST ACK_ALLOC(YYST ACK_BYT ES(yystacksize));if (! yyptr)goto yyoverflowlab;YYST ACK_RELOCATE(yyss);YYST ACK_RELOCATE(yyvs);# undef YYST ACK_RELOCATEif (yyss1 != yyssa)YYST ACK_FREE(yyss1);}# endif#endif /* no yyoverflow */yyssp = yyss + yysize - 1;yyvsp = yyvs + yysize - 1;YYDPRINTF ((stderr,"Stack size increased to %lu\n",(unsigned longint) yystacksize));if (yyss + yystacksize - 1 <=yyssp)YY ABORT;}YYDPRINTF ((stderr, "Enteringstate %d\n", yystate));goto yybackup;yybackup:/* Do appropriate processing giventhe current state. *//* Read a lookahead token if weneed one and don't already have one.*//* yyresume: *//* First try to decide what to dowithout reference to lookaheadtoken. */yyn = yypact[yystate];if (yyn == YYPACT_NINF)goto yydefault;/* Not known => get a lookaheadtoken if don't already have one. *//* YYCHAR is either YYEMPT Yor YYEOF or a valid lookaheadsymbol. */if (yychar == YYEMPT Y){YYDPRINTF ((stderr,"Reading a token: "));yychar = YYLEX;}if (yychar <= YYEOF){yychar = yytoken = YYEOF;YYDPRINTF ((stderr, "Nowat end of input.\n"));}else{yytoken = YYTRANSLATE(yychar);YYDSYMPRINTF ("Nexttoken is", yytoken, &yylval,&yylloc);}/* If the proper action on seeingtoken YYT OKEN is to reduce or todetect an error, take thataction. */yyn += yytoken;if (yyn < 0 || YYLAST < yyn ||yycheck[yyn] != yytoken)goto yydefault;yyn = yytable[yyn];if (yyn <= 0){if (yyn == 0 || yyn ==YYT ABLE_NINF)goto yyerrlab;yyn = -yyn;goto yyreduce;}if (yyn == YYFINAL)YYACCEPT;/* Shift the lookahead token. */YYDPRINTF ((stderr, "Shiftingtoken %s, ", yytname[yytoken]));/* Discard the token being shiftedunless it is eof. */if (yychar != YYEOF)yychar = YYEMPTY;*++yyvsp = yylval;/* Count tokens shifted sinceerror; after three, turn off errorstatus. */if (yyerrstatus)yyerrstatus--;yystate = yyn;goto yynewstate;/*-----------------------------------------------------------.| yydefault -- do the default actionfor the current state. |`-----------------------------------------------------------*/yydefault:yyn = yydefact[yystate];if (yyn == 0)goto yyerrlab;goto yyreduce;/*-----------------------------.| yyreduce -- Do a reduction. |`-----------------------------*/yyreduce:/* yyn is the number of a rule toreduce with. */yylen = yyr2[yyn];/* If YYLEN is nonzero,implement the default value of theaction:`$$ = $1'.Otherwise, the following linesets YYV AL to garbage.This behavior isundocumented and Bisonusers should not rely upon it.Assigning to YYV ALunconditionally makes theparser a bit smaller, and it avoids aGCC warning that YYV ALmay be used uninitialized. */yyval = yyvsp[1-yylen];YY_REDUCE_PRINT (yyn);switch (yyn){case 3:#line 39 "calc.y"{printf("--> ");}break;case 4:#line 40 "calc.y"{yyerrok;printf("\n--> ");}break;case 7:#line 45 "calc.y"{if(dflag==DEC_ON)printf("\t=%g\n--> " , yyvsp[0] );elseif(dflag==HEX_ON)printf( "\t=0x%08X\n--> " , (BIGINT)yyvsp[0] );elseprintf( "\t=0%o\n--> " ,(BIGINT)yyvsp[0] );}break;case 8:#line 50 "calc.y"{i=(int)yyvsp[-2]; reg[i] = yyvsp[0] ;if(dflag==DEC_ON)printf( "\t%c=%g\n--> " ,'a'+i,reg[i] );else if(dflag==HEX_ON)printf( "\t%c=0x%08X\n--> " ,'a'+i,(BIGINT)reg[i] );else printf( "\t%c=0%o\n--> " ,'a'+i,(BIGINT)reg[i] );}break;case 9:#line 58 "calc.y"{ yyval = yyvsp[-2] + yyvsp[0] ; }break;case 10:#line 59 "calc.y"{ yyval = yyvsp[-2] - yyvsp[0] ; }break;case 11:#line 60 "calc.y"{ yyval = yyvsp[-2] * yyvsp[0] ; }break;case 12:#line 61 "calc.y"{ yyval = yyvsp[-2] / yyvsp[0] ; }break;case 13:#line 62 "calc.y"{ yyval = (BIGINT)yyvsp[-2] % (BIGINT)yyvsp[0] ; }break;case 14:#line 63 "calc.y"{ yyval = (BIGINT)yyvsp[-2] & (BIGINT)yyvsp[0] ; }break;case 15:#line 64 "calc.y"{ yyval = (BIGINT)yyvsp[-2] | (BIGINT)yyvsp[0] ; }break;case 16:#line 65 "calc.y"{ yyval = (BIGINT)yyvsp[-2] ^ (BIGINT)yyvsp[0] ; }break;case 17:#line 66 "calc.y"{ yyval = (BIGINT)yyvsp[-2] << (BIGINT)yyvsp[0] ; }break;case 18:#line 67 "calc.y"{ yyval = (BIGINT)yyvsp[-2] >> (BIGINT)yyvsp[0] ; }break;case 19:#line 68 "calc.y"{ yyval = (BIGINT)yyvsp[-2] && (BIGINT)yyvsp[0] ; }break;case 20:#line 69 "calc.y"{ yyval = (BIGINT)yyvsp[-2] || (BIGINT)yyvsp[0] ; }break;case 21:#line 70 "calc.y"{ yyval = yyvsp[-2]<yyvsp[0]?1:0; }break;case 22:#line 71 "calc.y"{ yyval = yyvsp[-2]>yyvsp[0]?1:0; }break;case 23:#line 72 "calc.y"{ yyval = yyvsp[-1] ; }break;case 24: #line 73 "calc.y"{ yyval = -yyvsp[0] ; }break;case 25:#line 74 "calc.y"{ yyval =~((BIGINT)yyvsp[0]); }break;case 26:#line 75 "calc.y"{ yyval= !( (BIGINT)yyvsp[0] ) ; }break;case 27:#line 76 "calc.y"{ yyval = yyvsp[0] ; }break;case 28:#line 77 "calc.y"{ ival=(int)yyvsp[0];yyval =reg[ival] ; }break;case 29:#line 78 "calc.y"{ yyval =sin(yyvsp[0]) ; }break;case 30:#line 79 "calc.y"{ yyval =cos(yyvsp[0]) ; }break;case 31:#line 80 "calc.y"{ yyval =tan(yyvsp[0]) ; }break;case 32:#line 81 "calc.y"{ yyval =log(yyvsp[0]) ; }break;case 33:#line 82 "calc.y"{ yyval =abs(yyvsp[0]) ; }break;case 34:#line 83 "calc.y"{ yyval =sqrt(yyvsp[0]) ; }break;case 35:#line 86 "calc.y"{ exit(0); }break;case 36:#line 87 "calc.y"{ printf("\033[2J\033[1;1H\n-->"); }break;case 37:#line 88 "calc.y"{ dflag=HEX_ON; printf(">>Hex display mod on!\n--> "); }break;case 38:#line 89 "calc.y"{ dflag=DEC_ON; printf(">>Dec display mod on!\n--> "); }break;case 39:#line 90 "calc.y"{ dflag=OCT_ON; printf(">>Oct display mod on!\n--> "); }break;case 40:#line 91 "calc.y"{for(i=0;i<26;i++){if(dflag==DEC_ON)printf("\t%c=%g\n",'a'+i,reg[i]);elseif(dflag==HEX_ON)printf("\t%c=0x%08X\n",'a'+i,(BIGINT)reg[i]);elseprintf("\t%c=0%o\n",'a'+i,(BIGINT)reg[i]);}printf("--> ");}break;case 41:#line 100 "calc.y"{ for(i=0;i<26;i++)reg[i]=0;printf(">>All registers erased!\n-->");}break;case 42:#line 101 "calc.y"{printf(">>COMMANDS:\n");printf(">help: Display this help section.\n");printf(">clear: Clear the screen.\n");printf(">dec: Decimal mode to displaynumbers or registers.\n");printf(">hex: Hexadecimal mode to displaynumbers or registers.\n");printf(">oct: Octal mode to display numbersor registers.\n");printf(">list: List the values in the 26registers which are ranged from'a'/'A' to 'z'/'Z'.\n");printf(">erase: Reset all registers to 0.\n");printf(">exit: Quit this program.\n");printf("-->");}break;}/* Line 999 of yacc.c. */#line 1349 "y.tab.c"yyvsp -= yylen;yyssp -= yylen;YY_ST ACK_PRINT (yyss,yyssp);*++yyvsp = yyval;/* Now `shift' the result of thereduction. Determine what statethat goes to, based on the statewe popped back to and the rulenumber reduced by. */yyn = yyr1[yyn];yystate = yypgoto[yyn -YYNT OKENS] + *yyssp;if (0 <= yystate && yystate <=YYLAST && yycheck[yystate] ==*yyssp)yystate = yytable[yystate];elseyystate = yydefgoto[yyn -YYNT OKENS];goto yynewstate;/*------------------------------------.| yyerrlab -- here on detecting error |`------------------------------------*/yyerrlab:/* If not already recovering froman error, report this error. */if (!yyerrstatus){++yynerrs;#if YYERROR_VERBOSEyyn = yypact[yystate];if (YYPACT_NINF < yyn&& yyn < YYLAST){YYSIZ E_T yysize = 0;int yytype =YYTRANSLAT E (yychar);const char* yyprefix;char *yymsg;int yyx;/* Start YYX at -YYN ifnegative to avoid negative indexesinYYCHECK. */int yyx begin = yyn < 0 ?-yyn : 0;/* Stay within bounds ofboth yycheck and yytname. */int yychecklim =YYLAST - yyn;int yyxend =yychecklim< YYNT OKENS ? yychecklim :YYNT OKENS;int yycount = 0;yyprefix = ", expecting ";for (yyx = yyx begin; yyx< yyxend; ++yyx)i f (yycheck[yyx + yyn]== yyx && yyx != YYTERROR){yysize += yystrlen(yyprefix) + yystrlen (yytname[yyx]);yycount += 1;if (yycount == 5){yysize = 0;break;}}yysize += (sizeof ("syntaxerror, unexpected ")+ yystrlen(yytname[yytype]));yymsg = (char *)YYST ACK_ALLOC (yysize);if (yymsg != 0){char *yyp =yystpcpy (yymsg, "syntax error,unex pected ");yyp = yystpcpy (yyp,yytname[yytype]);if (yycount < 5){yyprefix = ",expecting ";for (yyx =yyx begin; yyx < yyxend; ++yyx)i f(yycheck[yyx + yyn] == yyx &&yyx != YYTERROR){yyp =yystpcpy (yyp, yyprefix);yyp =yystpcpy (yyp, yytname[yyx]);yyprefix = "or ";}}yyerror (yymsg);YYST ACK_FREE(yymsg);}elseyyerror ("syntax error;also virtual memory exhausted");}else#endif /* YYERROR_V ERBOSE */yyerror ("syntax error");}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
课程设计报告
课程:
编译原理
学号:
姓名:
班级:
11级嵌入式应用技术班
教师:
时间:
2014年6月
计算机科学与技术系
输入一个简单运算表达式
A=当前输
入符号
A是运算符吗?
根据运算符的特点从栈顶部取出若干个运算对象进行该运算,将运果入栈
将该字符入栈A=‘\0’
程序结束
是
否
是
否图二表达式结果计算流程图
结果与分析(可以加页):
图三实现计算器加法功能
源代码:
#include<stdio.h> #include<stdlib.h> #define MaxSize 99
void translate(char str[],char exp[])
{
struct
{
char data[MaxSize];
int top;
}op;
char ch;
int i = 0,t = 0;
op.top = -1;
ch = str[i];
i++;
while(ch != '\0')
{
switch(ch)
{
case '(':
op.top++;op.data[op.top]=ch;
break;
case ')':
while(op.data[op.top] != '(')
{
exp[t]=op.data[op.top];
op.top--;
t++;
}
op.top--;
break;
case '+':
case '-':
while(op.top != -1&&op.data[op.top] != '(')
{
exp[t] = op.data[op.top];
op.top--;
t++;
}
op.top++;
op.data[op.top] = ch;
break;
case '*':
case '/':
while(op.data[op.top] == '/'||op.data[op.top] == '*')
{
exp[t] = op.data[op.top];
op.top--;
t++;
}
op.top++;
op.data[op.top] = ch;
break;
case ' ':
break;
default:
while(ch >= '0'&&ch <= '9')
{
exp[t] = ch;t++;
ch = str[i];i++;
}
i--;
exp[t] = ' ';
t++;
}
ch = str[i];
i++;
}
while(op.top != -1)
{
exp[t] = op.data[op.top];
t++;
op.top--;
}
exp[t] = '\0';
}
float cal_value(char exp[])
{
struct
{
float data[MaxSize];
int top;
}st;
float d;
char ch;
int t = 0;
st.top = -1;
ch = exp[t];
t++;
while(ch != '\0')
{
switch(ch)
{
case '+':
st.data[st.top-1] = st.data[st.top-1]+st.data[st.top];
st.top--;
break;
case '-':
st.data[st.top-1] = st.data[st.top-1]-st.data[st.top];
st.top--;
break;
case '*':
st.data[st.top-1] = st.data[st.top-1]*st.data[st.top];
st.top--;
break;
case '/':
if(st.data[st.top] != 0)
st.data[st.top-1]=st.data[st.top-1]/st.data[st.top];
else
{
printf("\n\terror");
}
st.top--;
break;
default:
d=0;
while(ch >= '0'&&ch <= '9')
{
d = 10*d+ch-'0';
ch = exp[t];
t++;
}
st.top++;
st.data[st.top] = d;
}
ch = exp[t];
t++;
}
return st.data[st.top];
}
int main()
{
char ch;
while(1)
{
char str[MaxSize],exp[MaxSize];
printf("请输入一个表达式:\n");
gets(str);
printf("原表达式:%s\n",str);
translate(str,exp);
printf("逆波兰式:%s\n",exp);
printf("计算机结果:%g\n",cal_value(exp));
printf("continue y or n?:");
scanf("%c",&ch);
if(ch=='Y' || ch=='y')
{
gets(str);
}
else
{
break;
}
}
//system("pause");
return 0;
}。