简体   繁体   English

表达式的Java语法(仅JDK7)

[英]java syntax for Expression (JDK7 only)

This is for jdk 7 only. 这仅适用于jdk 7。 The following grammar is taken from https://docs.oracle.com/javase/specs/jls/se7/html/jls-18.html 以下语法摘自https://docs.oracle.com/javase/specs/jls/se7/html/jls-18.html

Expression: 
    Expression1 [AssignmentOperator Expression1]

Expression1: 
    Expression2 [Expression1Rest]

Expression2:
    Expression3 [Expression2Rest]

Expression2Rest:
    { InfixOp Expression3 }
    instanceof Type

InfixOp: 
|| && | ^ & == != < > <= >= << >> >>> + - * / %

Look at this code snippet 看看这个代码片段

 if (Math.abs(x) >= EDGE || Math.abs(y) >= EDGE)
        clear();

The line inside the parenthesis is an expression (by definition of ParExpression, which is not listed above) 括号内的行是一个表达式(根据ParExpression的定义,上面没有列出)

 Math.abs(x) >= EDGE || Math.abs(y) >= EDGE

But I am not able to parse it according to the grammar: 但是我不能根据语法来解析它:

  1. ">= EDGE" is an Expression2Rest. “> = EDGE”是Expression2Rest。
  2. "Math.abs(x) >= EDGE" is an Expression2 (since "Math.abs(x)" is an Expression3). “ Math.abs(x)> = EDGE”是Expression2(因为“ Math.abs(x)”是Expression3)。
  3. Similarly, "Math.abs(y) >= EDGE" is an Expression2. 同样,“ Math.abs(y)> = EDGE”是一个Expression2。
  4. "||" “ ||” is an infix op, which indicates the next part should be an Expression3. 是一个infix op,表示下一部分应该是Expression3。

Here is the problem: "Math.abs(y)>=EDGE" cannot be an Expression3, since it contains the infix op. 这是问题所在:“ Math.abs(y)> = EDGE”不能为Expression3,因为它包含中缀op。 It is an Expression2 by the grammar. 它是语法上的一个Expression2。 After parsing the example becomes 解析后,示例变为

Expression2 InfixOp Expression2

This does not lead to a valid Expression. 这不会导致有效的表达式。 What is wrong with the above logic? 以上逻辑有什么问题? In addition, currently I use recursion to do the parsing. 另外,当前我使用递归进行解析。 That's inefficient. 效率低下。 Any tutorial for parsing by tree or graph? 是否有任何通过树或图进行解析的教程? I need to parse the real java code, the tutorials about parsing simple arithmetic expressions do not help. 我需要解析真正的Java代码,有关解析简单算术表达式的教程无济于事。 Thanks. 谢谢。

Read the doc: 阅读文档:

  • [x] denotes zero or one occurrences of x . [x]表示x出现零次或一次。
  • {x} denotes zero or more occurrences of x . {X}表示零和或多个x的。

So: 所以:

Expression2:
    Expression3 [Expression2Rest]
Expression2Rest:
    { InfixOp Expression3 }

Means that: 意思是:

Expression2:
    Expression3 InfixOp Expression3 InfixOp Expression3 InfixOp Expression3 ...

Hence: 因此:

Math.abs(x) >=      EDGE  ||      Math.abs(y) >=      EDGE
=========== ------- ===== ------- =========== ------- =====
Expr3       InfixOp Expr3 InfixOp Expr3       InfixOp Expr3

And: 和:

Math         . abs           ( x          )
==========     ==========    ================
Identifier { . Identifier } [IdentifierSuffix]   <-- Primary
                             ( Expression )      <-- Arguments

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM