Write a parser and an evaluator for the arithmetic expressions generated by the following grammar:
E →
T + E | T - E | T
(Addition/subtraction)
T →
F * T | F / T | F % T | F
F →
( E ) | - F| + F | N
(Plus/minus sign)
N →
D | DN
D →
0 | 1 | 2 | ... | 9
Observe that an expression may contain only integers (generated by N). Hence, the value of an expression is defined to be an integer. In particular, as in C and C++, / and % are integer division for quotient and remainder, respectively. The meanings of other operators are standard.
The priority of operator:
– (Negative)
%
*, /
+, – (Minus)
Each line contains an expression string, with length < 1024.
For each case, the first line is a prompt as following: “case 1:”. The next line is the answer.
Print “syntactically incorrect”, if the input is not accepted.
The last line of each case is an empty line.
範例輸入 1
789-(400+300) 789-400+300 -9*80+72/61%7 72+((38-66) -101**29 123/78%23 45
範例輸出 1
case 1: 89 case 2: 689 case 3: -706 case 4: syntactically incorrect case 5: syntactically incorrect case 6: syntactically incorrect
Pro 專屬功能: 查看這題在歷屆 CPE 出現過幾次 — 升級以解鎖.