编程介的小学生 2017-11-20 14:05 采纳率: 20.5%
浏览 626
已采纳

Liveness Analysis

Problem Description
A program syntax is defined as below:
PROGRAM ::= STATEMENTS end '\n'
STATEMENTS ::= STATEMENT STATEMENTS | epsilon
STATEMENT ::= a [variable] '\n' | u [variable] '\n' | IF-CLAUSE
IF-CLAUSE ::= if '\n' STATEMENTS end '\n' | if '\n' STATEMENTS else '\n' STATEMENTS end '\n'

In which '\n' means the new line character, epsilon means an empty string, [variable] means a variable, which is represented by a number between 1 and n (inclusive). Same numbers denote the same variable.
The program runs as follows:
If the statement is "a [variable]", it assigns the variable a new value;
If the statement if "u [variable]", it uses value of the variable to do something interesting;
If there is an IF-CLAUSE, it is possible that condition of this clause is satisfied or not satisfied.

We only need to do liveness analysis on this program. This is why we do not explicitly write assigned values, what we do with the variables and condition of IF-CLAUSEs.
In this task, there are three kinds of liveness for each of the variables.
1. Live. In one or more paths of this program, value of the variable at the beginning of the program is used.
2. Dead. In all paths of this program, the variable is assigned a new value before any use of this variable.
3. Half-dead. In all paths of this program, value of the variable at the beginning of the program is not used, but in one or more paths, the variable is never assigned a new value.

Input
First line, number of test cases, T.
Following are T test cases. For each test case, the first line is n, number of variables in the program. The following lines is the program.

Number of lines in the input <= 500000

Output
T lines. Each line contains n numbers. If the variable is live, output 1; if the variable is dead, output -1; if the variable is half-dead, output 0.

Sample Input
10
1
end
1
a 1
end
1
u 1
end
1
a 1
u 1
end
1
u 1
a 1
end
1
if
a 1
else
a 1
end
end
1
if
a 1
else
u 1
end
end
1
if
a 1
end
end
1
if
u 1
end
end
2
a 1
u 2
end

Sample Output
0
-1
1
-1
1
-1
1
0
1
-1 1

  • 写回答

1条回答 默认 最新

  • threenewbee 2018-09-15 11:49
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能