📄 c-.y
字号:
;initializer : assignment_expression {$$=$1; if(strcmp($1->name,"STRING")==0) arraySize=strlen(literals[$1->strIndex])-1; else arraySize++; } | '{' initializer_list '}' {$$=$2;} ;initializer_list : initializer {$$=$1;} | initializer_list ',' initializer {$$=addSibling(addNext(newLeaf("initLst"),$1), addNext(newLeaf("init"),$3));} ;statement : labeled_statement {$$=$1;} | compound_statement {$$=$1;} | expression_statement {$$=$1;} | selection_statement {$$=$1;} | iteration_statement {$$=$1;} | jump_statement {$$=$1;} | print_statement {$$=$1;} ;labeled_statement : ID ':' statement {$$=addSibling(newLeaf("label"), addNext(newLeaf("stmt"),$3)); strcpy($$->value, $1);} | CASE constant_expression ':' statement {$$=addSibling(newLeaf("case"), addSibling(addNext(newLeaf("consExp"),$2), addNext(newLeaf("stmt"),$4)));} | DEFAULT ':' statement {$$=addSibling(newLeaf("default"),addNext(newLeaf("stmt"),$3));} ;compound_statement : '{' '}' {$$=NULL;} | '{' statement_list '}' {$$=$2;} | '{' declaration_list '}' {$$=$2;} | '{' declaration_list statement_list '}' {$$=addSibling(addNext(newLeaf("decLst"), $2), addNext(newLeaf("stmtLst"), $3));} //| error ';' {$$=NULL;symRoot=NULL;} ;declaration_list : declaration {$$=$1;} | declaration_list declaration {$$=addSibling(addNext(newLeaf("decLst"), $1), addNext(newLeaf("dec"), $2));} ;statement_list : statement {$$=$1;} | statement_list statement {$$=addSibling(addNext(newLeaf("stmtLst"), $1), addNext(newLeaf("stmt"),$2));} ;expression_statement : ';' {$$=NULL;} | expression ';' {$$=$1;} ;print_statement : PRINTLN '(' print_list ')' ';' {$$=addSibling(newLeaf($1), addNext(newLeaf("prntLst"),$3));} | PRINTLN '(' error ';' {symRoot=NULL;} ;print_list : assignment_expression {$$=$1;} | print_list PRN_OP assignment_expression {$$=addSibling(addNext(newLeaf("prntLst"),$1), $3);} ; selection_statement : IF '(' expression ')' statement {$$=addSibling(newLeaf("if"), addSibling(addNext(newLeaf("exp"),$3), addNext(newLeaf("stmt"),$5) ));strcpy($$->value,"if");} | IF '(' expression ')' statement ELSE statement {$$=addSibling(newLeaf("if"), addSibling(addNext(newLeaf("exp"),$3), addSibling(addNext(newLeaf("stmt"),$5), addNext(newLeaf("stmt"),$7))));strcpy($$->value,"ifelse");} | SWITCH '(' expression ')' statement {$$=addSibling(newLeaf("switch"), addSibling(addNext(newLeaf("exp"),$3), addNext(newLeaf("stmt"),$5) ));} ;iteration_statement : WHILE '(' expression ')' statement {$$=addSibling(newLeaf("while"), addSibling(addNext(newLeaf("exp"),$3), addNext(newLeaf("stmt"),$5) )); strcpy($$->sibling->value,"while"); strcpy($$->sibling->sibling->value,"while"); } | DO statement WHILE '(' expression ')' ';' {$$=addSibling(newLeaf("doWhile"), addSibling(addNext(newLeaf("stmt"),$2), addNext(newLeaf("exp"),$5) )); strcpy($$->sibling->value,"doWhile"); strcpy($$->sibling->sibling->value,"doWhile"); } | FOR '(' expression_statement expression_statement ')' statement { $$=addSibling(newLeaf($1), addSibling(addNext(newLeaf("expStmt"),$3), addSibling(addNext(newLeaf("expStmt"),$4), addNext(newLeaf("stmt"),$6) )) ); strcpy($$->sibling->value,"for"); strcpy($$->sibling->sibling->value,"for"); strcpy($$->sibling->sibling->sibling->value,"for");} | FOR '(' expression_statement expression_statement expression ')' statement { $$=addSibling(newLeaf($1), addSibling(addNext(newLeaf("expStmt"),$3), addSibling(addNext(newLeaf("expStmt"),$4), addSibling(addNext(newLeaf("exp"),$5), addNext(newLeaf("stmt"),$7) )) )); strcpy($$->sibling->value,"for"); strcpy($$->sibling->sibling->value,"for"); strcpy($$->sibling->sibling->sibling->value,"for"); strcpy($$->sibling->sibling->sibling->sibling->value,"for");} ;jump_statement : GOTO ID ';' {$$=newLeaf("goto");strcpy($$->value, $2);} | CONTINUE ';' {$$=newLeaf($1);} | BREAK ';' {$$=newLeaf($1);} | RETURN ';' {$$=newLeaf($1);} | RETURN expression ';' {$$=addSibling(newLeaf($1), $2);} ;program : external_declaration {$$=symRoot=$1;strcpy(scope, "Global");} | program external_declaration {$$=symRoot=addSibling(addNext(newLeaf("program"),$1),addNext(newLeaf("extDecl"),$2));strcpy(scope, "Global");} ;external_declaration : function_definition {$$=addSibling(newLeaf("func"),$1);} | declaration {$$=$1;} ;function_definition : declaration_specifiers declarator declaration_list compound_statement /* {$$=addSibling($1, addSibling($2, addSibling($3, $4)));}*/ {$$=addSibling(addNext(newLeaf("decSpec"),$1), addSibling(addNext(newLeaf("dectr"),$2), addSibling(addNext(newLeaf("decLst"),$3), addNext(newLeaf("cmpStmt"),$3)))); } | declaration_specifiers declarator compound_statement /* {$$=addSibling($1, addSibling($2, $3));}*/ {$$=addSibling(addNext(newLeaf("decSpec"),$1), addSibling(addNext(newLeaf("dectr"),$2), addNext(newLeaf("cmpStmt"),$3))); } | declarator declaration_list compound_statement /* {$$=addSibling($1, addSibling($2, $3));}*/ {$$=addSibling(addNext(newLeaf("dectr"),$1), addSibling(addNext(newLeaf("decLst"),$2), addNext(newLeaf("cmpStmt"),$3))); } | declarator compound_statement /* {$$=addSibling($1, $2);}*/ {$$=addSibling(addNext(newLeaf("dectr"),$1), addNext(newLeaf("cmpStmt"),$2)); } // | error ';' {$$=symRoot=NULL;yyerrok;} ;%%#include <stdio.h>#include "c-.yy.c"//extern char yytext[];yyerror(s)char *s;{ // numError++; //fprintf (stderr, "%s in line %i\n", s, currLine); // printf ("c- ERROR in line %i: parsing error before token \"%s\" .\n", yytext, currLine); printf ("ERROR @(%d,%d) : parsing error before token \"%s\"\n", currLine, column,yylval);} int main( argc, argv)int argc;char **argv;{ char input; extern int yydebug; ++argv, --argc; /* skip over program name */ int i=0; char buf[255], c_filename[255]; int s_flag=0, p_flag=0; int t; for(i=0; i<argc; i++){ if(argv[i][0]=='-'){ switch(argv[i][1]){ case 's': s_flag=1; break; case 'p': p_flag=1; break; } continue; } strcpy(c_filename, argv[i]); } if(argc>0){ yyin = fopen( c_filename, "r"); if(yyin==NULL){ printf("can open input file\n"); exit(0); } } else{ printf("Default usage: ./a.out test.cc -s\n"); yyin = stdin; } yydebug = 0; initGlobalVal(); yyparse(); //if(p_flag) printParseTree(symRoot); //if(s_flag) printSymbolTable(); if(yynerrs==0){ empty(); typeCheck(symRoot); //printf("finished!\n"); if(typeErrs==0){ if(p_flag) printParseTree(symRoot); if(s_flag) printSymbolTable(); }else{ printf("%d error(s) found totally\n", typeErrs); exit (0); } }else{ printf("%d error(s) found totally\n", yynerrs); exit (0); } strcpy(source_filename,c_filename); for(i=strlen(source_filename)-1;i>0;i--){ if(source_filename[i]=='.') source_filename[i]='\0'; } strcpy( literals_filename, strcat(strcpy(buf, source_filename), ".literals") ); literals_file = fopen( literals_filename, "w" ); output_literal_file( literals_file,strCount, literals ); fclose( literals_file ); strcpy( assembler_filename, strcat(strcpy(buf, source_filename), ".S") ); strcpy( object_code_filename, strcat(strcpy(buf, source_filename), ".o") ); assembler_file = fopen( assembler_filename, "w" ); output_start( assembler_file, literals_filename ); empty(); // empty the stack code_gen(assembler_file, symRoot); //output_end( assembler_file); fclose(assembler_file); char buff[40]; sprintf(buff,"as -o%s %s", object_code_filename, assembler_filename); system(buff); sprintf(buff,"ld -o%s %s %s", source_filename, object_code_filename,"ishow.o fshow.o"); system(buff); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -