解析C switch-case和AWK中的Statement

时间:2013-07-17 15:49:46

标签: c parsing for-loop awk switch-statement

如何用awk解析下面的switch-case语句?我想用awk创建简单的C语法检查器。此检查器必须读取代码并返回是否存在语法错误。如果有,awk应该打印出什么错误。

switch(number)
{
    case 1  : number = 'a'; break;
    case 2  : number = 'b'; break;
    default : number = 'x'; 
}

对于 for()语句,如下所示:

for(i=0;i<10;i++) 
    {
        number = 'A';
    }

switch-case 语句的当前代码是:

#parser_switchcase.awk
{
for(i=1; i<=NF; i++)
{
  if($i~/switch\([[:alnum:]]+\)/)
    print("switch(VALID_VARIABLE)")
}

}

我的第一个C switch-case 代码的结果:

master@master:~/Dokumen/Root$ awk -f parser_switchcase.awk soalswitch 
switch(VALID_VARIABLE)

但实际上它需要很多改进。它不完整。

我需要awk建议只是为了阅读和检查我上面输入的代码示例。确切地说,所以我只需要awk解析那些代码,而不是外部可能性,如附加功能,附加代码,只有上面代码中提到的内容。谢谢。

2 个答案:

答案 0 :(得分:0)

使用awk进行C语法检查是一个勇敢的项目。玩得开心!

我会使用gcc进行语法检查。试试这个:

gcc -fsyntax-only test.c

答案 1 :(得分:0)

正如其他人所说,awk不适合这项工作......
但是,如果您可以保证您的代码符合相当严格和精确的结构,例如问题中提供的结构,那么可以为它编写一个非常基本的awk解释器。

例如:

BEGIN {
  ERROR = "ERROR: ";
  WARNING = "WARNING: ";
}

# Start of switch statment
/switch/ { 
  # Cursory check for valid variable name: must start with a letter or underscore,
  # and be composed of alphanumeric characters or underscores.
  if ($0 !~ /switch\([A-Za-z_]+[A-Za-z0-9_]*\)/)
    print ERROR "switch statement '" $0 "' has a syntax error.";

  switch_stmnt = 1;
  next;
}

# Start of for statement
/for/ {
  # For loop can have lots of various stuff between parentheses, so hard to check.
  # But, if you know it will always be `(i=0;i<10;i++)`, then it's much easier to 
  # create a rule.
  if ($0 !~ /for\(.*;.*;.*\)/)
    print ERROR "for statement '" $0 "' has a syntax error.";

  for_stmnt = 1;
  next;
}

# Start of case statement
/case/ { 
  # Check if in switch
  if (! switch_stmnt)
    print ERROR "case statement '" $0 "' outside of switch statement.";

  # Already in a case statement
  if (case_stmnt)
    print WARNING "case statement fall-through.";

  # Check syntax
  if ($2 !~ /[A-Za-z0-9_]+/ || $3 != ":")
    print ERROR "case statement '" $0 "' has a syntax error.";

  case_stmnt = 1;
}

# Default
/default/ {
  # Check if in switch
  if (! switch_stmnt)
    print ERROR "default statement '" $0 "' outside of switch statement.";

  # Already in a case statement
  if (case_stmnt)
    print WARNING "case statement fall-through.";
}

# Break
/break;/ {
  if (case_stmnt) { case_stmnt = 0; }
  else if (for_stmnt) { }
  else { print ERROR "'break' outside of case statement or for loop."; }
}

# Start of control structure
/{/ { ++brace; }

# End of control structure
/}/ {
  if (switch_stmnt) {
    switch_stmnt = 0;
    case_stmnt = 0;
  }
  else if (for_stmnt)
    for_stmnt = 0;

  if (brace == 0)
    print ERROR "Extra closing brace '}' with no matching open brace.";

  --brace;
}

{
  # Do syntax checking on regular lines, eg. "number = 'a';"
  next;
}

END {
  if (switch_stmnt || for_stmnt || brace)
    print ERROR "Unterminated for or switch statement at end of file.";
}

这会检查一些语句是否符合一些规则。您可以使用更多正则表达式规则和标志来扩展它。特别困难的是没有关键词的简单陈述,因为这些可以是声明,作业,函数调用等。但是,如果你只是如上所述进行number = 'a';的作业,那么匹配这些作品也不是很难行(类似于/[A-Za-z_]+[A-Za-z0-9_]* = '.'/