2 import java_cup.runtime.ComplexSymbolFactory;
3 import java_cup.runtime.ScannerBuffer;
4 import java_cup.runtime.XMLElement;
5 import javax.xml.stream.XMLOutputFactory;
6 import javax.xml.stream.XMLStreamWriter;
9 import javax.xml.transform.*;
10 import javax.xml.transform.stream.*;
13 public Parser(Lexer lex, ComplexSymbolFactory sf) {
16 public static void main(String[] args) throws Exception {
17 // initialize the symbol factory
18 ComplexSymbolFactory csf = new ComplexSymbolFactory();
19 // create a buffering scanner wrapper
20 ScannerBuffer lexer = new ScannerBuffer(new Lexer(new BufferedReader(new FileReader(args[0])),csf));
22 Parser p = new Parser(lexer,csf);
23 ParseNode pn = (ParseNode) p.parse().value;
27 terminal SEMICOLON, COMMA, LPAR, RPAR, BEGIN, END, ASSIGN;
28 terminal PUBLIC, INTERFACE, CAPABILITY, DESCRIPTION, METHOD, REQUIRES, WITH, AS;
30 terminal IDENT, STRINGCONST;
32 non terminal ParseNode policy;
33 non terminal ParseNode intface, methlist, meth, paramlist, param;
34 non terminal ParseNode capablist, capab, capabcont, cont;
35 non terminal ParseNode reqlist, require, capintlist;
38 * A policy file normally consists of:
40 * - Interface definition
41 * - List of capabilities and their contents
43 * We also define "requires" statements for users
44 * to declare their required capabilities in the
45 * generated interfaces
46 * 2) List of generated interfaces (requires)
50 ParseNode pn = new ParseNode("policy");
56 ParseNode pn = new ParseNode("policy");
62 //1) Interface class definition
63 // 1) Interface definition
68 intface ::= PUBLIC INTERFACE IDENT:idint BEGIN methlist:ml capablist:cl END
70 ParseNode pn = new ParseNode("interface");
71 pn.addChild("intface_ident").setLiteral(idint);
77 methlist ::= methlist:ml meth:m
84 ParseNode pn = new ParseNode("method_list");
88 meth ::= PUBLIC TYPE:typemeth IDENT:idmeth LPAR paramlist:pl RPAR SEMICOLON
90 ParseNode pn = new ParseNode("method");
91 pn.addChild("method_type").setLiteral(typemeth);
92 pn.addChild("method_ident").setLiteral(idmeth);
96 | PUBLIC IDENT:clsmeth IDENT:idmeth LPAR paramlist:pl RPAR SEMICOLON
98 ParseNode pn = new ParseNode("method");
99 pn.addChild("method_class").setLiteral(clsmeth);
100 pn.addChild("method_ident").setLiteral(idmeth);
105 paramlist ::= paramlist:pl param:p
112 ParseNode pn = new ParseNode("param_list");
116 param ::= TYPE:typeprm IDENT:idprm COMMA
118 ParseNode pn = new ParseNode("param");
119 pn.addChild("param_type").setLiteral(typeprm);
120 pn.addChild("param_ident").setLiteral(idprm);
123 | TYPE:typeprm IDENT:idprm
125 ParseNode pn = new ParseNode("param");
126 pn.addChild("param_type").setLiteral(typeprm);
127 pn.addChild("param_ident").setLiteral(idprm);
130 | IDENT:clsprm IDENT:idprm COMMA
132 ParseNode pn = new ParseNode("param");
133 pn.addChild("param_class").setLiteral(clsprm);
134 pn.addChild("param_ident").setLiteral(idprm);
137 | IDENT:clsprm IDENT:idprm
139 ParseNode pn = new ParseNode("param");
140 pn.addChild("param_class").setLiteral(clsprm);
141 pn.addChild("param_ident").setLiteral(idprm);
146 //2) List of capabilities and their respective contents, i.e. description, method, etc.
147 capablist ::= capablist:cl capab:cap
154 ParseNode pn = new ParseNode("capab_list");
158 capab ::= CAPABILITY IDENT:idcap BEGIN capabcont:ccont END
160 ParseNode pn = new ParseNode("capability");
161 pn.addChild("capab_ident").setLiteral(idcap);
166 capabcont ::= capabcont:ccont cont:cnt
173 ParseNode pn = new ParseNode("capab_content");
177 cont ::= DESCRIPTION:dsc ASSIGN STRINGCONST:strdsc SEMICOLON
179 ParseNode pn = new ParseNode("capab_content");
180 pn.addChild("capab_desc").setLiteral(strdsc);
183 | METHOD:mtd ASSIGN STRINGCONST:strmeth SEMICOLON
185 ParseNode pn = new ParseNode("capab_content");
186 pn.addChild("capab_meth").setLiteral(strmeth);
191 //3) List of interface generation definitions ("requires" statements)
192 reqlist ::= reqlist:rl require:req
199 ParseNode pn = new ParseNode("reqlist");
203 require ::= REQUIRES IDENT:idint WITH capintlist:cil AS INTERFACE IDENT:idnewint SEMICOLON
205 ParseNode pn = new ParseNode("requires");
206 pn.addChild("intface_ident").setLiteral(idint);
208 pn.addChild("new_intface_ident").setLiteral(idnewint);
212 capintlist ::= IDENT:idcap
214 ParseNode pn = new ParseNode("capab_ident_list");
215 pn.addChild("capab_ident").setLiteral(idcap);
218 | capintlist:cil COMMA IDENT:idcap
220 cil.addChild("capab_ident").setLiteral(idcap);
225 ParseNode pn = new ParseNode("capab_ident_list");