1 /* spec-compiler.jj Grammer definition for the specification */
6 Each construct should be embraced by /DOUBLE_STAR ... STAR/ annotation.
7 Within there, any line beginning with a "#" is a comment of the annotation.
8 Each constrcut should begin with @Begin and end with @End. Otherwise, the
9 annotation would be considered as normal comments of the source.
14 # If LANG is not define, it's C++ by default. C does not support class
15 # and template, so if it's defined as C, we should also have a explicit
29 b) Interface construct
33 IDENTIFIER | IDENTIFIER ...
34 @Condition: ... (Optional)
36 IDENTIFIER :: <C_CPP_Condition>
38 @ID: ... (Optional, use default ID)
42 @DefineVar: Type var1 = SomeExpression (Optional)
45 @Post_action: (Optional)
46 @Post_check: (Optional)
49 c) Potential commit construct
51 @Potential_commit_point_define: ...
55 d) Commit point define construct
57 @Commit_point_define_check: ...
64 @Commit_point_define: ...
65 @Potential_commit_point_label: ...
69 e) Entry point construct
74 f) Interface define construct
76 @Interface_define: <Interface_Name>
84 JAVA_UNICODE_ESCAPE = true;
87 PARSER_BEGIN(SpecParser)
88 package edu.uci.eecs.specCompiler.grammerParser;
90 import java.io.FileInputStream;
91 import java.io.FileNotFoundException;
92 import java.io.InputStream;
93 import java.io.ByteArrayInputStream;
94 import java.util.ArrayList;
95 import java.util.HashMap;
96 import java.util.HashSet;
98 import edu.uci.eecs.specCompiler.specExtraction.Construct;
99 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
100 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
101 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
102 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
103 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
104 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
105 import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct;
106 import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct.DefineVar;
107 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
108 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
109 import edu.uci.eecs.specCompiler.specExtraction.EntryPointConstruct;
111 public class SpecParser {
112 public static void main(String[] argvs)
113 throws ParseException, TokenMgrError {
115 FileInputStream fis = new FileInputStream("./grammer/spec.txt");
116 SpecParser parser = new SpecParser(fis);
118 System.out.println("Parsing finished!");
119 } catch (FileNotFoundException e) {
124 public static Construct parseSpec(String text)
125 throws ParseException, TokenMgrError {
126 InputStream input = new ByteArrayInputStream(text.getBytes());
127 SpecParser parser = new SpecParser(input);
128 return parser.Parse();
133 PARSER_END(SpecParser)
147 // "#" comment for the specification
148 <"#" (~["\n", "\r"])* (["\n", "\r"])>
150 // "//" comment for the specification
151 <"//" (~["\n", "\r"])* (["\n", "\r"])>
156 /* Above are specification-only tokens */
165 <OPTIONS: "@Options:">
167 <GLOBAL_DEFINE: "@Global_define:">
169 <DECLARE_VAR: "@DeclareVar:">
171 <INIT_VAR: "@InitVar:">
173 <DEFINE_FUNC: "@DefineFunc:">
175 <INTERFACE_CLUSTER: "@Interface_cluster:">
177 <HAPPENS_BEFORE: "@Happens_before:">
179 <INTERFACE: "@Interface:">
181 <COMMIT_POINT_SET: "@Commit_point_set:">
183 <ENTRY_POINT: "@Entry_point">
185 <INTERFACE_DEFINE: "@Interface_define:">
187 <CONDITION: "@Condition:">
189 <HB_CONDITION: "@HB_condition:">
197 <DEFINEVAR: "@DefineVar:">
201 <POST_ACTION: "@Post_action:">
203 <POST_CHECK: "@Post_check:">
205 <POTENTIAL_COMMIT_POINT_DEFINE: "@Potential_commit_point_define:">
209 <COMMIT_POINT_DEFINE_CHECK: "@Commit_point_define_check:">
211 <COMMIT_POINT_DEFINE: "@Commit_point_define:">
213 <POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
216 /* Specification & C/C++ shared tokens */
220 <#LETTER: ["a"-"z", "A"-"Z"]>
222 <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
239 /* C/C++ only token*/
270 <GREATER_EQUALS: ">=">
274 <LOGICAL_EQUALS: "==">
294 ((~["\"","\\","\n","\r"])
296 ( ["n","t","b","r","f","\\","'","\""]
297 | ["0"-"7"] ( ["0"-"7"] )?
298 | ["0"-"3"] ["0"-"7"]
307 ((~["'","\\","\n","\r"])
309 (["n","t","b","r","f","\\","'","\""]
310 | ["0"-"7"] ( ["0"-"7"] )?
311 | ["0"-"3"] ["0"-"7"]
319 <DECIMAL_LITERAL> (["l","L"])?
320 | <HEX_LITERAL> (["l","L"])?
321 | <OCTAL_LITERAL> (["l","L"])?>
323 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
325 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
327 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
329 < FLOATING_POINT_LITERAL:
330 <DECIMAL_FLOATING_POINT_LITERAL>
331 | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
333 < #DECIMAL_FLOATING_POINT_LITERAL:
334 (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
335 | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
336 | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
337 | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
339 < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
341 < #HEXADECIMAL_FLOATING_POINT_LITERAL:
342 "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
343 | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
345 < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
354 LOOKAHEAD(3) res = Global_construct() |
355 LOOKAHEAD(3) res = Interface() |
356 LOOKAHEAD(3) res = Potential_commit_point_define() |
357 LOOKAHEAD(3) res = Commit_point_define() |
358 LOOKAHEAD(3) res = Commit_point_define_check() |
359 LOOKAHEAD(3) res = Entry_point() |
360 LOOKAHEAD(3) res = Interface_define()
364 //System.out.println(res);
369 GlobalConstruct Global_construct() :
372 SequentialDefineSubConstruct code;
373 HashMap<String, String> options;
379 options = new HashMap<String, String>();
384 ((key = <IDENTIFIER>.image)
386 (value = <IDENTIFIER>.image)
388 if (options.containsKey(key)) {
389 throw new ParseException("Duplicate options!");
391 options.put(key, value);
396 (code = Global_define())
397 { res = new GlobalConstruct(code, options); }
398 (Interface_clusters(res))?
399 (Happens_before(res))?
403 res.unfoldInterfaceCluster();
408 String C_CPP_CODE() :
415 text = new StringBuilder();
421 t = <IDENTIFIER> | t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> |
422 t = <OPEN_BRACKET> | t = <CLOSE_BRACKET> | t = <HB_SYMBOL> | t = <COMMA> |
423 t = <DOT> | t = <STAR> | t = <NEGATE> | t = <EXCLAMATION> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
424 t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> |
425 t = <LESS_THAN> | t = <GREATER_THAN> | t = <GREATER_EQUALS> | t = <LESS_EQUALS> |
426 t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR> |
427 t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
428 t = <SEMI_COLON> | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
429 t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL>
432 text.append(t.image);
433 if (t.image.equals(";") || t.image.equals("\\")
434 || t.image.equals("{") || t.image.equals("}"))
441 //System.out.println(text);
442 return text.toString();
446 SequentialDefineSubConstruct Global_define() :
448 String declareVar, initVar, defineFunc;
457 (<DECLARE_VAR> (declareVar = C_CPP_CODE()))?
458 (<INIT_VAR> (initVar = C_CPP_CODE()))?
459 (<DEFINE_FUNC> (defineFunc = C_CPP_CODE()))?
461 SequentialDefineSubConstruct res = new SequentialDefineSubConstruct(declareVar, initVar, defineFunc);
462 //System.out.println(res);
467 ConditionalInterface Conditional_interface() :
469 String interfaceName, hbConditionLabel;
473 hbConditionLabel = "";
475 interfaceName = <IDENTIFIER>.image (<OPEN_BRACKET> hbConditionLabel =
476 <IDENTIFIER>.image <CLOSE_BRACKET>)?
478 return new ConditionalInterface(interfaceName, hbConditionLabel);
482 void Interface_cluster(GlobalConstruct inst) :
485 ConditionalInterface condInterface;
488 (clusterName= <IDENTIFIER>.image)
489 <EQUALS> <OPEN_PAREN>
490 (condInterface = Conditional_interface()
491 { inst.addInterface2Cluster(clusterName, condInterface); }
493 (<COMMA> condInterface = Conditional_interface()
494 { inst.addInterface2Cluster(clusterName, condInterface); }
499 void Interface_clusters(GlobalConstruct inst) :
502 <INTERFACE_CLUSTER> (Interface_cluster(inst))+
505 void Happens_before(GlobalConstruct inst) :
507 ConditionalInterface left, right;
512 left = Conditional_interface() <HB_SYMBOL> right = Conditional_interface()
513 { inst.addHBCondition(left, right); }
517 InterfaceConstruct Interface() :
519 InterfaceConstruct res;
520 String interfaceName, condition, idCode, check, postAction,
521 postCheck, commitPoint, hbLabel, hbCondition;
522 ActionSubConstruct action;
523 ArrayList<String> commitPointSet;
524 HashMap<String, String> hbConditions;
535 commitPointSet = new ArrayList<String>();
536 hbConditions = new HashMap<String, String>();
540 <INTERFACE> (interfaceName = <IDENTIFIER>.image)
542 (commitPoint = <IDENTIFIER>.image
543 { commitPointSet.add(commitPoint); }
546 (commitPoint = <IDENTIFIER>.image)
548 if (commitPointSet.contains(commitPoint)) {
549 throw new ParseException(interfaceName + " has" +
550 "duplicate commit point labels");
552 commitPointSet.add(commitPoint);
556 (<CONDITION> (condition = C_CPP_CODE()))?
559 (hbLabel = <IDENTIFIER>.image)
560 (hbCondition = C_CPP_CODE())
562 if (hbConditions.containsKey(hbLabel)) {
563 throw new ParseException(interfaceName + " has" +
564 "duplicate happens-before condtion labels");
566 hbConditions.put(hbLabel, hbCondition);
569 (<ID> (idCode = C_CPP_CODE()))?
570 (<CHECK> (check = C_CPP_CODE()))?
572 (<POST_ACTION> (postAction = C_CPP_CODE()))?
573 (<POST_CHECK> (postCheck = C_CPP_CODE()))?
577 res = new InterfaceConstruct(interfaceName, commitPointSet, condition,
578 hbConditions, idCode, check, action, postAction, postCheck);
583 ActionSubConstruct Action() :
585 String type, name, expr, defineVarStr, code;
586 ArrayList<DefineVar> defineVars;
590 defineVars = new ArrayList<DefineVar>();
596 (<DEFINEVAR> (defineVarStr = C_CPP_CODE())
598 int eqIdx = defineVarStr.indexOf('=');
599 int typeEnd = defineVarStr.lastIndexOf(' ', eqIdx - 2);
600 type = defineVarStr.substring(0, typeEnd);
601 name = defineVarStr.substring(typeEnd + 1, eqIdx - 1);
602 expr = defineVarStr.substring(eqIdx + 2);
603 DefineVar defineVar = new DefineVar(type, name, expr);
604 defineVars.add(defineVar);
605 })* (<CODE> (code = C_CPP_CODE()))? )
609 ActionSubConstruct res = new ActionSubConstruct(defineVars, code);
614 PotentialCPDefineConstruct Potential_commit_point_define() :
616 PotentialCPDefineConstruct res;
617 String label, condition;
624 <POTENTIAL_COMMIT_POINT_DEFINE> (condition = C_CPP_CODE())
625 <LABEL> (label = <IDENTIFIER>.image)
629 res = new PotentialCPDefineConstruct(label, condition);
635 CPDefineConstruct Commit_point_define() :
637 CPDefineConstruct res;
638 String label, potentialCPLabel, condition;
645 <COMMIT_POINT_DEFINE> (condition = C_CPP_CODE())
646 <POTENTIAL_COMMIT_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
647 <LABEL> (label = <IDENTIFIER>.image)
651 res = new CPDefineConstruct(label, potentialCPLabel, condition);
657 CPDefineCheckConstruct Commit_point_define_check() :
659 CPDefineCheckConstruct res;
660 String label, condition;
667 <COMMIT_POINT_DEFINE_CHECK> (condition = C_CPP_CODE())
668 <LABEL> (label = <IDENTIFIER>.image)
672 res = new CPDefineCheckConstruct(label, condition);
677 EntryPointConstruct Entry_point() :
687 return new EntryPointConstruct();
691 InterfaceDefineConstruct Interface_define() :
698 <INTERFACE_DEFINE> (name = <IDENTIFIER>.image)
702 return new InterfaceDefineConstruct(name);