C99 Language syntax summary

/*
 =========================================================
 Name        : C99 sntax.txt
 Author      : luqi
 Version     :
 Copyright   : Your copyright notice
 Description : C99 Language syntax summary
 ==========================================================
 */
 
/**************************************************************************
        Lexical grammar                                                                                                     
**************************************************************************/
token:
        keyword             // 关键字
        identifier            // 标识符
        constant            // 常量
        string_literal      // 字符串字面值
        punctuator         // 标点符号(算符)
        
 
/*
 * Keywords
 */
keyword:
        auto        break       case        char        const       continue
        default     do          double      else        enum        extern
        float       for         goto        if          inline      int
        long        register    restrict    return      short       signed
        sizeof      static      struct      switch      typedef     union
        unsigned    void        volatile    while       _Bool       _Complex
        _Imaginary
        
/*  
 * Identifiers  
 * /
identifier:
        identifier_nondigit
        identifier identifier_nondigit
        identifier digit
        
identifier_nondigit:
        nondigit
        universal_character_name                          // C99 新增
        other implementation_defined characters     // C99 新增
        
nondigit:
        a-z
        A-Z
        _
        
digit:
        0-9

/*        
 * Universal character names        
 */
universal_character_name:
        \u hex_quad
        \U hex_quad hex_quad
        
hex_quad:
        hexadecimal_digit hexadecimal_digit hexadecimal_digit hexadecimal_digit

/*
 * Constants
 */
constant:
        integer_constant        
        floating_constant
        enumeration_constant
        character_constant
 
//***integer_constant
integer_constant:
        decimal_constant integer_suffix ?
        octal_constant integer_suffix ?
        hexadecimal_constant integer_suffix ?
        
integer_suffix:
        unsigned_suffix long_suffix ?
        unsigned_suffix long_long_suffix
        long_suffix unsigned_suffix ?
        long_long_suffix unsigned_suffix ?   

unsigned_suffix:
        u
        U

long_suffix: one of
        l
        L
        
long_long_suffix: one of
        ll
        LL
        
decimal_constant:
        nonzero_digit
        decimal_constant digit
        
octal_constant:
        0
        octal_constant octal_digit

hexadecimal_constant:
        hexadecimal_prefix hexadecimal_digit
        hexadecimal_constant hexadecimal_digit  

hexadecimal_prefix:
        0x
        0X
        
nonzero_digit:
        1-9
        
octal_digit:
        0-7    

hexadecimal_digit:
        0-9
        a-f
        A-F  
   

//***floating_constant      
floating_constant:
        decimal_floating_constant
        hexadecimal_floating_constant    

decimal_floating_constant:
        fractional_constant exponent_part ? floating_suffix ?
        digit_sequence exponent_part floating_suffix ?
        
fractional_constant:
        digit_sequence ? . digit_sequence
        digit_sequence .
        
exponent_part:
        e sign ? digit_sequence
        E sign ? digit_sequence

sign:
        +
        -
        
digit_sequence:
        digit
        digit_sequence digit
        
        
hexadecimal_floating_constant:
        hexadecimal_prefix hexadecimal_fractional_constant binary_exponent_part floating_suffix ?
        hexadecimal_prefix hexadecimal_digit_sequence binary_exponent_part floating_suffix ?
        
hexadecimal_fractional_constant:
        hexadecimal_digit_sequence ? . hexadecimal_digit_sequence
        hexadecimal_digit_sequence .
        
binary_exponent_part:
        p sign ? digit_sequence
        P sign ? digit_sequence
        
hexadecimal_digit_sequence:
        hexadecimal_digit
        hexadecimal_digit_sequence hexadecimal_digit

floating_suffix:
        f
        l
        F
        L

//***enumeration_constant        
enumeration_constant:
        identifier
 
//***character_constant   
character_constant:
        ' c_char_sequence '
        L' c_char_sequence '
        
c_char_sequence:
        c_char
        c_char_sequence c_char
        
c_char:
        any member of the source character set except the single_quote ', backslash \, or new_line character
        escape_sequence
        
escape_sequence:
        simple_escape_sequence
        octal_escape_sequence
        hexadecimal_escape_sequence
        universal_character_name
        
