当前位置:首页 » 编程软件 » 编译原理计算器实现

编译原理计算器实现

发布时间: 2022-05-14 00:58:13

㈠ 简单计算器的编写

这是个支持表达式的计算器,代码很短。
可以输入类似(3+3*2)*5+6这样的表达式,自己看吧。
不懂其中的原理来找我,或者自助一下,学点编译原理的知识,一定要懂EBNF,EBNF不知道是什么东西?那就google一下吧。
/*
simple integer arithmetic calculator according to the EBNF
<exp> -> <term>{<addop><term>}
<addop>->+|-
<term>-><factor>{<mulop><factor>}
<mulop> -> *
<factor> -> ( <exp> )| Number
Input a line of text from stdin
Outputs "Error" or the result.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

char token;/*global token variable*/
/*function prototypes for recursive calls*/
int exp(void);
int term(void);
int factor(void);

void error(void)
{
fprintf(stderr,"Error\n");
exit(1);
}

void match(char expectedToken)
{
if(token==expectedToken)token=getchar();
else error();
}

main()
{
int result;
token = getchar();/*load token with first character for lookahead*/
result = exp();
if(token=='\n')/*check for end of line */
printf("Result = %d\n",result);
else error();/*extraneous cahrs on line*/
return 0;
}

int exp(void)
{
int temp = term();
while((token=='+')||(token=='-'))
switch(token)
{
case '+':
match('+');
temp+=term();
break;
case '-':
match('-');
temp-=term();
break;
}
return temp;
}

int term(void)
{
int temp = factor();
while (token=='*')
{
match('*');
temp*=factor();
}
return temp;
}

int factor(void)
{
int temp;
if(token=='('){
match('(');
temp = exp();
match(')');
}
else if(isdigit(token)){
ungetc(token,stdin);
scanf("%d",&temp);
token = getchar();
}
else error();
return temp;
}

㈡ 这个用C语言写的计算器的思路是什么

对初学编程者来说,这个程序的原理确实难了点,因为它用到了编译原理的知识.
即如果设一个四则运算表达式的形式为S,那么它一定是一个以等号结尾的运算式,即S->exp=,->是推导符号.
运算式exp又可以继续推导成
exp->exp+term|exp-term|term
exp表示加减运算,term表示乘除运算.这个推导式反映了乘除的优先级比加减高.
即要先计算乘除式的结果,再来加减.
term可以推导如下:
term->term*factor|term/factor|factor
factor->num|(E)
factor是数字或者一个被括号括住的运算式,表示最高优先级.
数字本身是不带运算的,是原子性的,肯定是最高优先级.
括号是被规定了优先计算.
这个程序的代码就是按照上面的推导式,用递归方式来分析运算式的.

java怎样实现计算器的连续运算;如1+2*3/3-4怎样用代码实现

如果只是实现的话,java有现成的函数
如果想自己写的话,我觉得用堆栈吧。

㈣ 表达式计算器的设计与实现 VB

Option Explicit

