Simple syntax analysis (arithmetic expressions without parentheses) E\E'\T\T'\F procedure

using System;

namespace test3
{
/*For grammar
E→TE'
E'→+TE' | -TE' |ε
T →FT'
T'→* FT' | /FT'|ε
F →(E) | i
according to recursive descent analysis A program construction method, or predictive analysis method, constructs a program for this grammar. The function of the program is, given the input, the output of the productions that the program will use in order.
For example, input 25.6 * 14.5 + 2 (first through lexical analysis, convert it to i * i + i), then output
E→TE'
T→FT'
F→i
T'→* FT'
F→i
T'→ ε
E'→+TE'
T→FT'
F→i
T'→ε
E'→ε
*/
class Program
{
//Conversion calculation
static void Main(string[] args)
{
Console.WriteLine("Please enter the arithmetic Equation: ");
String startequation = Console.ReadLine();
char[] equation = new char[10];
int wei = 0;
char[] charToken = startequation.ToCharArray();
int start = 0;
bool grammer = true;
bool startchar = true;
for (int i = 0; i < charToken.Length; i++)
{
if (charToken[i] != ' ')
{
start = i;
break;
}
}
// Loop through the input characters, if it is not an arithmetic symbol or a number or a decimal point, an error will be reported
for (int i = start; i < charToken.Length; i++)
{
//Console.WriteLine(charToken[i]);
if (!JudgeSymbol(charToken[i]))
{
if (!JudgeNumber(charToken[i]))
{
if (!Judgepoint(charToken[i]))
{
grammer = false;
break;
}
}
}
}
if (grammer)
{
startchar = Judgestart(charToken[start]);
for (int i = start; i < charToken.Length; i++)
{
if (startchar)
{
//If it is a number or decimal point, do not operate
if (JudgeNumber(charToken[i] ) || Judgepoint(charToken[i]))
{
equation[wei] = 'i';
continue;
}
//if it's not a number, it's an arithmetic symbol
else
{
wei += 1;
equation[wei] = charToken[i] ;
wei += 1;
startchar = Judgestart(charToken[i + 1]);
}
}
}
}
else
{
Console.WriteLine("The arithmetic expression contains non-arithmetic symbols, non-numbers and non-decimal points, the format is wrong!");
}
// View the converted formula
Console.WriteLine("The arithmetic formula is converted to: ");
for (int i = 0; i < equation.Length; i++)
{
Console.Write(equation[i]);
}
Console.WriteLine();
E(equation, 0);
}
//E过程
public static void E(char[] c,int value)
{
Console.WriteLine("E→TE'");
ET(c, value);
}
//E-T过程
public static void ET(char[] c, int value)
{
//Console.WriteLine(c[value]);
T(c, value);
}
//E-E1过程
public static void EE1(char[] c, int value)
{
//Console.WriteLine(c[value]);
E1(c, value);
}
//T过程
public static void T(char[] c, int value)
{
Console.WriteLine("T→FT'");
TF(c, value);
}
//TF过程
public static void TF(char[] c, int value)
{
//Console.WriteLine(c[value]);
F(c, value);
}
//TT1过程
public static void TT1(char[] c, int value)
{
//Console.WriteLine(c[value]);
T1(c, value);
}
//E1过程
public static void E1(char[] c, int value)
{
//判断‘+’
if (c[value] == '+')
{
Console.WriteLine("E'→+TE'");
//Console.WriteLine(c[value + 1]);
T(c, value + 1);
}
//判断‘-’
else if (c[value] == '-')
{
Console.WriteLine("E'→-TE'");
//Console.WriteLine(c[value + 1]);
T(c, value + 1);
}
else
{
Console.WriteLine("E'→ε");
}
}
//T1过程
public static void T1(char[] c,int value)
{
//判断‘*’
if (c[value] == '*')
{
Console.WriteLine("T'→*FT'");
//Console.WriteLine(c[value + 1]);
F(c, value + 1);
//T1(c, value + 2);
}
//判断‘/’
else if (c[value] == '/')
{
Console.WriteLine("T'→/FT'");
//Console.WriteLine(c[value + 1]);
F(c, value + 1);
//T1(c, value + 2);
}
else {
Console.WriteLine("T'→ε");
EE1(c, value);
}
}
//F
public static void F(char[] c,int value)
{
if (c[value] == '(')
{
Console.WriteLine("F→(E)");
E(c, value + 1);
}
//数字
else if (c[value] == 'i')
{
Console.WriteLine("F→i");
}
TT1(c,value + 1);
}
//判断算术符
public static bool JudgeSymbol(char c)
{
if ((c == '+') || (c == '-') || (c == '*') || (c == '/'))
{
return true;
}
return false;
}
//判断数字
public static bool JudgeNumber(char c)
{
if (c >= '0' && c <= '9')
{
return true;
}
return false;
}
//Judge the decimal point
public static bool Judgepoint(char c)
{
if (c == '.')
{
return true;
}
return false;
}
//Judge the first character is not a symbol and a decimal point
public static bool Judgestart( char c)
{
if (!JudgeSymbol(c) && !Judgepoint(c))
{
return true;
}
else
{
Console.WriteLine("Format error!");
return false;
}
}
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325117041&siteId=291194637