一道题:写一个简单的解释器

maview 2013-07-04 09:41:07
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.

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 + 3

Sample Output 1
1
6

Sample Input 2
a = 123
b = a + c
c = 456

Sample Output 2
123
ERROR

Sample Input 3
a = b = 1
xyz = 123

Sample Output 3
ERROR

Limits
Time limit: 500ms, memory limit: 20000kb. There are 20 test cases and 5 points for each.

这样一道题 写一个简单的解释器。 求指导,先谢了!
...全文
163 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-07-04
  • 打赏
  • 举报
回复
《编译原理》词法分析 有限状态自动机
geekjack 2013-07-04
  • 打赏
  • 举报
回复
引用 3 楼 zhao4zhong1 的回复:
LEX+YACC ?
赵老师 这只是个ACM题目呀 不是真要写解释器 - -
赵4老师 2013-07-04
  • 打赏
  • 举报
回复
LEX+YACC ?
赵4老师 2013-07-04
  • 打赏
  • 举报
回复
FLEX+BISON ?
geekjack 2013-07-04
  • 打赏
  • 举报
回复
LZ能看懂规则吗? 看懂了的话就不难实现了 程序更多的是涉及到一行字符串转化为表达式的检查,解释器解释过程中只要有一行不符合规则就停止解析抛出ERROR退出程序,符合规则就计算出结果,可能要有一个预置长度的数组来存储已出现的变量以及变量值。 自己实现以下吧 或者是贴出已经写过的代码,说说有什么问题,这样比较好哦。

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