Public Function str18(Num As String) As Double
Dim i&, l&, lStart&
Dim sTemp$, dTemp#, dTemp2#
If Num = vbNullString Then Exit Function
lStart = InStr(1, Num, "(")
Do While lStart > 0
If lStart > 1 Then
If InStr(1, "(*+-/", Mid$(Num, lStart - 1, 1)) <= 0 Then GoTo 10
End If
sTemp = GetInners(Num, lStart + 1)
l = Len(sTemp)
sTemp = CStr(str18(sTemp))
Num = Mid$(Num, 1, lStart - 1) & sTemp & Mid$(Num, lStart + l + 2)
10: lStart = InStr(lStart + 1, Num, "(")
Loop
lStart = InStrRev(Num, "int(")
Do While lStart > 0
sTemp = GetInners(Num, lStart + 4)
l = Len(sTemp)
sTemp = CStr(Int(str18(sTemp)))
Num = Mid$(Num, 1, lStart - 1) & sTemp & Mid$(Num, lStart + l + 5)
lStart = InStrRev(Num, "int(", lStart)
Loop
lStart = InStr(2, Num, "+")
If lStart > 0 Then
dTemp = str18(Mid$(Num, lStart + 1))
dTemp2 = str18(Left$(Num, lStart - 1))
str18 = dTemp + dTemp2: Exit Function
End If
lStart = InStr(2, Num, "-")
If lStart > 0 Then
If InStr(1, "(*+-/", Mid$(Num, lStart - 1, 1)) <= 0 Then
dTemp = str18(Mid$(Num, lStart + 1))
dTemp2 = str18(Left$(Num, lStart - 1))
str18 = dTemp2 - dTemp: Exit Function
End If
End If
lStart = InStr(1, Num, "*")
If lStart > 0 Then
dTemp = str18(Mid$(Num, lStart + 1))
dTemp2 = str18(Left$(Num, lStart - 1))
str18 = dTemp * dTemp2: Exit Function
End If
lStart = InStr(1, Num, "/")
If lStart > 0 Then
dTemp = str18(Mid$(Num, lStart + 1))
'If dTemp = 0 Then Exit Function
dTemp2 = str18(Left$(Num, lStart - 1))
str18 = dTemp2 / dTemp: Exit Function
End If
lStart = InStr(1, Num, "^")
If lStart > 0 Then
dTemp = str18(Mid$(Num, lStart + 1))
dTemp2 = str18(Left$(Num, lStart - 1))
str18 = dTemp2 ^ dTemp: Exit Function
End If
If Left$(Num, 1) = "-" Then
str18 = -1 * Val(Mid$(Num, 2))
Else
str18 = Val(Num)
End If
End Function
Private Function GetInners(Str$, lStart&) As String
'获取()内容,str(lstart-1)="("
Dim i&, sTemp As String * 1, k%, l&
l = Len(Str)
For i = lStart To l
sTemp = Mid$(Str, i, 1)
Select Case sTemp
Case "(": k = k + 1
Case ")": k = k - 1
End Select
If k = -1 Then Exit For
Next
If i <= l Then GetInners = Mid$(Str, lStart, i - lStart)
End Function
最好写在模块中 哦~~~~~[注,来自vbgood 很久前的了 谁的我也忘了]

㈤ 编译原理语法分析编程

#include <iostream>
#include <string>
#include <fstream>
#include <queue>
#include <string.h>
#include <stdio.h>

using namespace std;

enum Datatype { RESERVE_WORD=1,IDENTIFIER=2,DIGIT=3,OPERATOR=4,SEPRATOR=5 };

struct OutputStruct
{
public:
Datatype type;
string value;
};

string operate[]={"sin","cos","pow"};
string KeyWord[]={"main","int","if","char","cout"};
const int MAX_SIZE=255;
char BUFF[MAX_SIZE]; //buffer to contain a char line.
ifstream inFile;
ofstream outFileStream;
queue<OutputStruct> tt;

bool IsKeyWord(string& cs)
{
for(int i=0;i<5;++i)
if(cs==KeyWord[i])
return true; //Exist
return false;
}

