我目前正在爲我的玩具語言編寫解析器,並作爲解析器的一部分,我已經寫好打印函數,以及...基本上打印它的論點。對於字符串常量,它是所有打印字符串(字符*)從文件加載沒有逃脫特殊序列(如 n)
printf("%s", pointer);
所以
print("\n")
應
printf("%s", ptr_to_loaded_string);
執行(或多或少)
但是,我現在的問題是, C讀取腳本文件時會轉義特殊字符序列。因此,而不是「\ n」我得到「\\ n」。
我的問題是:有沒有什麼辦法可以避免這個序列的轉義,如果不是,處理它們的最好方法是什麼?我目前正在考慮搜索和替換 - 用'\'替換2'\'的每個序列,但它可能有點問題(字符串長度變化,重新分配等) - 我想避免這種解決方案,除非它是絕對有必要。
編輯:哎呀,計算器逃過我的例子....
是否該腳本文件包含換行符,或字符''\''後面跟着一個字符'N'? –
當前腳本文件只包含1行:'print(「test \ n」)'並且導致打印'test \ n' – stollgrin
正確,因爲轉義序列僅在_literal_字符串中被替換,即在你的C源代碼中。 –