文法分析1

14馮梓凡發表於2015-11-01

<translation-unit> ::= {<external-declaration>}*

<external-declaration> ::= <function-definition>
| <declaration>

<翻譯單位>:= {對外宣告> } *      //函式的定義

<declaration-specifier> ::= <storage-class-specifier>            //類的定義
| <type-specifier>
| <type-qualifier>

<儲存類說明符>::=“自動”      //自動儲存期,靜態儲存期,外部變數,定義型別
| "register"
| "static"
| "extern"
| "typedef"

<type-specifier> ::= "void"        //型別說明符
| "char"
| "short"
| "int"
| "long"
| "float"
| "double"
| "signed"
| "unsigned"
| <struct-or-union-specifier>
| <enum-specifier>
| <typedef-name>

<struct-or-union-specifier> ::= <struct-or-union> <identifier> "{" {<struct-declaration>}+ "}"      //結構或聯合說明符
| <struct-or-union> "{" {<struct-declaration>}+ "}"
| <struct-or-union> <identifier>

<struct-or-union> ::= "struct"       //結構體
| "union"

<struct-declaration> ::= {<specifier-qualifier>}* <struct-declarator-list>      //結構宣告

<specifier-qualifier> ::= <type-specifier>           //限定型別說明符
| <type-qualifier>

<struct-declarator-list> ::= <struct-declarator>        //struct宣告
| <struct-declarator-list> "," <struct-declarator>    

<struct-declarator> ::= <declarator>                       //struct宣告
| <declarator> ":" <constant-expression>
| ":" <constant-expression>

<declarator> ::= {<pointer>}? <direct-declarator>      
<pointer> ::= "*" {<type-qualifier>}* {<pointer>}?     //指標

<type-qualifier> ::= "const"          //const常量,是一個c語言關鍵字

| "volatile"

<direct-declarator> ::= <identifier>        //直接宣告,識別符號
| "(" <declarator> ")"
| <direct-declarator> "[" {<constant-expression>}? "]"
| <direct-declarator> "(" <parameter-type-list> ")"
| <direct-declarator> "(" {<identifier>}* ")"
                                                                                
<constant-expression> ::= <conditional-expression>           //條件表示式

<conditional-expression> ::= <logical-or-expression>                //三位運算子
| <logical-or-expression> "?" <expression> ":" <conditional-expression>

<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>

<multiplicative-expression> ::= <cast-expression>                 //'*,/,%' 
| <multiplicative-expression> "*" <cast-expression>
| <multiplicative-expression> "/" <cast-expression>
| <multiplicative-expression> "%" <cast-expression>

<cast-expression> ::= <unary-expression>            //一元運算子
| "(" <type-name> ")" <cast-expression>

<unary-expression> ::= <postfix-expression>
| "++" <unary-expression>               //自增
| "--" <unary-expression>                 //自減
| <unary-operator> <cast-expression>
| "sizeof" <unary-expression>                   //動態分配空間
| "sizeof" <type-name>

<postfix-expression> ::= <primary-expression>       //字尾表示式
| <postfix-expression> "[" <expression> "]"
| <postfix-expression> "(" {<assignment-expression>}* ")"    
| <postfix-expression> "." <identifier>
| <postfix-expression> "->" <identifier>
| <postfix-expression> "++"
| <postfix-expression> "--"

<primary-expression> ::= <identifier>     //字首表示式
| <constant>
| <string>
| "(" <expression> ")"

<constant> ::= <integer-constant>            //整型常量
| <character-constant>                              //字元常量
| <floating-constant>                                 //浮點常量
| <enumeration-constant>                          //列舉常量

<expression> ::= <assignment-expression>     //賦值表示式
| <expression> "," <assignment-expression>

<assignment-expression> ::= <conditional-expression>      //賦值表示式 
| <unary-expression> <assignment-operator> <assignment-expression>

<assignment-operator> ::= "="         //賦值操作符
| "*="
| "/="
| "%="
| "+="
| "-="
| "<<="
| ">>="
| "&="
| "^="
| "|="
<!--[endif]-->

<unary-operator> ::= "&"         //運算子
| "*"
| "+"
| "-"
| "~"
| "!"

<type-name> ::= {<specifier-qualifier>}+ {<abstract-declarator>}?   //修飾符

<parameter-type-list> ::= <parameter-list>        //引數型別列表
| <parameter-list> "," ...

<parameter-list> ::= <parameter-declaration>     //引數宣告
| <parameter-list> "," <parameter-declaration>

<parameter-declaration> ::= {<declaration-specifier>}+ <declarator>       //宣告說明符
| {<declaration-specifier>}+ <abstract-declarator>
| {<declaration-specifier>}+

<abstract-declarator> ::= <pointer>      

| <pointer> <direct-abstract-declarator>//指標,直接摘要說明

| <direct-abstract-declarator>

<direct-abstract-declarator> ::= ( <abstract-declarator> )           //直接宣告符
| {<direct-abstract-declarator>}? "[" {<constant-expression>}? "]"
| {<direct-abstract-declarator>}? "(" {<parameter-type-list>|? ")"

<enum-specifier> ::= "enum" <identifier> "{" <enumerator-list> "}"      //列舉宣告符
| "enum" "{" <enumerator-list> "}"
| "enum" <identifier>

<enumerator-list> ::= <enumerator>           //列舉器
| <enumerator-list> "," <enumerator>

<enumerator> ::= <identifier>            //列舉識別符號           
| <identifier> "=" <constant-expression>

<typedef-name> ::= <identifier>     //識別符號

<declaration> ::= {<declaration-specifier>}+ {<init-declarator>}*      //初始化,宣告說明符 

<init-declarator> ::= <declarator>                   // 初始化宣告符
| <declarator> "=" <initializer>

<initializer> ::= <assignment-expression>          //初始化
| "{" <initializer-list> "}"
| "{" <initializer-list> "," "}"

<initializer-list> ::= <initializer>                //初始化列表
| <initializer-list> "," <initializer>

<compound-statement> ::= "{" {<declaration>}* {<statement>}* "}"          //複合語句

<statement> ::= <labeled-statement>                   //標記語句

 

| <expression-statement>         //表示式語句
| <compound-statement>         //複合語句
| <selection-statement>           //選擇語句
| <iteration-statement>           //迭代語句
| <jump-statement>               //跳轉語句

<labeled-statement> ::= <identifier> ":" < statement>       //標記語句,識別符號      
| "case" <constant-expression> ":" <statement>
| "default" ":" <statement>

<expression-statement> ::= {<expression>}? ";"              //表示式語句

<selection-statement> ::= "if" "(" <expression> ")" <statement>       //選擇語句
| "if" "(" <expression> ")" <statement> "else" <statement>            //條件語句
| "switch" "(" <expression> ")" <statement>                           //switch

<iteration-statement> ::= "while" "(" <expression> ")" <statement>           //while do whilefor迴圈語句
| "do" <statement> "while" "(" <expression> ")" ";"
| "for" "(" {<expression>}? ";" {<expression>}? ";" {<expression>}? ")" <statement>

<jump-statement> ::= "goto" <identifier> ";"                       //goto跳轉語句
| "continue" ";"                                     //繼續
| "break" ";"                                        //結束
| "return" {<expression>}? ";"                     //返回

 

相關文章