void ReadLineAndAnalyze()
{
int strSize=0;
int i;
int errFlag=0;
char ch;
string outStructStr,str;
struct OutputStruct outStruct;
{
i=0;
inFile.getline(BUFF,MAX_SIZE,'\n');
strSize=inFile.gcount();
cout<<BUFF;
do{
str="";
do{
ch=BUFF[i];
i++;
}while(ch==' '||ch==' '||ch=='\n');
switch(ch)
{

case '+':
case '-':
case '*':
case '/':
outStruct.type=OPERATOR;
outStruct.value=ch;
break;
case '=':
case '>':
case '<':
outStructStr=ch;
if(BUFF[i]=='=')
{
outStruct.type=OPERATOR;
outStructStr+=BUFF[i];
outStruct.value=outStructStr;
i++;
}
else
{
outStruct.type=OPERATOR;
outStruct.value=ch;
};
break;

case ',':
case ';':
case '{':
case '}':
case '(':
case ')':
case '[':
case ']':
case '\"':
outStruct.type=SEPRATOR;
outStruct.value=ch;
break;

case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
outStructStr+=ch;
while(BUFF[i]>='0'&&BUFF[i]<='9'||BUFF[i]=='.')
{
outStructStr+=BUFF[i];
i++;
}//while
outStruct.type=DIGIT;
outStruct.value=outStructStr;
break;

default:
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
{
outStructStr+=ch;
while(BUFF[i]>='a'&&BUFF[i]<='z'||BUFF[i]>='A'&&BUFF[i]<='Z')
{
outStructStr+=BUFF[i];
i++;
}//while
if(IsKeyWord(outStructStr))
{
outStruct.type=RESERVE_WORD;
outStruct.value=outStructStr;
}
else
{
outStruct.type=IDENTIFIER;
outStruct.value=outStructStr;
}
break;
}
else
errFlag=1;
}//switch;
if(!errFlag)
tt.push(outStruct);
errFlag=0;
outStructStr="";
}while(i<strSize-1);

}//while(i<MAX_SIZE&&!inFile.eof());//do_while
return;
}

float F();
float T();
float E();
float S();

float F()
{
float ret;
if((tt.front().type==IDENTIFIER)||(tt.front().type==DIGIT))
{
ret=atof(tt.front().value.c_str());
if(tt.empty())
{
cout<<"END"<<endl;exit(0);
}
tt.pop();
return ret;
}
if(tt.front().value=="(")
{
if(tt.empty())
{
cout<<"END"<<endl;exit(0);
}
tt.pop();
ret=E();
if(tt.front().value==")")
{
if(tt.empty())
{
cout<<"END"<<endl;exit(0);
}
tt.pop();
return ret;
}
else
{
cout<<"\b ----ERROR! "<<tt.front().value<<" 缺少右括号"<<endl;
cout<<"Press \"enter\" to modify the data file!";
getchar();
system("notepad data.txt");
exit(0);
}
}
else
{
cout<<"\b ----ERROR! "<<tt.front().value<<" 缺少因子"<<endl;
cout<<"Press \"enter\" to modify the data file!";
getchar();
system("notepad data.txt");
exit(0);
}
}
float T()
{
float i,j;
i=F();
if(tt.front().value=="*")
{
if(tt.empty())
{
cout<<"END"<<endl;exit(0);
}
tt.pop();
j=T();
return i*j;
}
else if(tt.front().value=="/")
{
if(tt.empty())
{
cout<<"END"<<endl;exit(0);
}
tt.pop();
j=T();
if(abs(j)<0.0000001)
{
cout<<"\b ----ERROR! 除数为零!"<<endl;
cout<<"Press \"enter\" to modify the data file!";
getchar();
system("notepad data.txt");
exit(0);
}
return i/j;
}
return i;
}

float E()
{
float i,j;
i=T();
if(tt.front().value=="+")
{
if(tt.empty())
{
cout<<"END"<<endl;exit(0);
}
tt.pop();
j=E();
i=i+j;
}
else if(tt.front().value=="-")
{
if(tt.empty())
{
cout<<"END"<<endl;exit(0);
}
tt.pop();
j=E();
i=i-j;
}
if(tt.front().value==";"||tt.front().type==OPERATOR||tt.front().value==")")
return i;
else
{
cout<<"\b ----ERROR! "<<tt.front().value<<" 缺少运算符"<<endl;
cout<<"Press \"enter\" to modify the data file!";
getchar();
system("notepad data.txt");
exit (0);
}
}

