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;
25 /*XMLElement e = (XMLElement)p.parse().value;
26 // create XML output file
27 XMLOutputFactory outFactory = XMLOutputFactory.newInstance();
28 XMLStreamWriter sw = outFactory.createXMLStreamWriter(new FileOutputStream(args[1]), "UTF-8");
29 // dump XML output to the file
30 XMLElement.dump(lexer,sw,e,"expr","stmt");
32 // transform the parse tree into an AST and a rendered HTML version
33 Transformer transformer = TransformerFactory.newInstance()
34 .newTransformer(new StreamSource(new File("tree.xsl")));
35 Source text = new StreamSource(new File(args[1]));
36 transformer.transform(text, new StreamResult(new File("output.xml")));
37 transformer = TransformerFactory.newInstance()
38 .newTransformer(new StreamSource(new File("tree-view.xsl")));
39 text = new StreamSource(new File("output.xml"));
40 transformer.transform(text, new StreamResult(new File("ast.html")));*/
44 terminal SEMICOLON, COMMA, DOT, LPAR, RPAR, BEGIN, END, ASSIGN;
45 terminal PUBLIC, INTERFACE, CAPABILITY, DESCRIPTION, METHOD, REQUIRES, WITH, AS;
47 terminal IDENT, STRINGCONST;
49 non terminal ParseNode policy, intface, methlist, meth, paramlist, param;
50 non terminal ParseNode capablist, capab, capabcont, cont;
51 non terminal ParseNode reqlist, require, capintlist;
54 * A policy file normally consists of 3 parts:
55 * 1) Interface (in Java syntax)
56 * 2) List of capabilities and their contents
57 * 3) List of interface generation definitions
60 intface:in capablist:cap reqlist:rl
62 ParseNode pn = new ParseNode("policy");
70 // Interface class definition
71 intface ::= PUBLIC INTERFACE IDENT:idint BEGIN methlist:ml END
73 ParseNode pn = new ParseNode("interface");
74 pn.addChild("intface_ident").setLiteral(idint);
80 ParseNode pn = new ParseNode("interface");
84 methlist ::= methlist:ml meth:m
91 ParseNode pn = new ParseNode("method_list");
95 meth ::= PUBLIC TYPE:typemeth IDENT:idmeth LPAR paramlist:pl RPAR SEMICOLON
97 ParseNode pn = new ParseNode("method");
98 pn.addChild("method_type").setLiteral(typemeth);
99 pn.addChild("method_ident").setLiteral(idmeth);
104 paramlist ::= paramlist:pl param:p
111 ParseNode pn = new ParseNode("param_list");
115 param ::= TYPE:typeprm IDENT:idprm COMMA
117 ParseNode pn = new ParseNode("param");
118 pn.addChild("param_type").setLiteral(typeprm);
119 pn.addChild("param_ident").setLiteral(idprm);
122 | TYPE:typeprm IDENT:idprm
124 ParseNode pn = new ParseNode("param");
125 pn.addChild("param_type").setLiteral(typeprm);
126 pn.addChild("param_ident").setLiteral(idprm);
129 | IDENT:clsprm IDENT:idprm COMMA
131 ParseNode pn = new ParseNode("param");
132 pn.addChild("param_class").setLiteral(clsprm);
133 pn.addChild("param_ident").setLiteral(idprm);
136 | IDENT:clsprm IDENT:idprm
138 ParseNode pn = new ParseNode("param");
139 pn.addChild("param_class").setLiteral(clsprm);
140 pn.addChild("param_ident").setLiteral(idprm);
145 // List of capabilities and their respective contents, i.e. description, method, etc.
146 capablist ::= capablist:clist capab:cap
153 ParseNode pn = new ParseNode("capab_list");
157 capab ::= CAPABILITY IDENT:idint DOT IDENT:idcap BEGIN capabcont:ccont END
159 ParseNode pn = new ParseNode("capability");
160 pn.addChild("intface_ident").setLiteral(idint);
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:str SEMICOLON
179 ParseNode pn = new ParseNode("capab_content");
180 pn.addChild("capab_desc").setLiteral(str);
183 | METHOD:mtd ASSIGN IDENT:idmeth SEMICOLON
185 ParseNode pn = new ParseNode("capab_content");
186 pn.addChild("capab_ident").setLiteral(idmeth);
191 // List of interface generation definitions
192 reqlist ::= reqlist:rl require:req
199 ParseNode pn = new ParseNode("requires_list");
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");