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) Main _Main() : { Main it = null; String _s; } { { it=new Main(); } _s=_String() { it.set_s(_s); } { return it; } } Program _Program() : { Program it = null; AdaptiveMethod_L _adaptivemethod_l; } { { it=new Program(); } _adaptivemethod_l=_AdaptiveMethod_L() { it.set_adaptivemethod_l(_adaptivemethod_l); } { return it; } } AdaptiveMethod _AdaptiveMethod() : { AdaptiveMethod it = null; CD _cd; OG _og; Strategy _strategy; Visitor _visitor; } { { it=new AdaptiveMethod(); } "(program" _cd=_CD() { it.set_cd(_cd); } _og=_OG() { it.set_og(_og); } _strategy=_Strategy() { it.set_strategy(_strategy); } _visitor=_Visitor() { it.set_visitor(_visitor); } ")" { return it; } } CD _CD() : { CD it = null; DD_PL _dd_pl; } { { it=new CD(); } _dd_pl=_DD_PL() { it.set_dd_pl(_dd_pl); } { return it; } } DD _DD() : { DD it = null; TypeName _typename; Alternative_L _alternative_l; } { { it=new DD(); } "(datatype" _typename=_TypeName() { it.set_typename(_typename); } _alternative_l=_Alternative_L() { it.set_alternative_l(_alternative_l); } ")" { return it; } } Alternative _Alternative() : { Alternative it = null; AlternativeName _alternativename; TypedField_L _typedfield_l; } { { it=new Alternative(); } "(" _alternativename=_AlternativeName() { it.set_alternativename(_alternativename); } _typedfield_l=_TypedField_L() { it.set_typedfield_l(_typedfield_l); } ")" { return it; } } TypedField _TypedField() : { TypedField it = null; FieldName _fieldname; TypeName _typename; } { { it=new TypedField(); } "(" _fieldname=_FieldName() { it.set_fieldname(_fieldname); } _typename=_TypeName() { it.set_typename(_typename); } ")" { return it; } } FieldName _FieldName() : { FieldName it = null; Ident _ident; } { { it=new FieldName(); } _ident=_Ident() { it.set_ident(_ident); } { return it; } } TypeName _TypeName() : { TypeName it = null; Ident _ident; } { { it=new TypeName(); } _ident=_Ident() { it.set_ident(_ident); } { return it; } } AlternativeName _AlternativeName() : { AlternativeName it = null; Ident _ident; } { { it=new AlternativeName(); } _ident=_Ident() { it.set_ident(_ident); } { return it; } } OG _OG() : { OG it = null; } { ( it=_Numerical() | it=_Textual() | it=_ConstrCall() ) { return it; } } void common_OG(OG it) : { } { { } } Numerical _Numerical() : { Numerical it = null; Number _number; } { { it=new Numerical(); } _number=_Number() { it.set_number(_number); } common_OG(it) { return it; } } Textual _Textual() : { Textual it = null; String _string; } { { it=new Textual(); } _string=_String() { it.set_string(_string); } common_OG(it) { return it; } } ConstrCall _ConstrCall() : { ConstrCall it = null; AlternativeName _alternativename; OG_L _ogs; } { { it=new ConstrCall(); } "(" _alternativename=_AlternativeName() { it.set_alternativename(_alternativename); } _ogs=_OG_L() { it.set_ogs(_ogs); } ")" common_OG(it) { return it; } } Strategy _Strategy() : { Strategy it = null; } { ( it=_SimpleStrategy() | it=_CompoundStrategy() ) { return it; } } void common_Strategy(Strategy it) : { } { { } } SimpleStrategy _SimpleStrategy() : { SimpleStrategy it = null; TypeName _source; TypeName _target; } { { it=new SimpleStrategy(); } "(from-to" _source=_TypeName() { it.set_source(_source); } _target=_TypeName() { it.set_target(_target); } ")" common_Strategy(it) { return it; } } CompoundStrategy _CompoundStrategy() : { CompoundStrategy it = null; SOp _sop; Strategy _arg1; Strategy _arg2; } { { it=new CompoundStrategy(); } "(" _sop=_SOp() { it.set_sop(_sop); } _arg1=_Strategy() { it.set_arg1(_arg1); } _arg2=_Strategy() { it.set_arg2(_arg2); } ")" common_Strategy(it) { return it; } } SOp _SOp() : { SOp it = null; } { ( it=_Join() | it=_Merge() ) { return it; } } void common_SOp(SOp it) : { } { { } } Join _Join() : { Join it = null; } { { it=new Join(); } "join" common_SOp(it) { return it; } } Merge _Merge() : { Merge it = null; } { { it=new Merge(); } "merge" common_SOp(it) { return it; } } Visitor _Visitor() : { Visitor it = null; VisName _visname; Expression _expression; FinishAction _finishaction; Action_L _action_l; } { { it=new Visitor(); } "(visitor" _visname=_VisName() { it.set_visname(_visname); } _expression=_Expression() { it.set_expression(_expression); } _finishaction=_FinishAction() { it.set_finishaction(_finishaction); } _action_l=_Action_L() { it.set_action_l(_action_l); } ")" { return it; } } FinishAction _FinishAction() : { FinishAction it = null; Expression _expression; } { { it=new FinishAction(); } _expression=_Expression() { it.set_expression(_expression); } { return it; } } Action _Action() : { Action it = null; } { ( it=_Before() | it=_After() ) { return it; } } void common_Action(Action it) : { VarName _varname; TypeName _typename; Expression _expression; } { "(" _varname=_VarName() { it.set_varname(_varname); } _typename=_TypeName() { it.set_typename(_typename); } ")" _expression=_Expression() { it.set_expression(_expression); } { } } Before _Before() : { Before it = null; } { { it=new Before(); } "before" common_Action(it) { return it; } } After _After() : { After it = null; } { { it=new After(); } "after" common_Action(it) { return it; } } Expression _Expression() : { Expression it = null; } { ( it=_Simple() | it=_Compound() | it=_Fetch() | it=_Get() | it=_Set() ) { return it; } } void common_Expression(Expression it) : { } { { } } Simple _Simple() : { Simple it = null; } { ( it=_Numerical2() | it=_VarName() ) { return it; } } void common_Simple(Simple it) : { } { common_Expression(it) { } } Compound _Compound() : { Compound it = null; Op _op; Expression_L _args; } { { it=new Compound(); } "(" _op=_Op() { it.set_op(_op); } _args=_Expression_L() { it.set_args(_args); } ")" common_Expression(it) { return it; } } Op _Op() : { Op it = null; } { ( it=_Add() | it=_Mul() | it=_Sub() ) { return it; } } void common_Op(Op it) : { } { { } } Add _Add() : { Add it = null; } { { it=new Add(); } "+" common_Op(it) { return it; } } Mul _Mul() : { Mul it = null; } { { it=new Mul(); } "*" common_Op(it) { return it; } } Sub _Sub() : { Sub it = null; } { { it=new Sub(); } "-" common_Op(it) { return it; } } Fetch _Fetch() : { Fetch it = null; Strategy _strategy; Expression _expression; } { { it=new Fetch(); } "(fetch" _strategy=_Strategy() { it.set_strategy(_strategy); } _expression=_Expression() { it.set_expression(_expression); } ")" common_Expression(it) { return it; } } Get _Get() : { Get it = null; VarName _varname; FieldName _fieldname; } { { it=new Get(); } "(get" _varname=_VarName() { it.set_varname(_varname); } _fieldname=_FieldName() { it.set_fieldname(_fieldname); } ")" common_Expression(it) { return it; } } Set _Set() : { Set it = null; VarName _varname; FieldName _fieldname; Expression _expression; } { { it=new Set(); } "(set!" _varname=_VarName() { it.set_varname(_varname); } _fieldname=_FieldName() { it.set_fieldname(_fieldname); } _expression=_Expression() { it.set_expression(_expression); } ")" common_Expression(it) { return it; } } VisName _VisName() : { VisName it = null; Ident _ident; } { { it=new VisName(); } _ident=_Ident() { it.set_ident(_ident); } { return it; } } VarName _VarName() : { VarName it = null; Ident _ident; } { { it=new VarName(); } _ident=_Ident() { it.set_ident(_ident); } common_Simple(it) { return it; } } Numerical2 _Numerical2() : { Numerical2 it = null; Number _number; } { { it=new Numerical2(); } _number=_Number() { it.set_number(_number); } common_Simple(it) { return it; } } AdaptiveMethod_L _AdaptiveMethod_L() : { AdaptiveMethod_L it = null; Nonempty_AdaptiveMethod_L _first; } { { it=new AdaptiveMethod_L(); } [ _first=_Nonempty_AdaptiveMethod_L() { it.set_first(_first); } ] { return it; } } DD_PL _DD_PL() : { DD_PL it = null; Nonempty_DD_PL _first; } { { it=new DD_PL(); } "(" [ _first=_Nonempty_DD_PL() { it.set_first(_first); } ] ")" { return it; } } Alternative_L _Alternative_L() : { Alternative_L it = null; Nonempty_Alternative_L _first; } { { it=new Alternative_L(); } [ _first=_Nonempty_Alternative_L() { it.set_first(_first); } ] { return it; } } TypedField_L _TypedField_L() : { TypedField_L it = null; Nonempty_TypedField_L _first; } { { it=new TypedField_L(); } [ _first=_Nonempty_TypedField_L() { it.set_first(_first); } ] { return it; } } OG_L _OG_L() : { OG_L it = null; Nonempty_OG_L _first; } { { it=new OG_L(); } [ _first=_Nonempty_OG_L() { it.set_first(_first); } ] { return it; } } Action_L _Action_L() : { Action_L it = null; Nonempty_Action_L _first; } { { it=new Action_L(); } [ _first=_Nonempty_Action_L() { it.set_first(_first); } ] { return it; } } Expression_L _Expression_L() : { Expression_L it = null; Nonempty_Expression_L _first; } { { it=new Expression_L(); } [ _first=_Nonempty_Expression_L() { it.set_first(_first); } ] { return it; } } Nonempty_AdaptiveMethod_L _Nonempty_AdaptiveMethod_L() : { Nonempty_AdaptiveMethod_L it = null; AdaptiveMethod _it; Nonempty_AdaptiveMethod_L _next; } { { it=new Nonempty_AdaptiveMethod_L(); } _it=_AdaptiveMethod() { it.set_it(_it); } [ _next=_Nonempty_AdaptiveMethod_L() { it.set_next(_next); } ] { return it; } } Nonempty_DD_PL _Nonempty_DD_PL() : { Nonempty_DD_PL it = null; DD _it; Nonempty_DD_PL _next; } { { it=new Nonempty_DD_PL(); } _it=_DD() { it.set_it(_it); } [ _next=_Nonempty_DD_PL() { it.set_next(_next); } ] { return it; } } Nonempty_Alternative_L _Nonempty_Alternative_L() : { Nonempty_Alternative_L it = null; Alternative _it; Nonempty_Alternative_L _next; } { { it=new Nonempty_Alternative_L(); } _it=_Alternative() { it.set_it(_it); } [ _next=_Nonempty_Alternative_L() { it.set_next(_next); } ] { return it; } } Nonempty_TypedField_L _Nonempty_TypedField_L() : { Nonempty_TypedField_L it = null; TypedField _it; Nonempty_TypedField_L _next; } { { it=new Nonempty_TypedField_L(); } _it=_TypedField() { it.set_it(_it); } [ _next=_Nonempty_TypedField_L() { it.set_next(_next); } ] { return it; } } Nonempty_OG_L _Nonempty_OG_L() : { Nonempty_OG_L it = null; OG _it; Nonempty_OG_L _next; } { { it=new Nonempty_OG_L(); } _it=_OG() { it.set_it(_it); } [ _next=_Nonempty_OG_L() { it.set_next(_next); } ] { return it; } } Nonempty_Action_L _Nonempty_Action_L() : { Nonempty_Action_L it = null; Action _it; Nonempty_Action_L _next; } { { it=new Nonempty_Action_L(); } _it=_Action() { it.set_it(_it); } [ _next=_Nonempty_Action_L() { it.set_next(_next); } ] { return it; } } Nonempty_Expression_L _Nonempty_Expression_L() : { Nonempty_Expression_L it = null; Expression _it; Nonempty_Expression_L _next; } { { it=new Nonempty_Expression_L(); } _it=_Expression() { it.set_it(_it); } [ _next=_Nonempty_Expression_L() { 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 }