simple_escape_sequence:
        \' \" \? \\ \a
        \b \f \n \r \t \v
        
 
octal_escape_sequence:
        \ octal_digit
        \ octal_digit octal_digit
        \ octal_digit octal_digit octal_digit
        
hexadecimal_escape_sequence:
        \x hexadecimal_digit
        hexadecimal_escape_sequence hexadecimal_digit
        
/*
 * String literals
 */
 
string_literal:
        " s_char_sequence ? "
        L" s_char_sequence ? "
        
s_char_sequence:
        s_char
        s_char_sequence s_char

s_char:        
        any member of the source character set except the double_quote ", backslash \, or new_line character
        escape_sequence
        
/*
 * Punctuators
 */
 
punctuator:
        [ ] ( ) { } . ->
        ++  --   &  * + - ~ !
        / % << >> < > <= > == = != ^ | && ||
        ? : ; ...
        = *= /= %= += -= <<= >>= &= ^= |=
        , # ##
        <: :> <% %> %: %:%:
        
        
/**************************************************************************
        Phrase structure grammar                                                                                      
**************************************************************************/       
/**
 * Expressions
 */
 
primary_expression:
        identifier
        constant
        string_literal
        ( expression )
        
/**
 * 后缀表达式,优先级最高
 */
postfix_expression:             
        primary_expression                                                      // exp
        postfix_expression [ expression ]                                  // exp[], exp[pxe]
        postfix_expression ( argument_expression_list ? )         // exp(), exp[pxe1,pxe2,pxe3]
        postfix_expression . identifier                                       // exp.idn
        postfix_expression -> identifier                                    // exp->idn
        postfix_expression ++                                                 // exp++
        postfix_expression --                                                   // exp--
        ( type_name ){ initializer_list }  // fifi          // (int *){1, 2, 3}               C99 新增
        ( type_name ){ initializer_list ,} // fifi          // (int *){1, 2, 3,}              C99 新增 字面值常量
        
argument_expression_list:
        assignment_expression
        argument_expression_list , assignment_expression

unary_expression:
        postfix_expression
        ++ unary_expression
        -- unary_expression
        unary_operator cast_expression
        sizeof unary_expression
        sizeof ( type_name )
        
unary_operator:
        & * + - ~ !

cast_expression:
        unary_expression
        ( type_name ) cast_expression
        
multiplicative_expression:
        cast_expression
        multiplicative_expression * cast_expression
        multiplicative_expression / cast_expression
        multiplicative_expression % cast_expression
        
additive_expression:
        multiplicative_expression
        additive_expression + multiplicative_expression
        additive_expression - multiplicative_expression
        
shift_expression:
        additive_expression
        shift_expression << additive_expression
        shift_expression >> additive_expression
        
relational_expression:
        shift_expression
        relational_expression < shift_expression
        relational_expression > shift_expression
        relational_expression <= shift_expression
        relational_expression >= shift_expression
        
equality_expression:
        relational_expression
        equality_expression == relational_expression
        equality_expression != relational_expression
        
AND-expression:
        equality_expression
        AND-expression & equality_expression
        
        
exclusive-OR-expression:
        AND-expression
        exclusive-OR-expression ^ AND-expression
        
inclusive-OR-expression:
        exclusive-OR-expression
        inclusive-OR-expression | exclusive-OR-expression

logical-AND-expression:
        inclusive-OR-expression
        logical-AND-expression && inclusive-OR-expression
        
logical-OR-expression:
        logical-AND-expression
        logical-OR-expression || logical-AND-expression
        
conditional_expression:
        logical-OR-expression
        logical-OR-expression fi expression : conditional_expression
        
assignment_expression:
        conditional_expression
        unary_expression assignment_operator assignment_expression
        
assignment_operator: one of
        = *= /= %= += -= <<= >>= &= ^= |=
        
expression:
        assignment_expression
        expression , assignment_expression
        
constant_expression:            //
        conditional_expression
        
/**************************************************************************
        Declarations                                                                                                           
**************************************************************************/
declaration:
        declaration_specifiers init_declarator_list ? ;
        
declaration_specifiers:
        storage_class_specifier declaration_specifiers ?
        type_specifier declaration_specifiers ?
        type_qualifier declaration_specifiers ?
        function_specifier declaration_specifiers ?
        