float S()
{
float i;
i=E();
if(tt.front().value==";")
{
if(tt.empty())
{
cout<<"END"<<endl;exit(0);
}
tt.pop();
return i;
}
cout<<"\b ----ERROR! "<<tt.front().value<<" 缺少左括号"<<endl;
cout<<"Press \"enter\" to modify the data file!";
getchar();
system("notepad data.txt");
exit(0);
}

void GrammaAnalize()
{
float i;
if(tt.empty())
{
cout<<"END"<<endl;exit(0);
}
i=S();
cout<<"\b="<<i<<endl;
}

int main()
{
inFile.open("data.txt");
if(!inFile)
{
cout<<"打开源文件失败!";
return 1;
}
while(!inFile.eof())
{
ReadLineAndAnalyze();
GrammaAnalize();
}
inFile.close();
return 0;
}

㈥ (高分)求一个正确lex和yacc编的计算器(编译原理)

网上多的是,大多可以运行。关键是你要在linux下分别将 lex和yacc文件用 flex 和 bison 编译成对应的c文件,然后在windows下用VC或者Codeblock的IDE下编写界面,连接刚才对应的c文件,生成exe交给老师。

㈦ 用C#做WinFrom的计算器,怎样实现计算器中的括号问题高手的进!高分悬赏!

电脑实际上就是一个计算器,你在编程的时候写一个表达式他都是可以帮你计算的,只不过你用了计算器去平均的表达式就成为了字符串,其实这个字符串程序也是可以帮你计算的就要用到动态编译了!以前做过一个动态添加公式来算物流运费的工具,代码找不到了

㈧ 用C语言编写计算器,怎么才能计算一串表达式啊,我该怎么编。在线等,我只学了些比本的结构,如分支,循环

计算一串的表达式需要用到语义分析那部分的知识,构建一颗表达式二叉树

比如要想计算2*(5+3)-6这个表达式的话,构建结果如图,这个需要数据结构里二叉树和编译原理里面的语义分析知识,如果你仅仅是学了分支循环的话,应该不会让你们编这么复杂的东西的....

恩..我觉得是这样的..

㈨ 用c++编写一个简单的计算器

你的这个要求绝对不会有人满足的。因为你的这个要求不只是一个编写一个最简单的计算器问题。
主要是在编写这个程序的过程中,还涉及到了计算机软件专业中的“编译原理”这门课程的其中重要知识。即:表达式的分析与求值(即:何时将相应的数字、以及运算符压入堆栈,何时又需要将相应的数字、以及运算符弹出堆栈)、以及对运算符优先级的处理(例如:括号的最优先最高、乘除法的优先级高于加减法)。
所以说你的这个要求可以说是:至少是一个大作业了。而且了,另外还有一个别的任何人无法满足你的原因就是:对于编写任何程序来说,都是必须要通过自己上机编写程序源代码、编译、链接、通过花费很多的时间和精力去调试,最终才能够得出程序的正确运行结果。

㈩ 编译原理 tiny.l怎么生成tiny.exe

最近对解释型程序(类似python或者是linux里的bc计算器)非常感兴趣,就开始学习一下编译原理。今天自己实现了TINY语言的词法扫描程序。大部分参考《编译原理及实践》一书。但是我做了一些小小的改进。 先说一下TINY语言:

热点内容
app什么情况下找不到服务器 发布:2025-05-12 15:46:25 浏览:714
php跳过if 发布:2025-05-12 15:34:29 浏览:467
不定时算法 发布:2025-05-12 15:30:16 浏览:131
c语言延时1ms程序 发布:2025-05-12 15:01:30 浏览:165
动物园灵长类动物配置什么植物 发布:2025-05-12 14:49:59 浏览:734
wifi密码设置什么好 发布:2025-05-12 14:49:17 浏览:148
三位数乘两位数速算法 发布:2025-05-12 13:05:48 浏览:397
暴风影音缓存在哪里 发布:2025-05-12 12:42:03 浏览:540
access数据库exe 发布:2025-05-12 12:39:04 浏览:628
五开的配置是什么 发布:2025-05-12 12:36:37 浏览:365