FAQ for final project 1. expression 有哪些? ANS: A legal combination of (, ), operators (+-*/....><=...and or not) and operands (variables, constants, functions with return values, read(), length()) 2. 可不可以定義 sub read? ANS: length/read/write/writeln,writestr are all reserved words 3. 有傳回值的函數可不可以自己當 statement? ANS: No. 4. a() 和 a 是不是可以同時存在的? ANS: No. 5. What is the syntax rule for block structured variable definition? ANS: This is an optional feature. a. must be the first statement after begin b. the reserved word "local" is required in every block structured variable declaration c. can have multiple local var definitions example as follows: begin local int x,y,z local int ab,c ....# executable statments begin local int y,z,a ....# executable statments end end 6. What is the syntax rule for nested procedure definition? ANS: This is an optional feature. a. insert a "sub" def between the end of variable declaration and "begin" of executable or local variable-definition statements b. can have a sequence of "sub" definitions example as follows: program var int x sub hello() var y sub hello2() var int z begin # beginning of hello2() # .... end sub hello3() var int r begin # beginning of hello3() ... hello2() ... end begin # beginning of hello() ... hello3() ... end begin # beginning of main program ... hello() ... end 7. begin 與 end 間可不可以沒有 statement 即 begin end 合法嗎? ANS: This is illegal. If you want, you can write begin ; end 8. 可不可以提供 bad variable name 的定義 除了上課所說的以數字開頭及中文字外 還可以定義的更清楚嗎 我們可不可以把scanner 無法辨識的字全當作 bad name ? EX A := & 1 那算 illegal statement 或 bad name "&" ? ANS: The definition of a legal variable name is given in page 6 of the slide. You can either treat the example as illegal syntax or bad name depending on your implementation. There is no standard way to report errors. 9. Do I need write a 1-pass compiler? ANS: 1-pass is best, 2-pass is okay, 3-pass is acceptable, but anything after that is not desirable. Remember this is a "compiler" course, not an "interpreter" course. 10. 請問在 + - * / mod 時 可以在compiler時直接把值算出來 ex: a=b+c 先算出 b+c 的值 然後再gen code ?? ANS: If b and c are constants, then of course you may do this. If not, then the value of b+c cannot be decided at compile time. 11. 請問 c-- 的 procedure 是否允許 recusive call? ANS: Yes, but remember procedure call in c-- does not take parameters, not you cannot define new variables in c--. 12. exception 的語法看不太懂. 一個 try 之後可以有幾個 catch? 那如果 try 裡面是另一個 try 的話 後面的 catch 歸誰? ANS: 一個try後固定跟一個catch 13. 想請老師把在union 裡面放structure的用法 告訴我 yystype yyin yywrape等等系統需要的東西 我們也不是很清楚他的做法 所以長出bug在這種地方 ANS: Please read some YACC/LEX manuals to find out the answers. Some of them are available on-line. For example: http://dinosaur.compilertools.net/yacc (The answer to your question is located in Appendix C). 14. Why I cannot compiler your code at SlideB.pdf? ANS: This set of slides contain a ``pseudo code'' for a very simple compiler. This example does not pass LEX, YACC or GCC. Usage of this example is entirely to illustrate the high level ideas. Not responsibile for any syntax errors. :) I have updated the slides (slideB_1.pdf) so that some, but not all, of the errors are gone. Please read LEX, YACC and GCC manuals carefully to avoid programming mistakes. 15. 關於"陣列拷貝"這部份的用法。 是 a := b (a, b 同為陣列名) 嗎? 如果是的話,那可以接受 a := b + 1 (從 b[1] 開始複製) 這種用法嗎? ANS: The language only accepts array copy, nenver array operations. a := b is legal assuming NAME equivalence. a := b + 1 is illegal if a and b are both arrays. 16. 那gen 出來的code要怎麼做出執行檔阿 是gcc xxx.cmm嗎? ANS: gcc only takes .c .o, not .cmm. So one way to compile it, cp file.cmm tempfile.c gcc tempfile.c rm tempfile.c Another way to do it is: gcc -x c file.cmm 17. in our language, if we write writestr("%%") what should be the output? %% or % i.e., since in C-- we use printf("str") to output strings, however printf accepts % and \ modifier. Do we need to consider these? or just keep the string untouched then passes to C--? ANS: writestr("%%") should output %. To mak eit simple, just keep the string in writestr untouched and then passes directly to C--. 18. 設定dynamic array的size的 function setlength 的長度可以用 變數嗎? ANS: YES! 19. 關於 nested procedure 這部份要怎麼使用? 是 a.b() 嗎? (a 是函數名稱) ANS: No. See the example above in item (6). 20. 關於多維陣列的複製 ex . A[3][3], B[3][3] 可以用 A[1] = B[2] 來把 B 的第三列複製到 A 的第二列嗎? ANS: You don't need to do this. 21. 關於自定型態的複製 可以整個複製嗎? ANS: Yes (using NAME equivalent). 22. 關於自定型態的宣告 可以允許巢狀嗎? 也就是說可以在自定型態裡使用已經定義過的自定型態嗎? ANS: Yes! 23. 在陣列的宣告裡 投影片上的意思是一行只能宣告一個嗎? var int a[3] //只能這樣? int a[3], b[5] //這樣算錯? ANS: int a[3], b[5] // This is legal. 24. 在陣列宣告的長度裡可以是常數運算式 請問它可以有除了+-*/mod以外的運算嗎? var int a[(3>5) and 1 or 0] //這種有必要做嗎? ANS: Any constant expression is okay! That is, int a[(3>5) and 1 or 0] 會相當於宣告 int a[0]. However, int b[2 and 3] 不一定會相當於宣告 int b[1]. The value of TRUE is anything that is 非零, and is implementation dependent. 25. 變數名稱可以和陣列變數名稱一樣嗎? var int a, i, j int a[3] //可以有一樣的名字嗎? ANS: This is illegal. In fact, you should report "a" is redefined. 26. Can we pass an array element to a function? i.e., write(a[3]) and f(a[3]), a user-defined function ANS: This is okay as long as you declare proper formal parameters. 27. What is the semantic of multiple dimension array copy if their size of some dimension is not the same? int a[4][3][2], b[3][2][1] a := b ANS: This is illegal. We use NAME equivalence to decide whether one array can be copied to another array. However, for 1-D arrays with the same element type, we allow coping between arrays of different sizes. See page 12 of TA's slides for details. In short, a larger 1-D array cannot be copied into a smaller 1-D array. A smaller 1-D array can be copied into a larger 1-D array. The content of the larger 1-D array is left untouched on the excess part. eg: int a[12],b[15] b := a // copies a[0..11] to b[0..11]. b[12..14] are not changed. 28. 關於 short-circuit 的定義 在要判斷 a and b and c or d 的值時 若 a 是 false 則是 b 不判斷 還是 b 跟 c 都不判斷呢? 我們的 project 似乎是指定前者 也就是只有 b 不判斷 然後再判斷一次 false and c (規定左至右結合) 不過這樣還符合 short-circuit 嗎? 還是只要有一個不判斷就可以了? ANS: a and b and c or d = ((a and b) and c) or d a is false, b is not evaluated (a and b) is false, c is not evaluated ((a and b) and c) is false, d is evaluated. 29. 關於 for i = n to m 的部份 m 的值可以在迴圈內更改嗎? 也就是說 假如一開始是 2 to 5 那可以在迴圈內改成 to 4 之類的嗎? ANS: Of course the value of m can be changed in the for loop. However, the values are fixed after the loop executed. That is, the values of n and m are computed and recorded somewhere else. In your example, the loops are for i = 2, 3, 4, 5 regardless the value of m being changed during execution. 30. i 可以是陣列的單元嗎? 也就是說 可以接受 for a[1] = 1 to 3 這種嗎? ANS: Yes, as long as it is a "singular" variable with the type int (can be short or byte in the optional features.) 31. 進階功能中的for迴圈可以充許巢狀嗎? 也就是for裡面還有for 因為在if / while有特別提到可以巢狀 for則沒有,想確定一下 ANS: Yes, nested for loop is allowed. 32. for i = 5 to 2 begin ... end then, is the for-loop been executed i.e, i = 5, 4, ... , 2 or for is not been executed since 5 >2? ANS: for is executed for i = 5,4,3,2. 33. if m or n can be changed in a for loop, eg. x:=1 y:=3 for i in x to y begin y:=5 end after first loop, what value should i be? ANS: The value of i after the first loop is 2, regardless what value y becomes later. the grammar and the semantic of for loop is for (l-value of i) = m to n ... compute the r-values of m and n and save them into some places called them temp1 and temp2 compute the l-value of i (l-value of i) <-- temp1 three cases: I: temp1 == temp2 execute the loop only once II: temp1 < temp2 statements compute the l-value of i if (l-value of i) >= temp2 then break (l-value of i) <-- temp1+1 statements compute the l-value of i if (l-value of i) >= temp2 then break (l-value of i) <-- temp1+2 statements ... III: temp1 > temp2 ... FYI: In the following examples: Example A: for i = 1 to 10 i := 20 ====> The for loop is executed only once for i = 1. Example B: j := 2 k := 9 for i = j to k begin ... j := 3 k := -2 ... end ====> in the for loop 不管j和k怎麼變,i 都是從2到9 ====> in the for loop 如果是 i 一直被assign成3,結果是無窮迴圈 Example C: j := 2 k := 9 for i = j to k begin ... i := 15 ... end ====> i 被assign成15的話(不在2和9 之間) 是在下一次迴圈跳出來 34. 如果有以下的 code program var short a, b begin a := 32000 b := 32000 write(a + b) writeln() end 那究竟是要印出 64000 (不轉) 還是印出 -1536 (要轉) 呢? ANS: You may assume write() takes an int variable as an argument. Thus the result is the same with var int temp short a,b begin a := 32000 b := 32000 temp := a + b write(temp) writeln() end 35. i := 0 for A[i] = 3 to 9 i := i + 1 這樣可以得到 A[0..6] = 3..9 嗎? ANS: No! The l-value of A[i] is re-computed for each for iteration. 36. int t[10] 那 foreach a[2] in t 也合法嗎? 也就是說 前面的那個變數也能是陣列嗎? ANS: Yes. 37. A[0..4] = (1, 2, 3, 4, 0) foreach i in A A[i] := 0 的結果是 A[1] := 0 A[2] := 0 A[3] := 0 A[4] := 0 A[0] := 0 而得 A[0..4] = (0, 0, 0, 0, 0) 還是 A[1] := 0 A[0] := 0 A[3] := 0 A[0] := 0 A[0] := 0 而得 A[0..4] = (0, 0, 3, 0, 0) ANS: The grammar and semantic of foreach is: grammar: foreach l-value i in a 1-D array A semantic: compute the l-value of i, assign A[0] to the l-value of i compute the l-value of i, assign A[1] to the l-value of i ... 38. 在array宣告時 其長度是必須要常數運算式 int a[1and(9+7)or3not5] a[a[3+a[4+a[5]]]] := 6 這樣算合法嗎 也就是說 在使用array值時 a[]其[]內的值或運算式限制是什麼 還是說是任意由布林運算比較運算和四則運算任意構成 也可放巢狀的array 在[]內 ANS: Yes, they are legal. 39. What are the rules for type conversion? ANS: Please read page 20 of TA's slide. We do automatic type conversion. That is, we use the type of the l-value of decide the type of the whole expression. That is: (1) 如果 short := int 那需要在不加任何其他東西的情況下 (ex. (short) or (byte)) 直接把 int 轉成 short 也就是說,我們可以直接看成 short := (short) int (2) The result of short+short generates short. (3) for short = int to int int is converted to short in each iteration (4) foreach short in int (後面是 int-array) int is converted in each iteration. (please read the answer of FAQ item 37) 40. 關於final project的問題 關於自訂結構的文法, type myType ... ... epyt 宣告myType的變數時, 若使用這樣的語法: myType var1,var2 會導致grammar的conflict 可不可以改成用以下的方式宣告? type myType var2,var2 conflict的原因是當parser看到myType這個id時, 無法判斷這是個typename或是variable name 必須要偷看再下一個token才能決定之後的動作: 若是typename則繼續parsing宣告區的文法, 若是variable name則shift到statement區, 這樣就不是LALR(1)了。 ANS: You cannot do this! You may consider using a pre-processor to process the possible conflicts. It can be done, though the solution may be ugly. 41. 在自訂型態裡 型態裡的member的名稱可以和變數名或函數名重覆嗎? 自訂的型態可以有很多個嗎?(幾百個?) 自訂型態要做到支援陣列嗎? ANS: Yes to all the three questions above. A user defined structure gives a new scope. 42. 測試檔可以多人共用嘛?? ANS: There is a fine-line between direct copying and discussing. Please make sure you do not cross the line. 43. 測試檔的副檔名是 .p 還是 .prg 助教的投影片寫 .prg 老師的投影片寫 .p ANS: Please follow TA' slides.