// STUFF LEFT OUT HERE TreeNode *addSibling(TreeNode *t, TreeNode *s) { TreeNode *tmp; if (t!=NULL) { tmp = t; while (tmp->sibling!=NULL) tmp = tmp->sibling; tmp->sibling = s; return t; } return s; } // // DATA // // the syntax tree goes here TreeNode *syntaxTree; // // // // // // // // // // // // // // // // // %} // union appears in the xxxx.tab.h file that is included in flex file // so any data types must be defined early in the flex file than the tab.h %union { TokenData *tokenData; // defined in scanType.h TreeNode *tree; // defined in treeNode.h ExpType type; // for passing type spec up the tree } // // TOKENS // // nonterminals %type argList %type args %type call %type constant %type exp %type expList %type factor %type immutable %type mulExp %type mutable %type unaryExp %type sumOp %type mulOp %type unaryOp // operators %token FIRSTOP %token ADDASS DEC DIVASS EQ GEQ IN INC LEQ MULASS NEQ SUBASS CHSIGN %token '&' '|' '!' '*' '+' '-' '.' '/' '<' '=' '>' '%' '?' %token LASTOP // keywords and syntactic "sugar" %token BOOL BREAK CHAR ELSE FOR IF INT RETURN STATIC WHILE %token ID %token BOOLCONST %token NUMCONST %token CHARCONST %token STRCONST %token '(' ')' ',' ';' '[' '{' '}' ']' ':' %token LASTTERM %% expList : exp ';' { syntaxTree = $1;} ; exp : exp sumOp mulExp { $$ = newExpNode(OpK, $2); $$->child[0] = $1; $$->child[1] = $3; } | mulExp { $$=$1;} ; sumOp : '+' { $$=$1;} | '-' { $$=$1;} ; mulExp : mulExp mulOp unaryExp { $$ = newExpNode(OpK, $2); $$->child[0] = $1; $$->child[1] = $3; } | unaryExp { $$=$1;} ; mulOp : '*' { $$=$1;} | '/' { $$=$1;} | '%' { $$=$1;} ; unaryExp : unaryOp unaryExp { $$ = newExpNode(OpK, $1); $$->child[0] = $2; } | factor { $$ = $1;} ; unaryOp : '-' { $1->tokenclass=CHSIGN; $$=$1;} ; factor : mutable { $$=$1;} | immutable { $$=$1;} ; mutable : ID { $$ = newExpNode(IdK, $1); $$->attr.name = $1->svalue; } ; immutable : '(' exp ')' { $$ = $2;} | constant { $$ = $1;} | call { $$ = $1;} ; call : ID '(' args ')' { $$ = newExpNode(CallK, $1); $$->attr.name = $1->svalue; $$->child[0] = $3; } ; args : argList { $$ = $1;} | /*empty*/ { $$ = NULL;} ; argList : argList ',' exp { $$ = addSibling($1, $3); } | exp { $$=$1;} ; constant : NUMCONST { $$ = newExpNode(ConstantK, $1); $$->attr.value = $1->nvalue; $$->expType = Integer; } | CHARCONST { $$ = newExpNode(ConstantK, $1); $$->attr.cvalue = $1->cvalue; $$->expType = Char; } | STRCONST { $$ = newExpNode(ConstantK, $1); $$->attr.string = $1->svalue; $$->expType = Char; $$->isArray = true; } | BOOLCONST { $$ = newExpNode(ConstantK, $1); $$->attr.value = $1->nvalue; $$->expType = Boolean; } ; %% // STUFF LEFT OUT HERE int main(int argc, char *argv[]) { // STUFF LEFT OUT HERE // scan and parse yyparse(); // print the AST to stdout if (printASTTree && numErrors==0) printTree(stdout, syntaxTree, false, false); // STUFF LEFT OUT HERE if (numErrors>0) return 1; return 0; }