一道题:写一个简单的解释器
Description
In this task, you are required to write an interpreter of a very simple language, i.e. the Y language.
The Y language consists of identifiers, integers, assignments, and additions.
There may or may not be white spaces between assignments, and/or between operands (identifiers or integers) and operators (= or +).
Only uppercase and lowercase letters, and decimal digits are allowed in valid identifiers, e.g. abc, xyz123.
The first character of an identifier must be a letter. So identifier 123xyz is invalid.
Integers are represented in decimal formats. Only decimal digits are allowed in valid integers. All the numbers (including calculation results) are very small. So int will be OK for just everything here.
The only operator for calculation is +, i.e. addition. You should NOT implement other arithmetic operators.
A valid assignment starts with a valid identifier, followed by =, then a valid expression. There may or may not be spaces around the identifier and/or =.
A valid expression consists of a single valid operand, or several valid operands separated by +. There may or may not be spaces around operands and/or +.
A valid operand is a valid integer, or a valid identifier which is already assigned a value. (See sample 2.)
A valid Y language program should contain at least one assignment.
Evaluate the expression first, then assign the value to the identifier.
You can use isspace(c) to determine whether c is a space.
The following (possibly informal) grammar may help you understand the language syntax better. But remember that spaces are omitted for brevity in the grammar.
In case you still do not understand the language clearly, you may just consider the Y language as a variant of C++ where there's no main, no variable declaration, no types other than int, no ; after a single line, and no operators other than +.
Do understand the language before you start. Be robust and good luck!
Input Format
A Y language program.
Output Format
For each assignment, print the value of the identifier after assignment in a single line. Once a syntax error is detected, print ERROR in a single line and stop interpreting the program.
Sample Input 1
Sample Output 1
Sample Input 2
Sample Output 2
Sample Input 3
Sample Output 3
Limits
Time limit: 500ms, memory limit: 20000kb. There are 20 test cases and 5 points for each.
这样一道题 写一个简单的解释器。 求指导,先谢了!
In this task, you are required to write an interpreter of a very simple language, i.e. the Y language.
The Y language consists of identifiers, integers, assignments, and additions.
There may or may not be white spaces between assignments, and/or between operands (identifiers or integers) and operators (= or +).
Only uppercase and lowercase letters, and decimal digits are allowed in valid identifiers, e.g. abc, xyz123.
The first character of an identifier must be a letter. So identifier 123xyz is invalid.
Integers are represented in decimal formats. Only decimal digits are allowed in valid integers. All the numbers (including calculation results) are very small. So int will be OK for just everything here.
The only operator for calculation is +, i.e. addition. You should NOT implement other arithmetic operators.
A valid assignment starts with a valid identifier, followed by =, then a valid expression. There may or may not be spaces around the identifier and/or =.
A valid expression consists of a single valid operand, or several valid operands separated by +. There may or may not be spaces around operands and/or +.
A valid operand is a valid integer, or a valid identifier which is already assigned a value. (See sample 2.)
A valid Y language program should contain at least one assignment.
Evaluate the expression first, then assign the value to the identifier.
You can use isspace(c) to determine whether c is a space.
The following (possibly informal) grammar may help you understand the language syntax better. But remember that spaces are omitted for brevity in the grammar.
program ::= assignment+
assignment ::= identifier '=' expression
expression ::= operand ('+' operand)*
operand ::= identifier | integer
In case you still do not understand the language clearly, you may just consider the Y language as a variant of C++ where there's no main, no variable declaration, no types other than int, no ; after a single line, and no operators other than +.
Do understand the language before you start. Be robust and good luck!
Input Format
A Y language program.
Output Format
For each assignment, print the value of the identifier after assignment in a single line. Once a syntax error is detected, print ERROR in a single line and stop interpreting the program.
Sample Input 1
a = 1
b = a + 2 + 3Sample Output 1
1
6Sample Input 2
a = 123
b = a + c
c = 456Sample Output 2
123
ERRORSample Input 3
a = b = 1
xyz = 123Sample Output 3
ERRORLimits
Time limit: 500ms, memory limit: 20000kb. There are 20 test cases and 5 points for each.
这样一道题 写一个简单的解释器。 求指导,先谢了!
...全文
相关推荐
自己写的一个刷题系统,超简单
自己写的一个刷题系统超简单,看不懂可以私聊,有视频讲解
ctf一道逆向题
近期ctf逆向题的一道简单题,新手小白,用它来学习一下手动脱壳。。
LeeCode上一道简单题引发的思考
【前言】可能看过我之前博客的朋友会发现这期有点不一样,hhhh,我怎么成标题党了???没有,而是想通过这个LeeCode简单题(可能平常我们都不屑一顾)来深入思考一些东西,有兴趣的朋友可以继续看下去。 【题目】169.多数元素 题目描述:给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。 你可以假设数组是非空的,并且给定的数组总是存在多数元素。 示例: 示例 1: 输入: [3,2,3] 输出: 3 示例 2: 输入: [2,2,1,1,1,2,2] 输出: 2
王垠:《怎样写一个解释器》
NULL 博文链接:https://chong-zh.iteye.com/blog/1826467
简单代码解释器
简单代码解释器


