FAQ for Compiler Final Project 2005 1. What are the rules for constant def? A: legal_id_name = constant_string or a previously defined constant constant strings are either an integer or a float float follows the syntax of C except that we need a decimal point constant variable is not a normal variable. It has r-value, but no l-value. That is, a constant variable be on the left hand side of the assignement 2. How to return a function value? A: In each function, the name of the function is a variable that has l-value, but no r-value. The value of the variable at the end of the function execution returns back to the called procedure/function. Read page 3 of slide0.pdf for an example. 3. for loop裡的statement我猜是只能放1個statement, 因為沒有END FOR之類的結束符 號, 但if裡的也是statement, 所以老師的意思是if, for, while裡面只能有一行嗎?? A: No! A statement can be a sequence of statements enclosed by begin end. example: FOR i := 1 TO 10 DO BEGIN IF i / 2 * 2 == i THEN BEGIN x := x + i; x := x * x; END ENDIF; y := x + x; END 4. FOR var := int-expression-1 TO int-expression-2 DO 其中的var要是之前有宣告過的嗎? 還是算是宣告, 可以放新變數? A: var must be declared before. var must be integer. Same for FOR ... DOWNTO ... DO 5. 同4, int-expression-1只在進入FOR之前做一次, 但int-expression-2有兩種可能, 一種是先算好再進入FOR, 一種是每次算, 不知道是哪一種? A: int-expression-2每次算. 6. In arithmetic expression : - no auto-type conversion what does this mean? A: It means the following. 1. int_expr float_expr --> error 2. int_var := float_expr --> error 3. float_var := int_expr --> error only the followings are correct. 1. int_expr int_expr 2. float_expr float_expr 3. int_var := int_expr 4. float_var := flaot_expr 7. "Can have "(" and ")" in expression" what is this ? A: It means ( and ) and allowed in expressions. That is, expression -> (expression) 8. 投影片中有要求 check for divide by 0 in compile and run time need to check array out of bound in compile and run time 如果只是檢查如 y := x / 0 這種還好 萬一 y := x / z 這種也要檢查 是不是就要像interpreter算出 z 的值 可是如果之前某行在算 z 的時候有syntax error 這樣 z 算出來的值就可能沒有必定的準則了 到時候判定 y := x / z 也可能會連帶出現問題 check array out of bound 也有類似的問題 (如 a[i] 不知道 i 為何值) A: You will know the value of variables in run time. So you can generate a code like load the value of z into register4 if value of register4 is zero then goto error_divid_by_zero Write a C-- code for handling run time error error_divide_by_zero: error handling code in run time 9. What are the priority of operators? A: The same as C. 10. How to use "not"? A: not logical_expressions 11. 教授有說過浮點數必須要有小數點,那是否小數點前後都必須有數字呢? 因為如果不是的話 ARRAY[1..10] 裡的1應該會parse 成1. (float) 10 應該會parse 成.10 (float) A: 小數點前後不須有數字, but at least one side must have digits. In array declaration, there is no space between the two dots. That is, ARRAY[1. .10] is illegal. 12. function的回傳值是以 function name表示 那有沒有可能其 code中出現多次的 function name? ex: FUNCTION Foo( x : INTEGER ) : INTEGER BEGIN Foo := x + 3; Foo := x + 5; Other Statement; END 如果可能且是合法的話,是以最後出現的為準嗎? 又出現再其後的statement是否需要繼續做? 還是說一看到 Foo 就馬上retuen? A: A function name is a variable. So your above example is legal. You don't immediately perform a return when you see the function name because it is simply a variable. 13. Type define中是不是也跟constant一樣也是剛宣告過的就可以用? Ex: TYPE Int1 = INTEGER; Int_Array = ARRAY[1..10] of Int1; ENDTYPE A: Yes.