📄 parsetree.java
字号:
package MyNa.utils;
import java.io.*;
import java.util.*;
/* *****************************************************************
The ParseTree class depends on a StringBuffer to contain the
text being parsed; a ParseTree pT has 0 or more children, a
range lo..hi and for 0<= i < pT.numChildren() we find
that pT.child(i) is a ParseTree, with pT.child(i).parent===pT;
we also find that the children's subranges never overlap, and
generally add up to a little less than the full lo..hi range.
Sample: given a string
Now <SUBST> is the |x| for all </SUBST> good <SUBST>|y|</SUBST>
to <SUBST delim="$">$a$ of the $b$, so go to sleep.</SUBST>
we would generate the parse tree describable as follows:
ROOT
TEXT:"Now "
SUBST[delim:"|"]
TEXT:" is the "
DELIM:"x"
TEXT:" for all "
TEXT:" good "
SUBST[delim="|"]
DELIM:"y"
TEXT:" to "
SUBST[delim="$"]
DELIM:"a"
TEXT:" of the "
DELIM:"b"
TEXT:", so go to sleep."
another diagram would be:
<ROOT>
<TEXT>Now </TEXT>
<SUBST delim="|">
<TEXT> is the </TEXT>
<DELIM>x</DELIM>
<TEXT> for all </TEXT>
</SUBST>
<TEXT> good </TEXT>
<SUBST delim="|">
<DELIM>y</DELIM>
</SUBST>
<TEXT> to </TEXT>
<SUBST delim="$">
<DELIM>a</DELIM>
<TEXT> of the </TEXT>
<DELIM>b</DELIM>
<TEXT>, so go to sleep.</TEXT>
</SUBST>
</ROOT>
(In this diagram, whitespace is significant in any innermost block
such as <TEXT>..</TEXT> or <DELIM>..</DELIM>, but not outside.)
the default write(Writer) method and the default toString()
method both produce XML-ish output like that of our second diagram.
In either case, we have basic properties such as these:
pS.tagName=="ROOT";
pS.numChildren()==6;
pS.child(0).tagName=="TEXT";
pS.child(0).value()=="Now ";
pS.child(0).value()== ? (null, or recursive evaluation)
pS.child(1).numChildren()==3;
pS.child(1).child(1).tagName=="DELIM";
pS.child(1).child(1).value()=="x";
pS.child(1).child(1).get("delim")=="|";
and so on.
We work with a grammar which is not completely context-free:
Root ::= T (Subst T)* ;
Subst ::= SubstTok(tok,d) T (Delim(d) T)* EndTok(tok);
Delim(d) ::= d T d ;
Read this by example, from bottom to top:
A delimited text is a delimiter d, followed by the text T, followed by d.
Thus "$alpha$" might be a delimited text if "$" is the delimiter d.
A "Subst" range begins with a SubstTok, e.g. <SUBST, defining the specific
token and the delimiter to be used within that range; it ends with a
corresponding EndTok; in between, we have a text, followed by zero or
more (delimited-text, plain text) pairs. For example,
<SUBST delim="$">From $alpha$ to $omega$, slowly</SUBST>
contains the initial text "From ", then the pairs ($alpha$," to ") and
($omega$, ", slowly").
The input (root) consists of a plain text, followed by zero or more
(Subst-range, plain text) pairs.
Representation: a parse tree has a tagName which is a String, a
Hashtable to store properties, and a vector of children; it also
has a parent which is a parsetree (but may of course be null) and
an index which identifies it to its parent (which may be -1, if
it has no parent). If parseTree is used without subclassing, then
the value() function simply reports contiguous text, from int
startText to int endText-1, from its StringBuffer. The "put"
method calls on that of the Hashtable, the "get" method ditto.
it has a startLoc and endLoc, with setStartLoc and setEndLoc methods.
The tagName can be replaced with the setTagName(String) method.
*/
public class ParseTree {
int lo=0; int hi=0; String tagName=null;
Hashtable props=null; Vector children=null;
public ParseTree(String tag){this(tag,0,0,null);}
public ParseTree(String tag, int L){this(tag,L,L,null);}
public ParseTree(String tag, int L,int H){this(tag,L,H,null);}
public ParseTree(String tag, int L,Hashtable H){this(tag,L,L,H);}
public ParseTree(String tag, int L,int H,Hashtable tab){
tagName=tag;lo=L;hi=H;props=tab;
}
public Object get(Object key){
if(props==null)return null;
else return props.get(key);
}
public Object put(Object key,Object val){
if(props==null)props=new Hashtable(1);
props.put(key,val);
return val;
}
public void setTagName(String tag){tagName=tag;}
public void setLow(int i){lo=i;}
public void setHigh(int i){hi=i;}
public String getTagName(){return tagName;}
public int getLow(){return lo;}
public int getHigh(){return hi;}
public void setProps(Hashtable H){props=H;}
public Hashtable getProps(){return props;}
public int numChildren(){
if(children==null)return 0;
else return children.size();
}
public ParseTree child(int i){
if(numChildren()<=i)return null;
return (ParseTree)children.elementAt(i);
}
public void addChild(ParseTree pT){
if(children==null)children=new Vector(1);
children.addElement(pT);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -