java - 使用hashmap java将两个键的值与第三个键相加
问题描述
我正在用 Jflex 和 CUP 制作编译器。我试图让我的编译器使这个例子工作。
start
x=1;
y=2;
z=x+y;
print(z);
end
我正在努力将x+y之和的值分配给z
这是我的代码的一部分
action code {:
public static HashMap<String, Object> hm = new HashMap<>();
public static void Put(String identifier, Object value) {
hm.put(identifier, value);
}
public static Object Get(String identifier) {
return hm.get(identifier);
}
public static void Show(String value) {
System.out.println("Result: " + value);
System.out.println("END.");
}
:};
我的语法是这样的
statement::= IDENTIFIER:v expr:e
{: RESULT = Get((String)v); :}
| IDENTIFIER:v = expr:e {: Put((String)v, e); :}
| IDENTIFIER:v1 = IDENTIFIER:v2 {: Put((String)v1, Get((String)v2)); :}
| PRINT LPARENTESIS IDENTIFIER:v RPARENTESIS {: System.out.println(Get((String)v)); :}
| IDENTIFIER:v1 = IDENTIFIER:v2 SUM IDENTIFIER:v3 {: Put((String)v1, Get((String)v2+v3)); :}
|expr:e
|
;
expr ::= expr:e1 SUM expr:e2
{: RESULT ASIGN new Integer(e1.intValue() + e2.intValue()); :}
| expr:e1 MINUS expr:e2
{: RESULT = new Integer(e1.intValue() - e2.intValue()); :}
| NUMBER:n
{: RESULT = n; System.out.println(n); :}
| LPARENTESIS expr:e RPARENTESIS
{: RESULT = e; :}
;
符号
SUM = "+"
LPARENTESIS = "("
RPARENTESIS = ")"
MINUS= "-"
ASIGN = "="`
| IDENTIFIER:v1 = IDENTIFIER:v2 + IDENTIFIER:v3 {: Put((String)v1, Get((String)v2+v3)); :}
这是我努力添加 v2 和 v3 的值并将其存储在 v1 中的地方
我在java中编程不多,需要帮助谢谢
解决方案
推荐阅读
- mysql - 如果表中不存在列值,则将行插入表中
- python - Python:空的测试套件
- node.js - 如何删除全局符号链接而不删除 npm 中的本地依赖项?
- java - 无法在带有查询注释的日期 jpa 数据之间执行
- javascript - 如何在 HTML 中使用枚举?
- sql - 在 MS Access 中将 2 个查询结果合并到一个查询的不同列中
- css - 在 mask 属性中使用 SVG 时,SVG 不会出现
- reactjs - React - 全局使用 Bootstrap 和 CSS 模块
- docker - 如何确定 Docker 版本
- python - Telethon:如何上传频道/聊天徽标?