init_declarator_list:
        init_declarator
        init_declarator_list , init_declarator
        
init_declarator:
        declarator
        declarator = initializer
        
storage_class_specifier:
        typedef
        extern
        static
        auto
        register
        
type_specifier:
        void
        char
        short
        int
        long
        float
        double
        signed
        unsigned
        _Bool
        _Complex
        _Imaginary
        struct_or_union_specifier
        enum_specifier
        typedef_name
        
struct_or_union_specifier:
        struct_or_union identifier ? { struct_declaration_list }
        struct_or_union identifier

struct_or_union:
        struct
        union
        
struct_declaration_list:
        struct_declaration
        struct_declaration_list struct_declaration
        
struct_declaration:
        specifier_qualifier_list struct_declarator_list ;
        
specifier_qualifier_list:
        type_specifier specifier_qualifier_list ?
        type_qualifier specifier_qualifier_list ?
        
struct_declarator_list:
        struct_declarator
        struct_declarator_list , struct_declarator
        
struct_declarator:
        declarator
        declarator ? : constant_expression
        
enum_specifier:
        enum identifier ? { enumerator_list }
        enum identifier ? { enumerator_list ,}
        enum identifier
        
enumerator_list:
        enumerator
        enumerator_list , enumerator
    
enumerator:
        enumeration_constant
        enumeration_constant = constant_expression
        
type_qualifier:
        const
        restrict
        volatile
 
function_specifier:
        inline
        
declarator:
        pointer ? direct_declarator
        
direct_declarator:
        identifier
        ( declarator )
        direct_declarator [ type_qualifier_list ? assignment_expression ? ]
        direct_declarator [ static type_qualifier_list ? assignment_expression ]
        direct_declarator [ type_qualifier_list static assignment_expression ]
        direct_declarator [ type_qualifier_list ? *]
        direct_declarator ( parameter_type_list )
        direct_declarator ( identifier_list ? )
        
pointer:
        * type_qualifier_list ?
        * type_qualifier_list ? pointer
        
type_qualifier_list:
        type_qualifier
        type_qualifier_list type_qualifier
        
parameter_type_list:
        parameter_list
        parameter_list , ...
        
parameter_list:
        parameter_declaration
        parameter_list , parameter_declaration
        
parameter_declaration:
        declaration_specifiers declarator
        declaration_specifiers abstract_declarator ?
        
identifier_list:
        identifier
        identifier_list , identifier
        
type_name:  // fi
        specifier_qualifier_list abstract_declarator ?

abstract_declarator:
        pointer
        pointer ? direct_abstract_declarator
        
direct_abstract_declarator:
        ( abstract_declarator )
        direct_abstract_declarator ? [ assignment_expression ? ]
        direct_abstract_declarator ? [*]
        direct_abstract_declarator ? ( parameter_type_list ? )
        
typedef_name:   // fi
    identifier
    
initializer:    // fi
        assignment_expression
        { initializer_list }
        { initializer_list ,}
        
initializer_list:
        designation ? initializer
        initializer_list , designation ? initializer
        
designation:
        designator_list =
        
designator_list:
        designator
        designator_list designator
        
designator:
        [ constant_expression ]
        . identifier
        
/**************************************************************************
        Statements                                                                                                            
**************************************************************************/
statement:
        labeled_statement
        compound_statement
        expression_statement
        selection_statement
        iteration_statement
        jump_statement
        

labeled_statement:
        identifier : statement
        case constant_expression : statement
        default : statement
        
compound_statement:
        { block_item_list ? }

block_item_list:
        block_item
        block_item_list block_item
        
block_item:
        declaration
        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 ? ; expression ? ; expression ? ) statement
        for ( declaration expression ? ; expression ? ) statement

jump_statement:
        goto identifier ;
        continue ;
        break ;
        return expression ? ;
        
/**************************************************************************
        External definitions                                                                                                  
**************************************************************************/
        
translation_unit:
        external_declaration
        translation_unit external_declaration
        
external_declaration:
        function_definition
        declaration
 
function_definition:
        declaration_specifiers declarator declaration_list ? compound_statement
        
declaration_list:
        declaration
        declaration_list declaration

你可能感兴趣的:(language)