%{ #include #include #include #include "a2.h" extern int yylex(void); extern void yyerror(char *msg); RuleList gRuleList; int gNumSlots = 0; %} %union { int id; char word[256]; char var; float val; ExprItem exprItem; Expression expression; ParamList paramList; Function function; Action action; ReplacementList replacementList; ActionList actionList; Rule rule; RuleList ruleList; ActionLists actionLists; }; %left '<' '>' '=' '!' %left '+' '-' %left '*' '/' %left '(' ')' %token FUNCNAME %token WORDVAL %token VARIABLE %token OPERATOR %token FLOATVAL %type OPERAND %type EXPRESSION %type PARAMS %type FUNC %type ACTION %type REPLACEMENTS %type ACTIONLIST %type RULE %type RULES %type ACTIONLISTS %% program: RULES '@' { gRuleList = $1; return 0; } ; RULES: RULES RULE { $$.rules[$$.numRules++] = $2; } | RULE { $$.rules[$$.numRules++] = $1; } ; RULE: FLOATVAL ':' WORDVAL '~' ACTIONLISTS { strcpy($$.predecessor, $3); $$.id = (int)($1 + 0.5); $$.actionLists = new ActionLists; *($$.actionLists) = $5; if ($$.actionLists->lists[0].probability != 0) { $$.actionLists->slot = gNumSlots++; } } | FLOATVAL ':' WORDVAL ':' EXPRESSION '~' ACTIONLISTS { strcpy($$.predecessor, $3); $$.id = (int)($1 + 0.5); $$.actionLists = new ActionLists; *($$.actionLists) = $7; $$.requirement = $5; if ($$.actionLists->lists[0].probability != 0) { $$.actionLists->slot = gNumSlots++; } } ; ACTIONLISTS: ACTIONLISTS '~' ACTIONLIST ':' FLOATVAL { $$.lists[$$.numLists++] = $3; $$.lists[$$.numLists-1].probability = $5; if ($$.numLists > 1) { $$.lists[$$.numLists-1].probability += $$.lists[$$.numLists-2].probability; } } | ACTIONLIST ':' FLOATVAL { $$.lists[$$.numLists++] = $1; $$.lists[$$.numLists-1].probability = $3; } | ACTIONLIST { $$.lists[$$.numLists++] = $1; } ; ACTIONLIST: ACTIONLIST ACTION { $$.actions[$$.numActions++] = $2; } | ACTION { $$.actions[$$.numActions++] = $1; } ; ACTION: FUNC '{' REPLACEMENTS '}' { $$.function = $1; $$.replacementList = $3; } | FUNC { $$.function = $1; } ; REPLACEMENTS: REPLACEMENTS '|' WORDVAL { strcpy($$.replacements[$$.numReplacements++], $3); } | WORDVAL { strcpy($$.replacements[$$.numReplacements++], $1); } ; FUNC: FUNCNAME '(' PARAMS ')' { $$.id = $1; $$.paramList = $3; if ($$.id == RAND) { $$.slot = gNumSlots++; } } | FUNCNAME { $$.id = $1; $$.paramList.numParams = 0; if ($$.id == RAND) { $$.slot = gNumSlots++; } } ; PARAMS: PARAMS ',' EXPRESSION { $$.params[$$.numParams++] = $3; } | EXPRESSION { $$.params[$$.numParams++] = $1; } | { $$.numParams = 0; } ; EXPRESSION: '(' EXPRESSION ')' { for (int i = 0; i < $2.numItems; i++) { $$.items[$$.numItems++] = $2.items[i]; } } | EXPRESSION '*' EXPRESSION { for (int i = 0; i < $3.numItems; i++) { $$.items[$$.numItems++] = $3.items[i]; } $$.items[$$.numItems++].op = '*'; } | EXPRESSION '/' EXPRESSION { for (int i = 0; i < $3.numItems; i++) { $$.items[$$.numItems++] = $3.items[i]; } $$.items[$$.numItems++].op = '/'; } | EXPRESSION '+' EXPRESSION { for (int i = 0; i < $3.numItems; i++) { $$.items[$$.numItems++] = $3.items[i]; } $$.items[$$.numItems++].op = '+'; } | EXPRESSION '-' EXPRESSION { for (int i = 0; i < $3.numItems; i++) { $$.items[$$.numItems++] = $3.items[i]; } $$.items[$$.numItems++].op = '-'; } | EXPRESSION '>' EXPRESSION { for (int i = 0; i < $3.numItems; i++) { $$.items[$$.numItems++] = $3.items[i]; } $$.items[$$.numItems++].op = '>'; } | EXPRESSION '<' EXPRESSION { for (int i = 0; i < $3.numItems; i++) { $$.items[$$.numItems++] = $3.items[i]; } $$.items[$$.numItems++].op = '<'; } | EXPRESSION '=' EXPRESSION { for (int i = 0; i < $3.numItems; i++) { $$.items[$$.numItems++] = $3.items[i]; } $$.items[$$.numItems++].op = '='; } | EXPRESSION '!' EXPRESSION { for (int i = 0; i < $3.numItems; i++) { $$.items[$$.numItems++] = $3.items[i]; } $$.items[$$.numItems++].op = '!'; } | OPERAND { $$.items[$$.numItems++] = $1; $$.items[$$.numItems-1].op = 0; } ; OPERAND: WORDVAL { $$.type = EI_VARIABLE; strcpy($$.name,$1); } | FLOATVAL '^' { $$.type = EI_RELATIVE; $$.val = $1; } | FLOATVAL { $$.type = EI_FLOAT; $$.val = $1; } | FUNC { $$.type = EI_FUNC; $$.function = new Function; *($$.function) = $1; } ; %% #include #include "main.h" int main(int argc, char *argv[]) { return MyMain(argc, argv); } void yyerror(char *msg) { printf("%s\n", msg); }