First, the purpose of the experiment
1. Familiarity with the concept of architectural style
2. Understand and apply the style of pipeline filter type.
3, understanding the principle of the interpreter
4. Understanding the compiler model
Second, the experimental environment
Hardware:
Software: Python or any one of your favorite languages
Iii. contents of the experiment
1, the realization "arithmetic" the simple translator.
Result requirements:
1) Implement subtraction arithmetic, allowing simultaneous multiple operands, such as: 2+3*5-6 result is 11
2) operands are integers, integers can have multiple digits
3) Handling Spaces
4) input error display error prompt, and return command status "CALC"
Figure 1 Example of experimental results
Strengthening exercises:
1, the ability of students, you can try to implement assignment statements, such as x=2+3*5-6, return x=11. (Note: To implement the function of the interpreter, not just the display)
2. Try to implement self-increment and decrement symbols, such as x + +
2, using the pipe-filter (Pipes and Filters) style to implement the interpreter
Figure 2 Pipe-filter style
Figure 3 Compiler model
In this experiment, two parts of lexical analysis and grammatical analysis are realized.
Four, the experimental steps:
Class arithmetic
{
Regex regexmultiplicationanddivision = new Regex (@ "([-+]?\d+ (\. \d+))?) ((\*|\/) ([-+]?\d+ (\) ( \d+))) +)");
Regex regexadditionandsubtraction = new regex (@ "\ (([-+]?\d+ (\) (( \d+))?) ((\+|\-) ([-+]?\d+ (\) ( \d+))) +)\)");
Regex regexeliminate = new regex (@ "\ ([-+]?\d+ (\. \d+)) ");
Regex regexcomplete = new Regex (@ "([-+]?\d+ (\. \d+))?) ((\+|\-) ([-+]?\d+ (\) ( \d+))) *)");
Regex regexerror = new regex (@ "\) \ (|\) (\d+ (\) ( \d+))?) | (\d+ (\. ( \d+))?) \(");
Internal string calculation (string expression)
{
if (regexerror.ismatch (expression))
{
throw new Exception ();
}
while (true)
{
int inotmatch = 0;
if (regexmultiplicationanddivision.ismatch (expression))
{
expression = regexmultiplicationanddivision.replace (expression, multiplicationanddivision);
}
Else
{
inotmatch++;
}
if (regexadditionandsubtraction.ismatch (expression))
{
expression = regexadditionandsubtraction.replace (expression, additionandsubtraction);
}
Else
{
inotmatch++;
}
if (regexeliminate.ismatch (expression))
{
expression = regexeliminate.replace (expression, eliminate);
}
Else
{
inotmatch++;
}
if (regexcomplete.match (expression). Value = = expression)
{
Return convert.todouble (regexcomplete.replace (expression, additionandsubtraction)). ToString ();
}
if (Inotmatch = = 3)
{
throw new Exception ();
}
}
}
String multiplicationanddivision (Match match)
{
string text = match. Value;
bool ispositive = true;
foreach (char c in text)
{
if (c = = '-')
{
Ispositive =!ispositive;
}
}
Text = text. Replace ("*+", "*");
Text = text. Replace ("* *", "*");
Text = text. Replace ("/+", "/");
Text = text. Replace ("/-", "/");
Text = text. Replace ("*", ", *");
Text = text. Replace ("/", ",/");
string[] numbers = text. Split (', ');
Double result = convert.todouble (numbers[0]) >= 0? Convert.todouble (Numbers[0]): (-convert.todouble (Numbers[0]));
for (int i = 1; i < numbers. length;i++)
{
if (numbers[i]! = "")
{
Switch (numbers[i][0])
{
Case ' * ':
Result *= convert.todouble (Numbers[i]. Substring (1, numbers[i]. LENGTH-1));
Break
Case '/':
Result/= convert.todouble (Numbers[i]. Substring (1, numbers[i]. LENGTH-1));
Break
}
}
}
if (ispositive = = False)
{
result =-result;
}
Return result >= 0? ("+" + result.) ToString ()): result. ToString ();
}
String additionandsubtraction (Match match)
{
string text = match. Value;
Text = text. Replace ("(", "");
Text = text. Replace (")", "");
Text = text. Replace ("+ +", "+");
Text = text. Replace ("+-", "-");
Text = text. Replace ("-+", "-");
Text = text. Replace ("--", "+");
Text = text. Replace ("+", ", +");
Text = text. Replace ("-", ",-");
string[] numbers = text. Split (', ');
Double result = 0;
foreach (string number in numbers)
{
if (number! = "")
{
Result + = convert.todouble (number);
}
}
Return result >= 0? ("+" + result.) ToString ()): result. ToString ();
}
String eliminate (match match)
{
Return match. Value.substring (1, match. VALUE.LENGTH-2);
}
}
Corresponding structure diagram:
V. Summary of the Experiment
Understanding of architectural applications, and so on.
C # Regular implementation simple arithmetic