⑴ 编译原理问题--优先关系表怎么画
先求出FIRSTVT和LASTVT。
找Firstvt的三条规则:如果要找A的Firstvt,A的候选式中出现:
A->a.......,即以终结符开头,该终结符入Firstvt
A->B.......,即以非终结符开头,该非终结符的Firstvt入A的Firstvt
A->Ba.....,即先以非终结符开头,紧跟终结符,则终结符入Firstvt
找Lastvt的三条规则:如果要找A的Lastvt,A的候选式中出现:
A->.......a,即以终结符结尾,该终结符入Lastvt
A->.......B,即以非终结符结尾,该非终结符的Lastvt入A的Lastvt
A->.....aB,即先以非终结符结尾,前面是终结符,则终结符入Lastvt
然后逐条扫描文法规则。例题如下,参考这个例题能很好地理解如何构造优先关系表。
《编译原理》(第4版)第三章例题4.12
⑵ 编译原理 G[S]: S::=a|b|(T) T::=T,S|S怎么消去左递归
T::=T,S|S 转化为以下两个式子
T::=SK
K::=,SK|空
这样就消除左递归了~
不懂的可以继续问我~
⑶ 编译原理里,什么是源语言,什么是目标语言,什么是翻译器,什么是编译器,什么是解释器,什么是T形图
在vc 将c/c++代码翻译成asm文件的过程中
c/c++ 是源语言 asm是目标语言 vc是翻译器
vc将asm在编译成 obj文件 最后于库文件链接成 二进制文件 vc就是编译器
java中 需要跑一个 java虚拟机 比如 sun的 java.exe java.exe就是解释器
c语言 a机器 c语言 b机器 C语言 b机器
a机器 c语言 a机器
图a 图b 图c
在上图中,图(a)为已有的编译程序,图(c)为需要得到的编译程序,图(b)为需要书写的编译程序,只要我们把(b)在(a)上编译就可得到(c)
打个比方
编译器a是已有的在intel主机上将c语言翻译成可在intel主机上运行的编译器 我们希望得到在intel机器上运行的将c语言翻译成可在苹果主机上运行的编译器c 那么我们只需要用c语言写一个将c语言翻译成可在苹果主机上运行的编译器b, 在编译器a上编译c语言写的编译器b 就可以得到编译器c
⑷ 编译原理语法分析编程
#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;
}
⑸ 编译原理t形图 源语言 表示语言 目标语言分别是什么啊,举个例子
编译器本身也是一个软件,该软件用何种语言编写的,比如是用c语言编写的,则c语言就是它的表示语言(一般说成实现语言)。编译系统的功能是翻译,它能把一种高级语言(即源语言)编写的程序等价的翻译成另一低级语言(即目标语言)的程序。
举例:
用 C语言实现一个java编译器,可以将java程序翻译成bytecode,则该编译器的源语言为java,目标语言为bytecode,表示语言是C。
⑹ 编译原理问题,求解答
好,我来帮你理解一下,先看基本知识:
四元式是一种比较普遍采用的中间代码形式。四元式的四个组成成分是:算符op,第一和第二运算对象ARG1和ARG@及运算结果RESULT。运算对象和运算结果有时指用户自己定义的变量,有时指编译程序引进的临时变量。例如a∶=b*c+b*d的四元式表示如下:
(1)(*,b,c,t1)
(2)(*,b,d,t2)
(3)(+, t1, t2, t3)
(4)(∶=,t3, -, a)
四元式和三元式的主要不同在于,四元式对中间结果的引用必须通过给定的名字,而三元式是通过产生中间结果的三元式编号。也就是说,四元式之间的联系是通过临时变量实现的。
有时,为了更直观,也把四元式的形式写成简单赋值形式或更易理解的形式。比如把上述四元式序列写成:
(1)t1∶=b*c
(2)t2∶=b*d
(3)t3∶=t1+t2
(4)a∶=t3
把(jump,-,-,L)写成goto L
把(jrop,B,C,L)写成if B rop C goto L
好,下面分析一下a<b
这是一个表达式,它的结果要么是0,要么是1,因为没有指定这个表达式存放在哪,所以需要一个临时变量来存放它的,在你的问题中,就是T。很显然T有2个值:0或者1
因此,有
101 T:=0 (这个是表达式为假的出口)
103 T:=1 (这个是表达式为真的出口)
因为你的表达式只有一个A<B,因此A<B的真假出口就是表达式的真假出口,所以
100: if a<b goto 103 (a<b为真,跳到真出口103)
101: T:=0(否则,进入假出口)
102: goto 104 (当然要跳过真出口罗,否则T的值不就又进入真出口了,变成真了)
103: T:=1
104:(程序继续执行)
⑺ 提问 编译原理问题(高分)
词法分析 的作用是把输入的源语句转化成单词形式
第五个最右推导没给要推出的句子 如果是 cbb 那过程也不对
E->CB
C->c
B->b
最右推导的分析为
1 CB
2 Cb
3 cb
你给的文法有问题吧,最右推导通俗的说 就是只按照最右边的非终结符推导
你这些都是要干什么的题,如果要考试,后面那几道的类型几乎必考!!!
⑻ 如何将永宏plc的T型图转成指令表
没有用过永宏的PLC,正常情况下,经过编译的梯形图,都可以生成指令表的,不知道永宏的PLC编程软件是否如此?
⑼ 编译原理的题目:对于文法G(E):E→T|E+T|E-T T→F|T*F|T/F F→(E)|i
终极符集合Vt={+,-,*,/,(,),i}
非终极符集合Vi={E,T,F}
最右推导:E => E-T => E-F => E-(E) => E-(T) => E-(T+F) => E-(T+i) => E-(T*F+i)
直接短语:T*F,i
⑽ 编译原理四元式
四元式的一般形式为(op, arg1, arg2, result),其中:op为一个二元(也可以是零元或一元)运算符。arg1和arg2为两个运算对象,可以是变量、常数或者系统定义的临时变量名。result为运算结果。
第一步:T1=a*b,
第二步:T2=c*d,
第三步:T3=T2/e,
第四步:T4=T1-T3,
第五步:f=T4.