当前位置:网站首页>SDUT compilation principle experimental code
SDUT compilation principle experimental code
2022-07-24 09:01:00 【Send a tilt dye|】
Compiler principle experiment
A - Small C Language – Lexical analysis program
answer :
#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={
-1, 0, 1, 0};
int dy[]={
0, 1, 0, -1};
using namespace std;
string key[]={
"main","if","else","for","while","int"};
string res[]={
"keyword","identifier","integer","boundary","operator"};
void judge(string s){
if(isdigit(s[0])) cout<<"("<<res[2]<<","<<s<<")"<<endl;
else{
bool tag=0;
for(int i=0;i<6;i++){
if(s==key[i]){
tag=1;
cout<<"("<<res[0]<<","<<s<<")"<<endl;
break;
}
}
if(!tag) cout<<"("<<res[1]<<","<<s<<")"<<endl;
}
}
inline void solve(){
string s;
while(cin>>s){
string str="";
for(int i=0;i<s.size();i++){
if(s[i]=='='||s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/'||s[i]=='<'||s[i]=='>'||s[i]=='!'){
if(str.size()) judge(str);
str="";
if(i+1<s.size()&&s[i+1]=='=') cout<<"("<<res[4]<<","<<s[i]<<s[i+1]<<")"<<endl,i++;
else cout<<"("<<res[4]<<","<<s[i]<<")"<<endl;
}else if(s[i]=='('||s[i]==')'||s[i]=='{'||s[i]=='}'||s[i]==','||s[i]==';'){
if(str.size()) judge(str);
str="";
cout<<"("<<res[3]<<","<<s[i]<<")"<<endl;
}
else str+=s[i];
}
if(str.size()) judge(str);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
//cin>>t;
while(t--) solve();
return 0;
}
B - Identify floating point constant problems
Law 1 :
#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={
-1, 0, 1, 0};
int dy[]={
0, 1, 0, -1};
using namespace std;
bool judge(string s,int l,int r){
bool flag=0;
int i,j;
for(i=l;i<=r;i++){
if(s[i]=='+'||s[i]=='-'){
if(i!=l){
if(i>=r) return 0;
if(!((s[i-1]=='e'||s[i-1]=='E')&&isdigit(s[i+1]))) break;
}
}else if(s[i]=='e'||s[i]=='E'){
if(i>=r) return 0;
if(!(isdigit(s[i-1])&&(s[i+1]=='+'||s[i+1]=='-'||isdigit(s[i+1])))) break;
for(j=i;j<=r;j++){
if(s[j]=='.') break;
}
if(j<=r) break;
}else if(s[i]=='.'){
if(i>=r) return 0;
if(!(isdigit(s[i-1])&&isdigit(s[i+1]))) break;
}else if(isdigit(s[i])) continue;
else break;
}
if(i<r) return 0;
else return 1;
}
inline void solve(){
string s;
while(getline(cin,s)){
//cout<<s<<endl;
int l,r;
for(int i=0;i<s.size();i++){
if(s[i]!=' '){
l=i;
break;
}
}
for(int i=s.size()-1;i>=0;i--){
if(s[i]!=' '){
r=i;
break;
}
}
int tag1=0,tag2=0;
for(int i=l;i<=r;i++){
if(s[i]=='.') tag1++;
if(s[i]=='e'||s[i]=='E') tag2++;
}
if((tag1==1&&tag2==1)||tag1+tag2==1){
if(judge(s,l,r)) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
else cout<<"NO"<<endl;
}
}
int main() {
solve();
return 0;
}
Law two :
#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={
-1, 0, 1, 0};
int dy[]={
0, 1, 0, -1};
using namespace std;
int get(int tag,char op){
switch(tag){
case 1:
if(op=='+'||op=='-') return 2;
if(isdigit(op)) return 3;
return -1;
case 2:
if(isdigit(op)) return 3;
return -1;
case 3:
if(isdigit(op)) return 3;
if(op=='.') return 4;
if(op=='e'||op=='E') return 6;
return -1;
case 4:
if(isdigit(op)) return 5;
return -1;
case 5:
if(isdigit(op)) return 5;
if(op=='\0') return 0;
if(op=='e'||op=='E') return 6;
return -1;
case 6:
if(op=='+'||op=='-') return 7;
if(isdigit(op)) return 8;
return -1;
case 7:
if(isdigit(op)) return 8;
return -1;
case 8:
if(isdigit(op)) return 8;
if(op=='\0') return 0;
return -1;
default:
return -1;
}
}
inline void solve(){
string s;
while(getline(cin,s)){
int l=0,r=s.size()-1;
while(l<s.size()&&s[l]==' ') l++;
while(r>=0&&s[r]==' ') r--;
string str="";
for(;l<=r;l++) str+=s[l];
str+='\0';
//cout<<str<<endl;
int tag=1;
for(int i=0;i<str.size();i++){
tag=get(tag,str[i]);
if(tag==-1) break;
}
if(!tag) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
//in>>t;
while(t--) solve();
return 0;
}
D - Expression parsing —— Recursive subroutine method
#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={
-1, 0, 1, 0};
int dy[]={
0, 1, 0, -1};
using namespace std;
int pos,cas;
string s;
void E(); // E-->TG
void T(); // T-->FS
void F(); // F-->(E)|i
void S(); // S-->*FS|ε
void G(); // G-->+TG|ε
void E(){
if(s[pos]=='('||s[pos]=='i'){
// E-->TG
cout<<cas++<<" E-->TG"<<endl;
T();G();
}else{
cout<<"error"<<endl;
exit(0);
}
}
void T(){
if(s[pos]=='('||s[pos]=='i'){
// T-->FS
cout<<cas++<<" T-->FS"<<endl;
F();S();
}else{
cout<<"error"<<endl;
exit(0);
}
}
void F(){
if(s[pos]=='('){
// F-->(E)
cout<<cas++<<" F-->(E)"<<endl;
pos++;
E();
if(s[pos]==')') pos++;
else{
cout<<"error"<<endl;
exit(0);
}
}else if(s[pos]=='i'){
// F-->i
cout<<cas++<<" F-->i"<<endl;
pos++;
}else{
cout<<"error"<<endl;
exit(0);
}
}
void S(){
if(s[pos]=='*'){
// S-->*FS
cout<<cas++<<" S-->*FS"<<endl;
pos++;
F();S();
}else cout<<cas++<<" S-->&"<<endl; // S-->ε
}
void G(){
if(s[pos]=='+'){
// G-->+TG
cout<<cas++<<" G-->+TG"<<endl;
pos++;
T();G();
}else cout<<cas++<<" G-->&"<<endl; // // G-->ε
}
inline void solve(){
cin>>s;
E();
if(s[pos]=='#') cout<<"accept"<<endl;
else cout<<"error"<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
E - Expression parsing —— Predictive analysis
#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={
-1, 0, 1, 0};
int dy[]={
0, 1, 0, -1};
using namespace std;
int pos,cas=1;
string s;
stack<char>st;
bool judge(char x,char y){
if(x=='E'&&(y=='('||y=='i')){
cout<<cas++<<" E->TG"<<endl;
st.pop();
st.push('G');
st.push('T');
return 1;
}else if(x=='T'&&(y=='('||y=='i')){
cout<<cas++<<" T->FS"<<endl;
st.pop();
st.push('S');
st.push('F');
return 1;
}else if(x=='F'&&y=='('){
cout<<cas++<<" F->(E)"<<endl;
st.pop();
st.push(')');
st.push('E');
st.push('(');
return 1;
}else if(x=='F'&&y=='i'){
cout<<cas++<<" F->i"<<endl;
st.pop();
st.push('i');
return 1;
}else if(x=='S'&&y=='*'){
cout<<cas++<<" S->*FS"<<endl;
st.pop();
st.push('S');
st.push('F');
st.push('*');
return 1;
}else if(x=='S'&&(y=='+'||y==')'||y=='#')){
cout<<cas++<<" S->^"<<endl;
st.pop();
return 1;
}else if(x=='G'&&y=='+'){
cout<<cas++<<" G->+TG"<<endl;
st.pop();
st.push('G');
st.push('T');
st.push('+');
return 1;
}else if(x=='G'&&(y==')'||y=='#')){
cout<<cas++<<" G->^"<<endl;
st.pop();
return 1;
}else{
if(x==y){
st.pop();
pos++;
return 1;
}
else return 0;
}
}
inline void solve(){
cin>>s;
st.push('#');
st.push('E');
while(1){
if(st.top()=='#'&&s[pos]=='#'){
cout<<"acc!"<<endl;
break;
}
else{
if(!judge(st.top(),s[pos])){
cout<<"error!"<<endl;
break;
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
N - DAG Optimize
#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={
-1, 0, 1, 0};
int dy[]={
0, 1, 0, -1};
using namespace std;
int n;
int tot;
struct node{
char id;
int l=-1,r=-1;
vector<char>vp;
}dp[M];
bool Find_var(int i,char op){
for(auto j:dp[i].vp){
if(j==op){
return 1;
}
}
return 0;
}
int add_node(char op){
for(int i=tot-1;i>=0;i--){
if(dp[i].id==op||Find_var(i,op)) return i;
}
dp[tot].id=op;
return tot++;
}
void add_op(char c,char op,int l,int r){
for(int i=tot-1;i>=0;i--){
if(dp[i].l==l&&dp[i].r==r&&dp[i].id==op){
dp[i].vp.eb(c);
return ;
}
}
dp[tot].id=op;
dp[tot].vp.eb(c);
dp[tot].l=l;
dp[tot].r=r;
tot++;
}
bool st[M];
void DFS(int x){
if(dp[x].l!=-1){
st[x]=1;
DFS(dp[x].l);
DFS(dp[x].r);
}
}
char res[M][11];
char judge(int x){
char op=0;
for(auto i:dp[x].vp){
if(i=='A'||i=='B') op=i;
}
if(op) return op;
else return dp[x].vp[0];
}
inline void solve(){
cin>>n;
string s;
for(int i=0;i<n;i++){
cin>>s;
int l=add_node(s[2]);
int r=add_node(s[4]);
add_op(s[0],s[3],l,r);
}
for(int i=0;i<tot;i++){
if(dp[i].l!=-1){
res[i][0]=judge(i);
res[i][1]='=';
node lr=dp[dp[i].l];
node rr=dp[dp[i].r];
if(lr.vp.size()) res[i][2]=judge(dp[i].l);
else res[i][2]=lr.id;
res[i][3]=dp[i].id;
if(rr.vp.size()) res[i][4]=judge(dp[i].r);
else res[i][4]=rr.id;
res[i][5]=0;
}
}
for(int i=tot-1;i>=0;i--){
if(res[i][0]=='A'){
DFS(i);
break;
}
}
for(int i=tot-1;i>=0;i--){
if(res[i][0]=='B'){
DFS(i);
break;
}
}
for(int i=0;i<tot;i++){
if(st[i]) cout<<res[i]<<endl;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("textin.txt","r",stdin);
// freopen("textout.txt","w",stdout);
int t=1;
//cin>>t;
while(t--) solve();
return 0;
}
P - Simple code generator
#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={
-1, 0, 1, 0};
int dy[]={
0, 1, 0, -1};
using namespace std;
int n,m;
int tot;
string s[M];
char R[M];
int inr(char op){
for(int i=0;i<m;i++){
if(R[i]==op) return i;
}
return -1;
}
int get_lastuse(int pos,char op){
for(int i=pos;i<n;i++){
if(s[i][3]==op||s[i][5]==op) return i;
}
return n;
}
int get_pos(int pos){
if(tot<m) return tot++;
int res=-1,maxn=-1;
for(int i=0;i<m;i++){
int ne=get_lastuse(pos,R[i]);
if(ne>maxn){
maxn=ne;
res=i;
}
}
return res;
}
void printop(char op){
if(op=='+') printf("ADD");
else if(op=='-') printf("SUB");
else if(op=='*') printf("MUL");
else if(op=='\\') printf("DIV");
}
void print(char op){
int pos=inr(op);
if(pos!=-1) printf("R%d\n",pos);
else printf("%c\n",op);
}
inline void solve(){
cin>>n>>m;
for(int i=0;i<n;i++) cin>>s[i];
for(int i=0;i<n;i++){
int pos=inr(s[i][3]);
if(pos==-1){
pos=get_pos(i);
if(R[pos]&&get_lastuse(i,R[pos])<n){
printf("ST R%d, %c\n",pos,R[pos]);
R[pos]=NULL;
}
printf("LD R%d, %c\n",pos,s[i][3]);
}
printop(s[i][4]);
printf(" R%d, ",pos);
print(s[i][5]);
R[pos]=s[i][0];
}
}
int main() {
solve();
return 0;
}
边栏推荐
- Advantages of using partitions
- Read write lock, shared lock, exclusive lock
- Porting boa server on imx6ull
- C language practice questions + Answers:
- 林业调查巡检数据采集解决方案
- JS built-in method
- Paclitaxel loaded tpgs reduced albumin nanoparticles /ga-hsa gambogic acid human serum protein nanoparticles
- TT ecosystem - cross border in-depth selection
- Rank 3 and count from 0 to 9. How many times does each number appear in total, and how many times does each number appear in the tens of hundreds,
- Unity解决Package Manager“You seem to be offline”
猜你喜欢
![[Shangshui Shuo series] final rad New Literacies](/img/a3/73a0b74e29f6bfc8f22ca6235b5465.png)
[Shangshui Shuo series] final rad New Literacies
![[Sheung Shui Shuo series] EE feedback details](/img/19/5a80d5ef80d75be74b7ce08d6df058.png)
[Sheung Shui Shuo series] EE feedback details

Interviewer: man, how much do you know about the read-write lock of go language?

Houdini 官方HDA SideFX Labs 安装

xtrabackup 实现mysql的全量备份与增量备份
![The solution of [an error occurred while trying to create a file in the destination directory: access denied] is prompted when installing the software](/img/e5/391cc8ffc3b0410831883be9bde320.png)
The solution of [an error occurred while trying to create a file in the destination directory: access denied] is prompted when installing the software

redis学习一redis介绍及NIO原理介绍

Taking advantage of the momentum, oceanbase promotes the lean growth of digital payment

Discuz论坛搭建详细过程,一看就懂

Source code analysis of BlockingQueue (arraybq and linkedbq)
随机推荐
From single architecture to distributed architecture, there are many pits and bugs!
OpenCV中文文档4.0.0学习笔记(更新中……)
链表——19. 删除链表的倒数第 N 个结点
The detailed process of building discuz forum is easy to understand
Discuz论坛搭建详细过程,一看就懂
Learn the rxjs operator
Rocky基础-Shell脚本基础知识
Android系统安全 — 5.2-APK V1签名介绍
Why is TCP a triple handshake
Unity解决Package Manager“You seem to be offline”
JS built-in method
JQ native write bullet frame mask layer
PIP3 installation complete with source
我们说的组件自定义事件到底是什么?
[FFH] websocket practice of real-time chat room
[Shangshui Shuo series] final rad New Literacies
【情感】何为“优秀”
Source code analysis of BlockingQueue (arraybq and linkedbq)
RPC调用方如何实现异步调用:CompletableFuture
读写锁、共享锁、独占锁