《编译原理》上课笔记1
2015年03月30日 课本P24 P226 P259(左侧,7-1图) 1.外部声明 external-declaration external int a; int b; void f(){} 2.ucc\examples\sc\decl.c 课本P24 Declaration -----> int Declarator Declarator -----> * Declarator | PostfixDeclarator ------------>D = {PD,*PD,**PD,***PD,.....} PostfixDeclarator ---> DirectDeclarator | PostfixDeclarator [num] | PostfixDeclarator (ParameterList) ------------>PD{DD,DD[num],DD(void), DD[num][num],DD(void)(void), DD[num](void),DD(void)[num], ........ } ParameterList ---> int | ParameterList , int DirectDeclarator -----> id | (Declarator) 3.P226"类型表达式" int --操作数 【运算符】:* --指针类型,[] --数组类型,() --函数类型 3.1 指针的数组:int *arr[4]; 数组的指针:int(*ptr)[4]; typedef int* INTPIR typedef int ARRAY[4] INTPIR arr[4]; ARRAY[4] *prt; 4.P259 代码区 静态区 【编译时】(全局变量、static变量、常量) 堆区 【运行时】(new,malloc) 空闲内存【运行时】 栈区 【运行时】(局部,形参) 4.1变参 printf("%d",a); printf("%d %d",a,b); printf(const char*fmt, ...); 4.2.13.png(C语言变量布局) 匿名参数 无名参数----->参数提升 char,short->int float ->double
Stmt.c
2015年03月16日 分析器的算法:(ucc/examples/sc/Expr.c) 1.PrimaryExpression -----> id | num | (Expression) ------PE--->----- | | | | | * / | | ---PE<-- void PrimaryExpression (void){ if(CurToken == ID) NEXT_TOKEN; else if((CurToken == NUM) NEXT_TOKEN; else if((CurToken == LP){ NEXT_TOKEN; if((CurToken == RP) NEXT_TOKEN; } else error("( is missed."); } 2."左结合" MultiplicativeExpression -----> PrimaryExpression MultiplicativeExpression -----> MultiplicativeExpression * PrimaryExpression MultiplicativeExpression -----> MultiplicativeExpression / PrimaryExpression void MultiplicativeExpression (void){ PrimaryExpression (); while(CurToken == MUL | CurToken == DIV){ NEXT_TOKEN; PrimaryExpression(); } } 3.E = {T,T+T,...} = T{e,+T,+T+T,...} -> T E' E' -> e -> +T E' void E'(void){ if(CurrentToken == +){ NEXT_TOKEN; T();E'(); }else{ } } 4.“右结合” ME-> PE ME' ME'->e(空) ->*|/ ME void MultiplicativeExpression (void){ PrimaryExpression (); if(CurToken == MUL | CurToken == DIV){ NEXT_TOKEN; MultiplicativeExpression(); }else{ } } 5.VisitArithmeticNode() (a+b)*c t1 * t0 c + a b 中间代码生成: t0 = a+b; t1 = t0+c; 6.int(*f(int,int,int))[4] eg.int (*arr)[4] typedef int ARRAY[4] ARRAY *f(int,int,int)
Expr.c
2015年03月22日课本P241 1.S->if(B)S1 if(!B)goto label0; S1 label0: 2.if(B)S1 else S2 if(!B)goto label0; S1 goto label1; label0:S2 goto label1; label1: 3.while(B) S1 label0: if(!B)goto label1; S1 goto label0; label1: 4.实验课自己写do...while();语句 ????? 5.if语句 kids[0]->label0 kids[1]->label1 expr->expression thenStmt->then语句 elseStmt->else语句 next astStmtNode 课本P129 6.VisitStatementNode->if(c) a=f;else b=k; kids[0]->label0 kids[1]->label1 expr->c thenStmt->a=f; elseStmt->b=k; 7.ExpressionStatement(void){ } -> a=3+5; kids[0]->a expr-> + 3 5 8.Compund语句 ------ ------ | | ->| | ------ ------.......... |next|-- |next| FIRST(a)首符号的集合 9.linux系统下 make 的结果: cse@ubuntu:~$ cd src/ucc/examples/sc cse@ubuntu:~/src/ucc/examples/sc$ make ucc -o sc lex.c expr.c error.c decl.c stmt.c main.c cat demo.c { int (*f(int,int,int))[4]; int (*(*fp2)(int,int,int))(int); if(c) a = f; else{ b = k; } while(c){ while(d){ if(e){ d = d - 1; } } c = c - 1; } } ./sc < demo.c f is: function(int,int,int) which returns pointer to array[4] of int fp2 is: pointer to function(int,int,int) which returns pointer to function(int) which returns int if(!c) goto Label_0 a = f goto Label_1 Label_0: b = k Label_1: Label_2: if(!c) goto Label_6 Label_3: if(!d) goto Label_5 if(!e) goto Label_4 t0 = d - 1 d = t0 Label_4: goto Label_3 Label_5: t1 = c - 1 c = t1 goto Label_2 Label_6: