compiler-inservice-s22 / Discussion

MIT License
0 stars 0 forks source link

What's the output of our scanner if the input is a string but in wrong format? #4

Closed PlusoneWang closed 2 years ago

PlusoneWang commented 2 years ago

Hi TAs,

I went through all the test cases provided in hw1 and there are no test cases that contain strings that missing final double quotes. For example, for the following input:

ss := """"";

From what I understand, the output of our scanner might be:

<id: ss>
<:=>
<string: "";>

or:

<id: ss>
<:=>

It seems like that the scanner of hw1 is unable to detect the wrong string format, am I right?

LittleLaGi commented 2 years ago

同學你好:

ss := """"";

一般來說的話 (按照順序讀 token 進來),scanner 的行為應該會如下:

  1. match 到兩個 ",得到第一個 string
  2. match 到兩個 ",得到第二個 string (跟第一個 string 合併成一個)
  3. match 到一個 ",接下來 match 到的東西都會被當成 string 的內容
  4. match 到 ;,當作 string 的內容
  5. match 到 \n,因為 string 內不能含有換行符號,所以報錯

所以它應該要能偵測到錯誤才對。


若以上回答有回答到同學的問題,請對助教的回覆加上 emoji 👍,助教會幫此 issue 加上「已解決」的標籤,感謝同學的配合。

PlusoneWang commented 2 years ago

Hi TA,

Thanks for your reply.

  1. match 到兩個 ",得到第一個 string

Do you mean that the first two " will get an empty string?

  1. match 到兩個 ",得到第二個 string (跟第一個 string 合併成一個)

I don't understand what you mean by 跟第一個 string 合併成一個.

LittleLaGi commented 2 years ago

Do you mean that the first two " will get an empty string?

是的

I don't understand what you mean by 跟第一個 string 合併成一個.

這個在 spec 中有提到: A double quote can be placed within a string constant by writing two consecutive double quotes. For example, an input "aa""bb" denotes the string constant aa"bb.

  1. match 到兩個 ",得到第二個 string (跟第一個 string 合併成一個)`

這個部分改成先把連續兩個 " 變成一個,然後接下來 match 到的都當成 string 的內容會比較好,做起來會比較流暢。

PlusoneWang commented 2 years ago

Hi @LittleLaGi, Thank you for the reply, I'll take more think about the parsing of consecutive double quotes. As the major question "error detection" has been explicitly answered by 5. match 到 \n,因為 string 內不能含有換行符號,所以報錯. I think this issue can be closed and marked as solved.