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. 14. 請問logical operators(or, and, not, xor)的用法為何? 例如or是用c的文法(a | b)還是(a or b)? A: example: IF ((a > b) and (a >= c)) or ((not (c <= d)) xor (c <> a)) THEN ... 15. 請問(or, and, not, xor), mod, return, of這些要列為reserved word嗎? 是否還 有其他特殊字串(大寫字串除外)要列為reserved word? A: These are all resevered words. Our language uses resevered words. 16. 請問array的宣告在low跟..之間以及..跟upper之間有沒有空格? ARRAY[low .. upper] 如果沒有的話在lex裡 就會被當作兩個合法的float number處理耶 A: Please also read FAQ#11. We allow spaces in between low and "..", and in between ".." and upper. We do not allow space in between "..", that is ". ." is illegal. I am sure you can find some LEX tricks to handle this. 17. part10的投影片內的第三頁,有期末project的範例程式, 其中的CONST與TYPE,老師用的assign符號是'=',但是下面的statement部分 老師用的又是":=",請問是不是兩種都要做('=' for CONST, TYPE declaration) A: Yes, 兩種都要做. "=" for equivalent. ":=" for assignment. 18. 關於 type equivalent 的部分我觀念不太清楚 Example: ===== TYPE type1 = INTEGER; type2 = type1; ENDTYPE ===== type1, type2, INTEGER三者算是equivalent? A: We use name equivalence. Hence type1 and INTEGER are equivanent. type2 and type1 are also equivalent. Since equivalence is a transitive relation, so type1, typ1 and INTEGER are all equivalent. 19. More questions about type equivalent. 那 ===== TYPE type3 = ARRAY[1..5] of INTEGER; type4 = ARRAY[1..5] of INTEGER; type5 = type3; type6 = ARRAY[2..6] of INTEGER; type7 = ARRAY[2..7] of INTEGER; ENDTYPE ===== (type3, type4, type5)相同,(type6)一種,(type7)一種? A: You are right. (type3, type4, type5)相同,(type6)一種,(type7)一種. 20. operator的部分 logical: and, or, not, xor 這四個只會和boolean variable做運算 而boolean variable是指integer非零為true 還是logical operator只會和comparison的expression做運算 例如(a > b) and (c = d) A: Boolean expression means a comparison of variable with equal elementary types. Logical operator only can be applied on boolean expressions. There is no value for boolean expressions. That is, we use flow-of-control, not numerical representation. If a, b, c and d are all INTEGER, then the following is illegal. /* illegal usage of logical operator "and" */ BEGIN VAR a,b : INTEGER; ENDVAR IF (a and b) THEN ... ... 21. 看了FAQ 20之後,我對 and, or, xor, not 的使用還是不太懂, 這四種只適用於Boolean expression,意思是應該等到做到phase 4時, 才需要考慮這些operator嗎? phase 1的expression,需要做的是不是就只有+. -, *, /, %, (, )呢? 也就是說這些不是像C語言中的bitwise operator囉,是嗎? A: Yes for all three questions. 22. 因為規定的語言中boolean expressions沒有數值,如果寫 if ( (a expr2)的情形(在loop variable遞 增的情況下)? (expr1 = expr2)還是會執行? 若有執行loop, 則在離開loop之後loop variable的值應該等於expr2? A: Yes for both questions. 26. 想請問 for loop 投影片說 if the loop is not executed then the value of the looping variable stays unchanged 如果 FOR i:=1000 TO 10 這樣 i會是1000還是原本的值? A: The value of i is 原本的值. 27. 另外,the value of the looping variable at the end of the loop is the last looping value 如果 FOR i:=1TO 10 這樣 i會是11還是10? A: The value of i is 10.