请根据ISO/IEC 9899:201x的文法集推导代码 int main() { printf("Hello world!\n"); return 0; }
时间: 2024-05-22 15:13:46 浏览: 103
程序的起始符号是translation-unit(翻译单元),可以推导出以下文法产生式:
translation-unit -> external-declaration
external-declaration -> function-definition
function-definition -> declaration-specifiers declarator compound-statement
declaration-specifiers -> storage-class-specifier
storage-class-specifier -> typedef
declarator -> direct-declarator
direct-declarator -> direct-declarator ( ) // 函数名
compound-statement -> { block-item-list }
block-item-list -> block-item
block-item -> statement
statement -> expression-statement
expression-statement -> expression ;
expression -> primary-expression
primary-expression -> identifier
identifier -> main
primary-expression -> ( expression )
expression -> argument-expression-list
argument-expression-list -> assignment-expression
assignment-expression -> conditional-expression
conditional-expression -> logical-OR-expression
logical-OR-expression -> logical-AND-expression
logical-AND-expression -> inclusive-OR-expression
inclusive-OR-expression -> exclusive-OR-expression
exclusive-OR-expression -> AND-expression
AND-expression -> equality-expression
equality-expression -> relational-expression
relational-expression -> shift-expression
shift-expression -> additive-expression
additive-expression -> multiplicative-expression
multiplicative-expression -> cast-expression
cast-expression -> unary-expression
unary-expression -> postfix-expression
postfix-expression -> primary-expression
primary-expression -> string-literal
string-literal -> " Hello world! \n "
postfix-expression -> ( )
statement -> return expression ;
expression -> primary-expression
primary-expression -> integer-constant
integer-constant -> 0
statement -> ;
compound-statement -> }
推导后得到的程序代码如下:
typedef int storage_class_specifier;
int main() {
"Hello world!\n";
return 0;
}
阅读全文