C++程序设计语言的文法
时间: 2023-11-11 21:05:38 浏览: 141
C++程序设计语言的文法可以采用巴克斯-诺尔范式(Backus-Naur Form,BNF)或扩展巴克斯-诺尔范式(Extended BNF,EBNF)进行描述。下面是一份简化的C++ BNF文法:
- program: declaration_list
- declaration_list: declaration | declaration_list declaration
- declaration: function_declaration | variable_declaration
- function_declaration: type_specifier function_name ( parameter_list ) compound_statement
- variable_declaration: type_specifier variable_list ;
- variable_list: variable | variable_list , variable
- variable: identifier | identifier = expression
- type_specifier: int | float | double | char | void
- parameter_list: parameter | parameter_list , parameter
- parameter: type_specifier identifier
- statement: compound_statement | expression_statement | selection_statement | iteration_statement | jump_statement
- compound_statement: { statement_list }
- statement_list: statement | statement_list statement
- expression_statement: expression ;
- selection_statement: if ( expression ) statement | if ( expression ) statement else statement | switch ( expression ) statement
- iteration_statement: while ( expression ) statement | do statement while ( expression ) ; | for ( expression_statement ; expression_statement ; expression ) statement
- jump_statement: return expression ; | break ; | continue ;
- expression: assignment_expression | expression , assignment_expression
- assignment_expression: logical_or_expression | unary_expression assignment_operator assignment_expression
- assignment_operator: = | *= | /= | %= | += | -= | <<= | >>= | &= | ^= | |=
- logical_or_expression: logical_and_expression | logical_or_expression || logical_and_expression
- logical_and_expression: inclusive_or_expression | logical_and_expression && inclusive_or_expression
- inclusive_or_expression: exclusive_or_expression | inclusive_or_expression | exclusive_or_expression
- exclusive_or_expression: and_expression | exclusive_or_expression ^ and_expression
- and_expression: equality_expression | and_expression & equality_expression
- equality_expression: relational_expression | equality_expression == relational_expression | equality_expression != relational_expression
- relational_expression: shift_expression | relational_expression < shift_expression | relational_expression > shift_expression | relational_expression <= shift_expression | relational_expression >= shift_expression
- shift_expression: additive_expression | shift_expression << additive_expression | shift_expression >> additive_expression
- additive_expression: multiplicative_expression | additive_expression + multiplicative_expression | additive_expression - multiplicative_expression
- multiplicative_expression: unary_expression | multiplicative_expression * unary_expression | multiplicative_expression / unary_expression | multiplicative_expression % unary_expression
- unary_expression: postfix_expression | ++ unary_expression | -- unary_expression | unary_operator unary_expression
- postfix_expression: primary_expression | postfix_expression [ expression ] | postfix_expression ( argument_expression_list ) | postfix_expression . identifier | postfix_expression -> identifier | postfix_expression ++ | postfix_expression --
- primary_expression: identifier | constant | string_literal | ( expression ) | function_name ( ) | function_name ( argument_expression_list )
- argument_expression_list: assignment_expression | argument_expression_list , assignment_expression
这个文法描述了C++程序设计语言的基本语法结构,包括程序、声明、函数、变量、语句、表达式等。其中,program是程序的入口,由一系列声明组成;declaration_list表示一系列声明;declaration表示函数或变量声明;function_declaration表示函数声明;variable_declaration表示变量声明;type_specifier表示类型说明符;parameter_list表示函数参数列表;statement表示语句;compound_statement表示复合语句;expression_statement表示表达式语句;selection_statement表示选择语句;iteration_statement表示迭代语句;jump_statement表示跳转语句;expression表示表达式;assignment_expression表示赋值表达式;logical_or_expression表示逻辑或表达式;logical_and_expression表示逻辑与表达式;inclusive_or_expression表示按位或表达式;exclusive_or_expression表示按位异或表达式;and_expression表示按位与表达式;equality_expression表示等于表达式;relational_expression表示关系表达式;shift_expression表示移位表达式;additive_expression表示加减表达式;multiplicative_expression表示乘除模表达式;unary_expression表示一元表达式;postfix_expression表示后缀表达式;primary_expression表示基本表达式;argument_expression_list表示函数参数表达式列表。
阅读全文