编译原理中文法G[E]:E::=E+T|T T::=T*F|F F::=(E)|i输入输出的程序实现代码,急!!!!!
答案:1 悬赏:20 手机版
解决时间 2021-03-19 22:05
- 提问者网友:疯子也有疯子的情调
- 2021-03-19 11:27
编译原理中文法G[E]:E::=E+T|T T::=T*F|F F::=(E)|i输入输出的程序实现代码,急!!!!!
最佳答案
- 五星知识达人网友:神鬼未生
- 2021-03-19 13:04
我这里有个差不多的,不过加减乘除都有namespace 递归下降调用子程序法
你给我邮箱发个邮件 我吧程序法给你吧
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox2.Select();
}
// A → BC
// C → (+|-)BC | ε
// B → ED
// E → (A)| i
// D → *ED | ε
private Stack<char> st = new Stack<char>();
bool j_j = false;//标识外7a64e58685e5aeb931333264653364层是否是减法,若是,则内层加减互换
bool c_c = false;//标识乘除的
private float A()
{
result += "A → BC\n";
float m = B();
if (st.Count != 0)
{
if (st.Peek() == '+')
{
return m + C();
}
else
if (st.Peek() == '-')
{
j_j = true;
return m - C();
}
else
{
C();
return m;
}
}
C();
return m;
}
private float B()
{
result += "B → ED\n";
float m = E();
if (st.Count != 0 && st.Peek() == '*')
return m * D();
else
if (st.Count != 0 && st.Peek() == '/')
{
c_c = true;
return m/D() ;
}
else
{
D();
return m;
}
}
private float C()
{
if (st.Count == 0)
result += "C →ε\n";
else
if (st.Peek() == '+' || st.Peek() == '-')
{
result += "C→" + st.Pop() + "BC\n";
float m = B();
if (st.Count != 0 && st.Peek() == '+')
{
if (j_j)//外层减法
{
j_j = false;//内层
return m - C();
}
else
return m + C();
}
else
if (st.Count != 0 && st.Peek() == '-')
{
if (j_j)
{
return m + C();
}
else
{
j_j = true;
return m - C();
}
}
else
{
C();
return m;
}
}
else //if(st.Peek()
result += "C →ε\n";
return 0;
}
private float D()
{
if (st.Count == 0)
{
result += "D →ε\n";
return 0;
}
if ((st.Peek() == '*') || (st.Peek() == '/'))
{
result += "D→" + st.Pop() + "ED\n";
float m = E();
if (st.Count != 0 && st.Peek() == '*')
{
if (c_c)
{
c_c = false;
return m / D();
}
else
return m * D();
}
else
if (st.Count != 0 && st.Peek() == '/')
{
if (c_c)
{
return m * D();
}
else
{
c_c = true;
return m / D();
}
}
else
{
D();
return m;
}
}
else
{
result += "D →ε\n";
}
return 0;
}
private float E()
{
if (st.Count == 0)
{
result += "错误,结尾应有数字或'('\n";
return 0;
}
if (isdigit(st.Peek()))
{
result += "E→i\n";
float num = 0;
while (st.Count != 0 && isdigit(st.Peek()))
{
num = num * 10 + float.Parse(st.Pop().ToString());
}
return num;
}
else
if (st.Peek() == '(')
{
result += "E→(A)\n";
st.Pop();
float m = A();
if (st.Count != 0)
{
if (st.Peek() == ')')
{
st.Pop();
}
else
result += "非法字符!";
}
else
result += "缺少“)”";
return m;
}
else
result += "错误,第" +(codeLength- st.Count+1).ToString() + "个字应有数字或'('\n";
return 0;
}
private bool isdigit(char d)
{
return d >= '0' && d <= '9';
}
private string result;
private int codeLength;
private void startbtn_Click(object sender, EventArgs e)
{
j_j = false;
c_c = false;
result = "";
label2.Text = "";
string s = textBox2.Text;
codeLength = textBox2.Text.Length;
for (int i = s.Length - 1; i >= 0; i--)
{
st.Push(s[i]);
}
string res = null;
try
{
res = A().ToString();
}
catch (Exception e1)
{
label2.Text = "出现错误:" + e1.Message;
return;
}
if (st.Count != 0)
label2.Text += result + "第" + (textBox2.Text.Length - st.Count + 1) + "个字符处有错误,请仔细查看\n";
else
label2.Text += result + "最终结果是:" + res;
}
}
}
你给我邮箱发个邮件 我吧程序法给你吧
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox2.Select();
}
// A → BC
// C → (+|-)BC | ε
// B → ED
// E → (A)| i
// D → *ED | ε
private Stack<char> st = new Stack<char>();
bool j_j = false;//标识外7a64e58685e5aeb931333264653364层是否是减法,若是,则内层加减互换
bool c_c = false;//标识乘除的
private float A()
{
result += "A → BC\n";
float m = B();
if (st.Count != 0)
{
if (st.Peek() == '+')
{
return m + C();
}
else
if (st.Peek() == '-')
{
j_j = true;
return m - C();
}
else
{
C();
return m;
}
}
C();
return m;
}
private float B()
{
result += "B → ED\n";
float m = E();
if (st.Count != 0 && st.Peek() == '*')
return m * D();
else
if (st.Count != 0 && st.Peek() == '/')
{
c_c = true;
return m/D() ;
}
else
{
D();
return m;
}
}
private float C()
{
if (st.Count == 0)
result += "C →ε\n";
else
if (st.Peek() == '+' || st.Peek() == '-')
{
result += "C→" + st.Pop() + "BC\n";
float m = B();
if (st.Count != 0 && st.Peek() == '+')
{
if (j_j)//外层减法
{
j_j = false;//内层
return m - C();
}
else
return m + C();
}
else
if (st.Count != 0 && st.Peek() == '-')
{
if (j_j)
{
return m + C();
}
else
{
j_j = true;
return m - C();
}
}
else
{
C();
return m;
}
}
else //if(st.Peek()
result += "C →ε\n";
return 0;
}
private float D()
{
if (st.Count == 0)
{
result += "D →ε\n";
return 0;
}
if ((st.Peek() == '*') || (st.Peek() == '/'))
{
result += "D→" + st.Pop() + "ED\n";
float m = E();
if (st.Count != 0 && st.Peek() == '*')
{
if (c_c)
{
c_c = false;
return m / D();
}
else
return m * D();
}
else
if (st.Count != 0 && st.Peek() == '/')
{
if (c_c)
{
return m * D();
}
else
{
c_c = true;
return m / D();
}
}
else
{
D();
return m;
}
}
else
{
result += "D →ε\n";
}
return 0;
}
private float E()
{
if (st.Count == 0)
{
result += "错误,结尾应有数字或'('\n";
return 0;
}
if (isdigit(st.Peek()))
{
result += "E→i\n";
float num = 0;
while (st.Count != 0 && isdigit(st.Peek()))
{
num = num * 10 + float.Parse(st.Pop().ToString());
}
return num;
}
else
if (st.Peek() == '(')
{
result += "E→(A)\n";
st.Pop();
float m = A();
if (st.Count != 0)
{
if (st.Peek() == ')')
{
st.Pop();
}
else
result += "非法字符!";
}
else
result += "缺少“)”";
return m;
}
else
result += "错误,第" +(codeLength- st.Count+1).ToString() + "个字应有数字或'('\n";
return 0;
}
private bool isdigit(char d)
{
return d >= '0' && d <= '9';
}
private string result;
private int codeLength;
private void startbtn_Click(object sender, EventArgs e)
{
j_j = false;
c_c = false;
result = "";
label2.Text = "";
string s = textBox2.Text;
codeLength = textBox2.Text.Length;
for (int i = s.Length - 1; i >= 0; i--)
{
st.Push(s[i]);
}
string res = null;
try
{
res = A().ToString();
}
catch (Exception e1)
{
label2.Text = "出现错误:" + e1.Message;
return;
}
if (st.Count != 0)
label2.Text += result + "第" + (textBox2.Text.Length - st.Count + 1) + "个字符处有错误,请仔细查看\n";
else
label2.Text += result + "最终结果是:" + res;
}
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