options { STATIC = false; JAVA_UNICODE_ESCAPE = true; } PARSER_BEGIN(Parser) import edu.neu.ccs.demeter.*; public class Parser { // oit is uugly. Why isn't there a Character.valueOf(String)? static char unescapifyChar(String s) { char c = s.charAt(0); if (c == '\\') { switch (s.charAt(1)) { case 'n': c = '\n'; break; case 't': c = '\t'; break; case 'b': c = '\b'; break; case 'r': c = '\r'; break; case 'f': c = '\f'; break; case '\\': c = '\\'; break; case '\'': c = '\''; break; case '\"': c = '\"'; break; default: c = (char) Integer.parseInt(s.substring(1, s.length()), 8); break; } } return c; } // Even uglier... static String unescapify(String s) { char str[] = new char[s.length()]; int i = 0, o = 0; while (i < s.length()) { char c = s.charAt(i++); if (c == '\\') { int j = i + 1; while (j < s.length() && Character.digit(s.charAt(j), 8) != -1) { j++; } c = unescapifyChar(s.substring(i-1, j)); i = j; } str[o++] = c; } return String.valueOf(str, 0, o); } } PARSER_END(Parser) Test _Test() : { Test it = null; MetaGameInfo _metagameinfo; IteratedGameHistory_List _iteratedgamehistory_list; } { { it=new Test(); } _metagameinfo=_MetaGameInfo() { it.set_metagameinfo(_metagameinfo); } "games" _iteratedgamehistory_list=_IteratedGameHistory_List() { it.set_iteratedgamehistory_list(_iteratedgamehistory_list); } { return it; } } IteratedGameHistory _IteratedGameHistory() : { IteratedGameHistory it = null; BasicGameStep_List _history; } { { it=new IteratedGameHistory(); } "(game" _history=_BasicGameStep_List() { it.set_history(_history); } ")" { return it; } } BasicGameStep _BasicGameStep() : { BasicGameStep it = null; RelationSelection _relationselection; FirstPlayer _firstplayer; SecondPlayer _secondplayer; NegOp _negop; float _payment; } { { it=new BasicGameStep(); } "basicGame" _relationselection=_RelationSelection() { it.set_relationselection(_relationselection); } _firstplayer=_FirstPlayer() { it.set_firstplayer(_firstplayer); } _secondplayer=_SecondPlayer() { it.set_secondplayer(_secondplayer); } [ _negop=_NegOp() { it.set_negop(_negop); } ] _payment=_float() { it.set_payment(_payment); } { return it; } } AbsPlayer _AbsPlayer() : { AbsPlayer it = null; } { ( it=_FirstPlayer() | it=_SecondPlayer() ) { return it; } } void common_AbsPlayer(AbsPlayer it) : { Player _player; } { _player=_Player() { it.set_player(_player); } { } } FirstPlayer _FirstPlayer() : { FirstPlayer it = null; CSP _instance; } { { it=new FirstPlayer(); } "csp" _instance=_CSP() { it.set_instance(_instance); } common_AbsPlayer(it) { return it; } } SecondPlayer _SecondPlayer() : { SecondPlayer it = null; Literal_List _literal_list; } { { it=new SecondPlayer(); } "assignment" _literal_list=_Literal_List() { it.set_literal_list(_literal_list); } common_AbsPlayer(it) { return it; } } CSP _CSP() : { CSP it = null; } { { it=new CSP(); } "done" { return it; } } Literal _Literal() : { Literal it = null; } { ( it=_Pos() | it=_Neg() ) { return it; } } void common_Literal(Literal it) : { Variable _variable; } { _variable=_Variable() { it.set_variable(_variable); } { } } Pos _Pos() : { Pos it = null; } { { it=new Pos(); } common_Literal(it) { return it; } } Neg _Neg() : { Neg it = null; } { { it=new Neg(); } "!" common_Literal(it) { return it; } } RelationSelection _RelationSelection() : { RelationSelection it = null; RelationNumber _r1; RelationNumber _r2; } { { it=new RelationSelection(); } "R1" _r1=_RelationNumber() { it.set_r1(_r1); } "R2" _r2=_RelationNumber() { it.set_r2(_r2); } { return it; } } Player _Player() : { Player it = null; Name _name; NegOp _negop; float _gainLoss; } { { it=new Player(); } _name=_Name() { it.set_name(_name); } [ _negop=_NegOp() { it.set_negop(_negop); } ] _gainLoss=_float() { it.set_gainLoss(_gainLoss); } { return it; } } NegOp _NegOp() : { NegOp it = null; } { { it=new NegOp(); } "-" { return it; } } MetaGameInfo _MetaGameInfo() : { MetaGameInfo it = null; MPlayer _player1; MPlayer _player2; RelationSelectionPolicy _rsp; } { { it=new MetaGameInfo(); } "metaInfo" _player1=_MPlayer() { it.set_player1(_player1); } _player2=_MPlayer() { it.set_player2(_player2); } _rsp=_RelationSelectionPolicy() { it.set_rsp(_rsp); } { return it; } } RelationSelectionPolicy _RelationSelectionPolicy() : { RelationSelectionPolicy it = null; RspName _rspName; } { { it=new RelationSelectionPolicy(); } _rspName=_RspName() { it.set_rspName(_rspName); } { return it; } } MPlayer _MPlayer() : { MPlayer it = null; Solver _solver; InstanceSelection _rsp; } { { it=new MPlayer(); } _solver=_Solver() { it.set_solver(_solver); } _rsp=_InstanceSelection() { it.set_rsp(_rsp); } { return it; } } Solver _Solver() : { Solver it = null; SolverName _solverName; } { { it=new Solver(); } _solverName=_SolverName() { it.set_solverName(_solverName); } { return it; } } InstanceSelection _InstanceSelection() : { InstanceSelection it = null; InstSelName _instSelName; } { { it=new InstanceSelection(); } _instSelName=_InstSelName() { it.set_instSelName(_instSelName); } { return it; } } SolverName _SolverName() : { SolverName it = null; String _v; } { { it=new SolverName(); } _v=_String() { it.set_v(_v); } { return it; } } RspName _RspName() : { RspName it = null; String _v; } { { it=new RspName(); } _v=_String() { it.set_v(_v); } { return it; } } InstSelName _InstSelName() : { InstSelName it = null; String _string; } { { it=new InstSelName(); } _string=_String() { it.set_string(_string); } { return it; } } Name _Name() : { Name it = null; Ident _v; } { { it=new Name(); } _v=_Ident() { it.set_v(_v); } { return it; } } RelationNumber _RelationNumber() : { RelationNumber it = null; int _v; } { { it=new RelationNumber(); } _v=_int() { it.set_v(_v); } { return it; } } Variable _Variable() : { Variable it = null; int _v; } { { it=new Variable(); } _v=_int() { it.set_v(_v); } { return it; } } Main _Main() : { Main it = null; } { { it=new Main(); } { return it; } } IteratedGameHistory_List _IteratedGameHistory_List() : { IteratedGameHistory_List it = null; Nonempty_IteratedGameHistory_List _first; } { { it=new IteratedGameHistory_List(); } [ _first=_Nonempty_IteratedGameHistory_List() { it.set_first(_first); } ] { return it; } } BasicGameStep_List _BasicGameStep_List() : { BasicGameStep_List it = null; Nonempty_BasicGameStep_List _first; } { { it=new BasicGameStep_List(); } [ _first=_Nonempty_BasicGameStep_List() { it.set_first(_first); } ] { return it; } } Literal_List _Literal_List() : { Literal_List it = null; Nonempty_Literal_List _first; } { { it=new Literal_List(); } [ _first=_Nonempty_Literal_List() { it.set_first(_first); } ] { return it; } } Nonempty_IteratedGameHistory_List _Nonempty_IteratedGameHistory_List() : { Nonempty_IteratedGameHistory_List it = null; IteratedGameHistory _it; Nonempty_IteratedGameHistory_List _next; } { { it=new Nonempty_IteratedGameHistory_List(); } _it=_IteratedGameHistory() { it.set_it(_it); } [ _next=_Nonempty_IteratedGameHistory_List() { it.set_next(_next); } ] { return it; } } Nonempty_BasicGameStep_List _Nonempty_BasicGameStep_List() : { Nonempty_BasicGameStep_List it = null; BasicGameStep _it; Nonempty_BasicGameStep_List _next; } { { it=new Nonempty_BasicGameStep_List(); } _it=_BasicGameStep() { it.set_it(_it); } [ _next=_Nonempty_BasicGameStep_List() { it.set_next(_next); } ] { return it; } } Nonempty_Literal_List _Nonempty_Literal_List() : { Nonempty_Literal_List it = null; Literal _it; Nonempty_Literal_List _next; } { { it=new Nonempty_Literal_List(); } _it=_Literal() { it.set_it(_it); } [ _next=_Nonempty_Literal_List() { it.set_next(_next); } ] { return it; } } boolean _boolean() : { Token t; }{ ( t= { return true; } | t= { return false; } ) } char _char() : { Token t; } { t= { String s = t.image; return unescapifyChar(s.substring(1, s.length()-1)); } } byte _byte() : { int i; } { i=_int() { return (byte) i; } } short _short() : { int i; } { i=_int() { return (short) i; } } int _int() : { Number num; } { num=_Number() { return num.intValue(); } } long _long() : { Number num; } { num=_Number() { return num.longValue(); } } float _float() : { Number num; } { num=_Number() { return num.floatValue(); } } double _double() : { Number num; } { num=_Number() { return num.doubleValue(); } } Boolean _Boolean() : { Token t; }{ ( t= { return Boolean.TRUE; } | t= { return Boolean.FALSE; } ) } Character _Character() : { char c; } { c=_char() { return new Character(c); } } Integer _Integer() : { int i; } { i = _int() { return new Integer(i); } } Long _Long() : { long l; } { l=_long() { return new Long(l); } } Float _Float() : { float f; } { f=_float() { return new Float(f); } } Double _Double() : { double d; } { d=_double() { return new Double(d); } } Number _Number() : { Token t; String s = null; int radix = 0; Number num = null; } { ( ( t= { s = t.image; radix = 10; } | t= { // Strip off the "0x". s = t.image.substring(2, t.image.length()); radix = 16; } | t= { s = t.image; radix = 8; } ) { switch (s.charAt(s.length()-1)) { case 'l': case 'L': s = s.substring(0, s.length()-1); num = new Long(new java.math.BigInteger(s, radix).longValue()); break; default: num = new Integer(new java.math.BigInteger(s, radix).intValue()); break; } } | t= { s = t.image; switch (s.charAt(s.length()-1)) { case 'd': case 'D': num = Double.valueOf(s.substring(0, s.length()-1)); break; case 'f': case 'F': num = Float.valueOf(s.substring(0, s.length()-1)); break; default: num = Float.valueOf(s); break; } } ) { return num; } } String _String() : { Token t; } { t= { String s = t.image; return unescapify(s.substring(1, s.length()-1)); } } StringBuffer _StringBuffer() : { String s; } { s=_String() { return new StringBuffer(s); } } Ident _Ident() : { Token t; } { t= { return new Ident(t.image); } } Text _Text() : { Token t; } { t= { String s = t.image; return new Text(s.substring(2, s.length()-2)); } } Line _Line() : { Token t; } { { token_source.SwitchTo(1); } t= { return new Line(t.image); } } Word _Word() : { Token t; } { { token_source.SwitchTo(2); } t= { return new Word(t.image); } } // Lexical specification (largely taken from Java.jack): SKIP : { " " | "\t" | "\n" | "\r" | <"//" (~["\n","\r"])* ("\n"|"\r\n")> | <"/*" (~["*"])* "*" (~["/"] (~["*"])* "*")* "/"> } TOKEN : { /* LITERALS */ < DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* (["l","L"])? > | < HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ (["l","L"])? > | < OCTAL_LITERAL: "0" (["0"-"7"])* (["l","L"])? > | < FLOATING_POINT_LITERAL: (["0"-"9"])+ "." (["0"-"9"])+ ()? (["f","F","d","D"])? | "." (["0"-"9"])+ ()? (["f","F","d","D"])? | (["0"-"9"])+ (["f","F","d","D"])? | (["0"-"9"])+ ()? ["f","F","d","D"] > | < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ > | < CHARACTER_LITERAL: "'" ( (~["\'","\\","\n","\r"]) | ("\\" ( ["n","t","b","r","f","\\","\'","\""] | ["0"-"7"] ( ["0"-"7"] )? | ["0"-"3"] ["0"-"7"] ["0"-"7"] ) ) ) "'" > | < STRING_LITERAL: "\"" ( (~["\"","\\","\n","\r"]) | ("\\" ( ["n","t","b","r","f","\\","\'","\""] | ["0"-"7"] ( ["0"-"7"] )? | ["0"-"3"] ["0"-"7"] ["0"-"7"] ) ) )* "\"" > | < TEXT_LITERAL: ( "(@" (~["@"])* ( "@" ~[")"] (~["@"])* )* "@)" ) | ( "{{" (~["}"])* ( "}" ~["}"] (~["}"])* )* "}}" ) > | < TRUE: "true" > | < FALSE: "false" > } TOKEN : { /* IDENTIFIERS */ < IDENTIFIER: (|)* > | < #LETTER: [ "\u0024", "\u0041"-"\u005a", "\u005f", "\u0061"-"\u007a", "\u00c0"-"\u00d6", "\u00d8"-"\u00f6", "\u00f8"-"\u00ff", "\u0100"-"\u1fff", "\u3040"-"\u318f", "\u3300"-"\u337f", "\u3400"-"\u3d2d", "\u4e00"-"\u9fff", "\uf900"-"\ufaff" ] > | < #DIGIT: [ "\u0030"-"\u0039", "\u0660"-"\u0669", "\u06f0"-"\u06f9", "\u0966"-"\u096f", "\u09e6"-"\u09ef", "\u0a66"-"\u0a6f", "\u0ae6"-"\u0aef", "\u0b66"-"\u0b6f", "\u0be7"-"\u0bef", "\u0c66"-"\u0c6f", "\u0ce6"-"\u0cef", "\u0d66"-"\u0d6f", "\u0e50"-"\u0e59", "\u0ed0"-"\u0ed9", "\u1040"-"\u1049" ] > } TOKEN : { < LINE: (~["\n","\r"])* > : DEFAULT } SKIP : { " " | "\t" | "\n" | "\r" } TOKEN : { < WORD: (~[" ","\t","\n","\r"])* > : DEFAULT }