%{ #include "IntRep.h" #define YYSTYPE string #include "lexical.h" // int vbltable[26]; // extern string varval; Attribute currAttribute; AttributeList currAttrList; AttributeList currState; string currName; string currTickCode; string currCCode; string headerCCode; string currAttrInitCCode; string currParent; Agent currAgent; Model currModel; Simulation simulation; char caux[50]; void insertAgent(); void insertModel(); %} %token SIMULATION %token MODEL %token AGENT %token STATE %token TICK %token GETMESSAGE %token POSTMESSAGE %token IDENTIFIER %token INTEGER %token OPENBRACES %token CLOSEBRACES %token BEGINBLOCK %token ENDBLOCK %token SPECIALCHAR %token SEMICOLON %token COMMA %token AT %token EQ %token SPACE %% simulation: header SIMULATION name BEGINBLOCK declarations ENDBLOCK { simulation.setHeaderCode( headerCCode ); simulation.setName( $3 ); } | SIMULATION name BEGINBLOCK declarations ENDBLOCK { simulation.setName( $2 ); } header: AT ccode AT { headerCCode = currCCode; currCCode = ""; } declarations: single_declaration { printf("rule.\n"); } | single_declaration declarations { printf("rule.\n"); } single_declaration: model_declaration { printf("rule.\n"); } | agent_declaration { printf("rule.\n"); } model_declaration: MODEL name BEGINBLOCK state_declaration tick_declaration ENDBLOCK { insertModel(); } | MODEL name BEGINBLOCK state_declaration ENDBLOCK { insertModel(); } | MODEL name BEGINBLOCK tick_declaration ENDBLOCK { insertModel(); } | MODEL name BEGINBLOCK ENDBLOCK { insertModel(); } agent_declaration: AGENT name BEGINBLOCK parent_declaration state_declaration tick_declaration ENDBLOCK { insertAgent(); } | AGENT name BEGINBLOCK parent_declaration state_declaration ENDBLOCK { insertAgent(); } | AGENT name BEGINBLOCK parent_declaration tick_declaration ENDBLOCK { insertAgent(); } | AGENT name BEGINBLOCK state_declaration tick_declaration ENDBLOCK { insertAgent(); } | AGENT name BEGINBLOCK state_declaration ENDBLOCK { insertAgent(); } | AGENT name BEGINBLOCK tick_declaration ENDBLOCK { insertAgent(); } | AGENT name BEGINBLOCK ENDBLOCK { insertAgent(); } name: IDENTIFIER { currName = $1; $$ = $1; } parent_declaration: MODEL IDENTIFIER BEGINBLOCK attr_declarations ENDBLOCK { currParent = $2; } | MODEL IDENTIFIER { currParent = $2; } state_declaration: STATE BEGINBLOCK attr_declarations ENDBLOCK { currState = currAttrList; currAttrList.clear(); } attr_declarations: attr_declaration {} | attr_declaration attr_declarations {} attr_declaration: attr_type attr_name EQ attr_init_val SEMICOLON { currAttribute.setType( $1 ); currAttribute.setName( $2 ); currAttribute.setInitVal( currAttrInitCCode ); currAttrList.push_back(currAttribute); currAttrInitCCode = ""; printf("Attribute declaration rule.\n"); } | attr_name EQ attr_init_val SEMICOLON { currAttribute.setType( "" ); currAttribute.setName( $1 ); currAttribute.setInitVal( currAttrInitCCode ); currAttrList.push_back(currAttribute); currAttrInitCCode = ""; printf("Attribute declaration rule.\n"); } attr_type: IDENTIFIER { $$ = $1; printf("Attribute type rule.\n"); } attr_name: IDENTIFIER { $$ = $1; printf("Attribute name rule.\n"); } attr_init_val: AT ccode_tokens AT { currAttrInitCCode = currCCode; currCCode = ""; printf("Attribute init value rule.\n"); } tick_declaration: TICK BEGINBLOCK AT ccode AT ENDBLOCK { currTickCode = currCCode; currCCode = ""; } ccode: ccode_statement | ccode_statement ccode ccode_statement: ccode_tokens SEMICOLON { currCCode += ";\n "; } ccode_tokens: ccode_token | ccode_token ccode_tokens ccode_token: INTEGER { currCCode += $1; $$ = ""; } | OPENBRACES { currCCode += "["; $$ = ""; } | CLOSEBRACES { currCCode += "]"; $$ = ""; } | BEGINBLOCK { currCCode += "{"; $$ = ""; } | ENDBLOCK { currCCode += "}"; $$ = ""; } | SPECIALCHAR { currCCode += $1; $$ = ""; } | COMMA { currCCode += ","; $$ = ""; } | EQ { currCCode += "="; $$ = ""; } | GETMESSAGE OPENBRACES IDENTIFIER CLOSEBRACES { currCCode += " messageBoard_->getMessage(\"" + $3 + "\") "; $$ = ""; } | POSTMESSAGE OPENBRACES IDENTIFIER COMMA IDENTIFIER CLOSEBRACES { currCCode += " messageBoardNew_->postMessage(\"" + $3 + "\", " + $5 + ") "; $$ = ""; } | IDENTIFIER { currCCode += $1; $$ = ""; } | SPACE { currCCode += " "; $$ = ""; } %% void insertAgent() { Agent agent; agent.setName( currName ); agent.setAttributes( currState ); agent.setTickCode( currTickCode ); agent.setModel( currParent ); currName = ""; currState.clear(); currTickCode = " "; currParent = ""; simulation.addAgent(agent); } void insertModel() { Model model; model.setName( currName ); model.setAttributes( currState ); model.setTickCode( currTickCode ); currName = ""; currState.clear(); currTickCode = " "; simulation.addModel(model); }