KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > grimp > PrecedenceTest


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Ondrej Lhotak
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27
28
29
30
31 package soot.grimp;
32 import soot.*;
33 import soot.jimple.*;
34
35 /** Provides static helper methods to indicate if parenthesization is
36  * required.
37  *
38  * If your sub-expression has strictly higher precedence than you,
39  * then no brackets are required: 2 + (4 * 5) = 2 + 4 * 5 is
40  * unambiguous, because * has precedence 800 and + has precedence 700.
41  *
42  * If your subexpression has lower precedence than you, then
43  * brackets are required; otherwise you will bind to your
44  * grandchild instead of the subexpression. 2 * (4 + 5) without
45  * brackets would mean (2 * 4) + 5.
46  *
47  * For a binary operation, if your left sub-expression has the same
48  * precedence as you, no brackets are needed, since binary operations
49  * are all left-associative. If your right sub-expression has the
50  * same precedence than you, then brackets are needed to reproduce the
51  * parse tree (otherwise, parsing will give e.g. (2 + 4) + 5 instead
52  * of the 2 + (4 + 5) that you had to start with.) This is OK for
53  * integer addition and subtraction, but not OK for floating point
54  * multiplication. To be safe, let's put the brackets on.
55  *
56  * For the high-precedence operations, I've assigned precedences of
57  * 950 to field reads and invoke expressions (.), as well as array reads ([]).
58  * I've assigned 850 to cast, newarray and newinvoke.
59  *
60  * The Dava DCmp?Expr precedences look fishy to me; I've assigned DLengthExpr
61  * a precedence of 950, because it looks like it should parse like a field
62  * read to me.
63  *
64  * Basically, the only time I can see that brackets should be required
65  * seems to occur when a cast or a newarray occurs as a subexpression of
66  * an invoke or field read; hence 850 and 950. -PL
67  */

68 public class PrecedenceTest
69 {
70     public static boolean needsBrackets( ValueBox subExprBox, Value expr ) {
71         Value sub = subExprBox.getValue();
72         if( !(sub instanceof Precedence) ) return false;
73         Precedence subP = (Precedence) sub;
74         Precedence exprP = (Precedence) expr;
75         return subP.getPrecedence() < exprP.getPrecedence();
76     }
77     public static boolean needsBracketsRight( ValueBox subExprBox, Value expr ) {
78         Value sub = subExprBox.getValue();
79         if( !(sub instanceof Precedence) ) return false;
80         Precedence subP = (Precedence) sub;
81         Precedence exprP = (Precedence) expr;
82         return subP.getPrecedence() <= exprP.getPrecedence();
83     }
84 }
85
Popular Tags