C解析textfile中的注释

时间:2018-03-04 03:47:15

标签: c parsing lexer

所以我试图实现一个非常简单的解析器来读取文件并执行一些命令。我猜这与bash脚本非常相似,但更简单。鉴于您可以使用#表示的注释,我无法弄清楚如何标记文件的内容。为您提供源文件外观的示例

# Script to move my files across
# Author: Person Name 

# First delete files
 "rm -rf ~/code/bin/debug";
 "rm -rf ~/.bin/tmp"; #deleting temp to prevent data corruption
# Dump file contents
 "cat ~/code/rel.php > log.txt";

到目前为止,这是我的代码。请注意,我基本上使用这个小项目作为一种变得更加舒适和熟悉C的方法。所以请原谅代码中的任何明显缺陷。非常感谢您的反馈。

// New line.
#define NL '\n'
// Quotes.
#define QT '"'
// Ignore comment.
#define IGN '#'

int main() {
  if (argc != 2) {
    show_help();
    return 0;
  }
  FILE *fptr = fopen(argv[1], "r");
  char *buff;
  size_t n = 0;
  int readlock = 0;
  int qread = 0;
  char c;

  if (fptr == NULL){
     printf("Error: invalid file provided %s for reading", argv[1]);
     exit(1);
  }

 fseek(fptr, 0, SEEK_END);
 long f_size = ftell(fptr);
 fseek(fptr, 0, SEEK_SET);
 buff = calloc(1, f_size);

 // Read file contents.
 // Stripping naked whitespace and comments.
 // qread is when in quotation mode. Everything is stored even '#' until EOL or EOF.
 while ((c = fgetc(fptr)) != EOF) {
    switch(c) {
        case IGN :
            if (qread == 0) {
                readlock = 1;
            }
            else {
                buff[n++] = c;
            }
            break;
        case NL :
            readlock = 0;
            qread = 0;
            break;
        case QT :
            if ((readlock == 0 && qread == 0) || (readlock == 0 && qread == 1)) {
                // Activate quote mode.
                qread = 1;
                buff[n++] = c;
            }
            else {
                qread = 0;
            }
            break;
        default :
            if ((qread == 1 && readlock == 0) || (readlock == 0  && !isspace(c))) {
                buff[n++] = c;
            }
            break;
    }
 }
fclose(fptr);
printf("Buffer contains %s \n", buff);
free(buff);

return 0;

}

所以上述解决方案有效,但我的问题是......是否有更好的方法来实现预期的结果?目前我实际上并没有“标记化”任何东西。当前的实现是否缺乏基于字符创建令牌的逻辑?

1 个答案:

答案 0 :(得分:2)

通过整行阅读文件更容易:

def addAccount():
    print ("You have chosen to add an account")
    proceed = 1
    while proceed != 0:
        proceed = int(input("Please enter any number except 0 to proceed. If you enter 0, the process will be cancelled: "))
        addName = str(input("Please enter the name of the new account: "))
        addBirthday = str(input("Please enter the birthday of the user (month day, year) :"))
        addAddress = str(input("Please enter the address of the user: "))
        addHkid = str(input("Please enter the HK IDcard number: "))
        addBalance = 0
        CustomerList.append(Customer(addName, addBirthday, addAddress, addHkid, addBalance))