expressions are made up by applying operators to expressions

Upload: watarderrr

Post on 30-May-2018

235 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    1/24

    1. 1.

    Expressions are made up by applying operators to primary_expressions.1. primary_expression::= variable | constant | string_literal | "(" expression ")",

    2. variable::= identifier & declared and in scope of declaration.3. argument_list::=List(assignment_expression),

    OperatorsSymbol See

    "("... ")" primary_expression cast_expression function_call

    "." part of a structure

    "-" additive_expression unary_expression"->" part of a pointed at structure

    "--"unary_expression postfix_expression

    "-=" assignment_expression

    "&" AND_expression bitwise Boolean"&=" assignment_expression

    "&" address_of unary_expression"&&" logical_AND_expression

    "*" multiplicative_expression contents of pointer unary_expression

    "*=" assignment_expression

    "+" additive_expression unary_expression"++" unary_expression postfix_expression

    "+=" assignment_expression

    "~" bitwise negation prefix"!" logical negation prefix

    "!=" equality_expression"sizeof" unary_expression"/" multiplicative_expression divide

    "/=" assignment_expression

    "%" multiplicative_expression mod"%=" assignment_expression

    ">" shift_expression right">=" relational_expression

    ">>=" assignment_expression

    "==" equality_expression"="assignment_expression

    "^" XOR_expression exclusive-or bitwise

    "^=" assignment_expression

    "|" OR_expression bitwise or

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    2/24

    "||" logical_OR_expression

    "||=" assignment_expression

    ..."?"... ":"... conditional_expression"," expression (discard previous value)

    Arithmetic4. post_fix::="++" | "--",

    5. post_fix_expression::=(primary_expression) #(post_fix),

    6. unary_operator::="&" | "*" | "+" | "-" | "!" | "-",

    7. pre_fix::="++" | "--" | "sizeof",

    8. unary_expression::=#(pre-fix) post_fix_expression | unary_operator

    cast_expression | "sizeof" "(" type_name")",

    9. cast_expression::=#(type_name) unary_expression. This implies that casts are

    done after doing post-fix operations..

    10. multiplicative_expression::=S(cast_expression, multiplicative_operator).

    [ serial_operator_expression ]

    The rule above means that 'casts' are done before multiplication and division,

    and that multiplication and division are done from left to right.

    11. multiplicative_operator::="*" | "%" | "/",

    12. additive_expression::=S(multiplicative_expression, additive_operator). Thismeans that addition and subtraction occurs after multiplication and from left to right.

    13. additive_operator::="+" | "-",

    Shifts

    14. shift_expression::=S(additive_expression, shift_operator),

    15. shift_operator::=">>" | "" is the reverse and divides by 2.

    Relations

    16. relational_expression::= S(shift_expression, relational_operator),

    17. relational_operator::="" | "=",

    18. equality_expression::=S(relational_expression, equality_operator),

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    3/24

    19. equality_operator::="==" | "!=",

    Bitwise ExpressionsThese use the lowest level machine code operations that manipulate the bits in

    words. They are very fast and powerful. They are mostly used in system programming:

    drivers, operating systems, compilers, interpreters, shells, ... . They are also a very fastand tight coding for subsets of small sets: one bit per element has 1 for membership and 0

    for nonmembership. For example if a class can meet on any collection of the 5 working

    days in a week (Monday=1, ..., Friday=5) then

    FRWTM

    10101 = MWF

    Decimal Binary (last 4 bits/ 1 byte)

    0 0000

    1 00012 0010

    3 00114 0100

    5 0101

    6 0110

    7 01118 1000

    20. AND_expression::=S(equality_expression, and_operator),

    21. and_operator::="&", This operator takes each bit in the value of its

    arguments in turn to calculate the bit in the answer. A bit is 1 if and only if botharguments have bits in that place that are 1.

    Decimal Binary

    3 00115 0101

    3&5 0001

    22. XOR_expression::=S(AND_expression, XOR_operator),

    23. XOR_operator::="^", XOR is short for eXclusive-OR. The n'th bit in the

    value is 1 precisly when the n'th bits in the two arguments are different.Decimal Binary

    3 0011

    5 01013^50110

    24. OR_expression::=S(XOR_expression, OR_operator),

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    4/24

    25. OR_operator::="|", This operator takes each bit in the value of its arguments

    in turn to calculate the bit in the answer. The n'th bit is 1 if either n'th bits is 1.

    Decimal Binary3 0011

    5 0101

    3|5 0111

    Logical Expressions

    In C, logical false is reresented by any zero value and true by any nonzerovalue. Here is a list of operators

    1. and::="&&".

    2. or::="||",

    3. not::="!",

    26. logical_AND_expression::=S(OR_expression, logical_AND_operator),

    27. logical_AND_operator::=and, A&&B is true precisely when both A and Bevaluate to be true. If A evaluates to false, B is not evaluated.

    28. logical_OR_expression::=S(logical_AND_expression,

    logical_OR_operator),

    29. logical_OR_operator::=or, A||B is true if A evaluates to be true, or when A isfalse and B evaluates to be true. If both evaluate to false (zero) then A||B is false.

    Conditional Expressions30. conditional_expression::=logical_OR_expression | logical_OR_expression

    "?" expression ":" conditional_expression,

    Assignment Statements

    31. assignment_expression::=S(unary_expression, assignment_operator),

    32. assignment_operator::="=" | "*=" | "/=" | "%=" | "+=" | "=" | "&="

    | "^=" | "|=",

    33. expression::=List(assignment_expression ),

    34. constant_expression::=conditional_expression,

    . . . . . . . . . ( end of section Expressions)

    Declarations

    1. declaration::=declaration_specifier | declarator_list,

    2. declarator_list::=List(declarator_initialized),

    3. declaration_specifier::=(storage_class | type_specifier | type_qualifier),

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    5/24

    4. storage_class::="typedef" | "extern" | "static" | "auto" | "register",

    Types

    5. type_specifier::="void" | "char" | "short" | "int" | "long" | "float" | "double" |

    "signed" | "unsigned" | struct_union_specifier | enumeration_specifier | typedef_name,

    6. type-qualifier::="const" | "volatile",

    7. typedef_name::=identifier,

    Initialization

    8. initializer::=assignment_expression | initializer_list,

    9. initializer_list::=List(initializer),

    10. declarator_initialized::=declarator ("=" initializer),

    Structs and Unions11. structure_declarator::=declarator | declarator ":" constant_expression,

    12. structure_declarator_list::=List(structure_declarator),

    13. structure_declaration::=(type_specifier | type_qualifier)

    structure_declarator_list ";" ,

    14. struct_union_specifier::=struct_union identifier | struct_union identifier

    "{"structure_declarator_list "}",

    15. struct_union::=( "struct" | "union" ),

    Enums

    16. enumeration_value::=enumeration_constant ("=" constant_expression|)

    17. enumeration_list::=List(enumeration_value ),

    18. enumeration_specifier::=enumeration_identifier | "enum" identifier

    "{"enumeration_list"}",

    Functions

    19. function_definition::=declaration_specifier declarator | declaration_list |compound_statement,

    20. parameter_declaration::=#declaration_specifier declarator |

    abstract_declarator,

    21. parameter_list::=List(parameter_declaration) (",..."|),

    Main Function

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    6/24

    A complete C program has to have a function with name 'main'. This is the

    function called by the operating system. It must return an int value indicating whether the

    prograam executed correctly or if there was an error. In UNIX, the main program returns0 to indicate no errors. Their are several valid forms:

    22. int main()

    23. int main(argc, argv)24. int main(argc, argv, envp) The parameters are set up by the operating system

    when the program starts. The traditional arg stands for argument.

    Pointers

    25. pointer::=#( "*" | #type_qualifier),

    26. declarator::=pointer | direct_declarator,

    Functions and Arrays

    27. post_declarator::="["constant_expression"]" | "("parameter_list")" |

    "("identifier_list")"

    28. direct_declarator::=identifier | "("declarator")" | direct_declaratorpost_declarator,

    29. abstract_declarator::=pointer | pointer direct_abstract_declarator,

    30. direct_abstract_declarator::= "(" abstract_declarator ")" |

    O( direct_abstract_declarator) O("[" O(constant_expression) "]" | "(" O(parameter_list)

    ")" ),

    . . . . . . . . . ( end of section Declarations)

    Statements1. statement::=labeled_statement | compound_statement | expression_statement |

    selection_statement | iteration_statement | jump_statement

    Branch2. jump_statement::="goto" identifier";" | "continue" ";" | "break;" | "return"

    expression ";",

    Structured

    3. loop::=iteration_statement.4. iteration_statement::="while" "("expression")" statement | "do" statement

    "while" "("expression")" ";" | for_statement.

    5. for_statement::="for" "("expression ";" expression ";" expression")"

    statement,

    6. selection_statement::=if_statement | "switch" "("expression")" statement,

    7. if_statement::="if ("expression")" statement | "if" "("expression")" statement

    "else" statement.

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    7/24

    8. expression_statement::= expression ";",

    9. labeled_statement::=identifier ":" statement | "case" constant_expression ":"statement | "default" ":" statement,

    Compound

    10. compound_statement::=block | "{" #statement "}",

    11. block::="{" declaration #declaration #statement "}",

    . . . . . . . . . ( end of section Statements)

    Pre-Processor Commands

    1. preprocess_token::=identifier | constant | string_literal | operator | punctuator |each Non-white space not one of the previous,

    2. header_char::=any character except new_line | and | >,

    3. header_name::=#(header_char),

    4. new_line::=new_line character,

    5. Left_paren::=left parenthesis with no white space before it,

    6. control_line::="#include" (#(preprocess_token | header_name) new_line |

    "#define" identifier #(preprocess_token) new_line | "#define" identifier left_paren

    identifier_list #(preprocess_token) new_line, | "#undef" identifier new_line | "#line"preprocess_token new_line | "#error" preprocess_token new_line | "#pragma"

    preprocess_token new_line | "#"new_line,

    7. endif_line::="#endif" new_line,

    8. elif_group::="#elif" constant_expression new_line pp_group,

    9. else_group::="#else" new_line pp_group,

    10. if_group::=("#if" constant_expression | "#ifdef" identifier | "#ifndef"identifier) new_line pp_group,

    11. if_part::=if_group #(elif_group) else_group endif_line,

    12. pp_part::=#preprocess_token new_line | if_part | control_line,

    13. pp_group::=#(pp_part),

    . . . . . . . . . ( end of section Pre-Processor Commands)

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    8/24

    . . . . . . . . . ( end of section Syntax of The C Programming Language)

    End

    21. A complete C program has to have a function with name 'main'. This is the

    function called by the operating system. It must return an int value indicating whether theprograam executed correctly or if there was an error. In UNIX, the main program returns

    0 to indicate no errors. Their are several valid forms:

    22. int main()23. int main(argc, argv)

    24. int main(argc, argv, envp) The parameters are set up by the operating system

    when the program starts. The traditional arg stands for argument.

    Pointers

    25. pointer::=#( "*" | #type_qualifier),

    26. declarator::=pointer | direct_declarator,

    Functions and Arrays27. post_declarator::="["constant_expression"]" | "("parameter_list")" |

    "("identifier_list")"

    28. direct_declarator::=identifier | "("declarator")" | direct_declaratorpost_declarator,

    29. abstract_declarator::=pointer | pointer direct_abstract_declarator,

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    9/24

    30. direct_abstract_declarator::= "(" abstract_declarator ")" |O( direct_abstract_declarator) O("[" O(constant_expression) "]" | "(" O(parameter_list)

    ")" ),

    . . . . . . . . . ( end of section Declarations) Statements

    1. statement::=labeled_statement | compound_statement | expression_statement |

    selection_statement | iteration_statement | jump_statementBranch

    2. jump_statement::="goto" identifier";" | "continue" ";" | "break;" | "return"

    expression ";",Structured

    3. loop::=iteration_statement.

    4. iteration_statement::="while" "("expression")" statement | "do" statement"while" "("expression")" ";" | for_statement.

    5. for_statement::="for" "("expression ";" expression ";" expression")"statement,

    6. selection_statement::=if_statement | "switch" "("expression")" statement,

    7. if_statement::="if ("expression")" statement | "if" "("expression")" statement"else" statement.

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    10/24

    8. expression_statement::= expression ";",

    9. labeled_statement::=identifier ":" statement | "case" constant_expression ":"statement | "default" ":" statement,

    Compound

    10. compound_statement::=block | "{" #statement "}",

    11. block::="{" declaration #declaration #statement "}",

    . . . . . . . . . ( end of section Statements)

    Pre-Processor Commands

    1. preprocess_token::=identifier | constant | string_literal | operator | punctuator |

    each Non-white space not one of the previous,

    2. header_char::=any character except new_line | and | >,

    3. header_name::=#(header_char),

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    11/24

    4. new_line::=new_line character,

    5. Left_paren::=left parenthesis with no white space before it,

    6. control_line::="#include" (#(preprocess_token | header_name) new_line |"#define" identifier #(preprocess_token) new_line | "#define" identifier left_paren

    identifier_list #(preprocess_token) new_line, | "#undef" identifier new_line | "#line"

    preprocess_token new_line | "#error" preprocess_token new_line | "#pragma"preprocess_token new_line | "#"new_line,

    7. endif_line::="#endif" new_line,

    8. elif_group::="#elif" constant_expression new_line pp_group,

    9. else_group::="#else" new_line pp_group,

    10. if_group::=("#if" constant_expression | "#ifdef" identifier | "#ifndef"

    identifier) new_line pp_group,

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    12/24

    11. if_part::=if_group #(elif_group) else_group endif_line,

    12. pp_part::=#preprocess_token new_line | if_part | control_line,

    13. pp_group::=#(pp_part),

    . . . . . . . . . ( end of section Pre-Processor Commands)

    . . . . . . . . . ( end of section Syntax of The C Programming Language)

    End 1.

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    13/24

    3.

    Arithmetic

    4. post_fix::="++" | "--",

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    14/24

    5. post_fix_expression::=(primary_expression) #(post_fix),

    6. unary_operator::="&" | "*" | "+" | "-" | "!" | "-",

    7. pre_fix::="++" | "--" | "sizeof",

    8. unary_expression::=#(pre-fix) post_fix_expression | unary_operator

    cast_expression | "sizeof" "(" type_name")",

    9. cast_expression::=#(type_name) unary_expression. This implies that casts are

    done after doing post-fix operations..

    10. multiplicative_expression::=S(cast_expression, multiplicative_operator).[ serial_operator_expression ]

    The rule above means that 'casts' are done before multiplication and division,

    and that multiplication and division are done from left to right.

    11. multiplicative_operator::="*" | "%" | "/",

    12. additive_expression::=S(multiplicative_expression, additive_operator). This

    means that addition and subtraction occurs after multiplication and from left to right.

    13. additive_operator::="+" | "-",

    Shifts14. shift_expression::=S(additive_expression, shift_operator),

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    15/24

    15. shift_operator::=">>" | "

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    16/24

    Decimal Binary (last 4 bits/ 1 byte)

    0 00001 0001

    2 0010

    3 00114 0100

    5 0101

    6 01107 0111

    8 1000

    20. AND_expression::=S(equality_expression, and_operator),

    21. and_operator::="&", This operator takes each bit in the value of its

    arguments in turn to calculate the bit in the answer. A bit is 1 if and only if both

    arguments have bits in that place that are 1.Decimal Binary

    3 0011

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    17/24

    5 0101

    3&5 0001

    22. XOR_expression::=S(AND_expression, XOR_operator),

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    18/24

    23. XOR_operator::="^", XOR is short for eXclusive-OR. The n'th bit in the

    value is 1 precisly when the n'th bits in the two arguments are different.

    Decimal Binary3 0011

    5 0101

    3^50110

    24. OR_expression::=S(XOR_expression, OR_operator),

    25. OR_operator::="|", This operator takes each bit in the value of its arguments

    in turn to calculate the bit in the answer. The n'th bit is 1 if either n'th bits is 1.

    Decimal Binary

    3 00115 0101

    3|5 0111

    Logical ExpressionsIn C, logical false is reresented by any zero value and true by any nonzero

    value. Here is a list of operators1. and::="&&".

    2. or::="||",

    3. not::="!",

    26. logical_AND_expression::=S(OR_expression, logical_AND_operator),

    27. logical_AND_operator::=and, A&&B is true precisely when both A and Bevaluate to be true. If A evaluates to false, B is not evaluated.

    28. logical_OR_expression::=S(logical_AND_expression,logical_OR_operator),

    29. logical_OR_operator::=or, A||B is true if A evaluates to be true, or when A isfalse and B evaluates to be true. If both evaluate to false (zero) then A||B is false.

    Conditional Expressions

    30. conditional_expression::=logical_OR_expression | logical_OR_expression"?" expression ":" conditional_expression,

    Assignment Statements31. assignment_expression::=S(unary_expression, assignment_operator),

    32. assignment_operator::="=" | "*=" | "/=" | "%=" | "+=" | "=" | "&="| "^=" | "|=",

    33. expression::=List(assignment_expression ),

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    19/24

    34. constant_expression::=conditional_expression,

    . . . . . . . . . ( end of section Expressions)

    Declarations

    1. declaration::=declaration_specifier | declarator_list,

    2. declarator_list::=List(declarator_initialized),

    3. declaration_specifier::=(storage_class | type_specifier | type_qualifier),

    4. storage_class::="typedef" | "extern" | "static" | "auto" | "register",

    Types

    5. type_specifier::="void" | "char" | "short" | "int" | "long" | "float" | "double" |

    "signed" | "unsigned" | struct_union_specifier | enumeration_specifier | typedef_name,

    6. type-qualifier::="const" | "volatile",

    7. typedef_name::=identifier,

    Initialization

    8. initializer::=assignment_expression | initializer_list,

    9. initializer_list::=List(initializer),

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    20/24

    10. declarator_initialized::=declarator ("=" initializer),

    Structs and Unions

    11. structure_declarator::=declarator | declarator ":" constant_expression,

    12. structure_declarator_list::=List(structure_declarator),

    13. structure_declaration::=(type_specifier | type_qualifier)

    structure_declarator_list ";" ,

    14. struct_union_specifier::=struct_union identifier | struct_union identifier

    "{"structure_declarator_list "}",

    15. struct_union::=( "struct" | "union" ),

    Enums

    16. enumeration_value::=enumeration_constant ("=" constant_expression|)

    17. enumeration_list::=List(enumeration_value ),

    18. enumeration_specifier::=enumeration_identifier | "enum" identifier

    "{"enumeration_list"}",

    Functions19. function_definition::=declaration_specifier declarator | declaration_list |

    compound_statement,

    20. parameter_declaration::=#declaration_specifier declarator |abstract_declarator,

    21. parameter_list::=List(parameter_declaration) (",..."|),

    Main Function

    A complete C program has to have a function with name 'main'. This is thefunction called by the operating system. It must return an int value indicating whether the

    prograam executed correctly or if there was an error. In UNIX, the main program returns

    0 to indicate no errors. Their are several valid forms:

    22. int main()23. int main(argc, argv)

    24. int main(argc, argv, envp) The parameters are set up by the operating system

    when the program starts. The traditional arg stands for argument.

    Pointers

    25. pointer::=#( "*" | #type_qualifier),

    26. declarator::=pointer | direct_declarator,

    Functions and Arrays

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    21/24

    27. post_declarator::="["constant_expression"]" | "("parameter_list")" |

    "("identifier_list")"

    28. direct_declarator::=identifier | "("declarator")" | direct_declarator

    post_declarator,

    29. abstract_declarator::=pointer | pointer direct_abstract_declarator,

    30. direct_abstract_declarator::= "(" abstract_declarator ")" |O( direct_abstract_declarator) O("[" O(constant_expression) "]" | "(" O(parameter_list)

    ")" ),

    . . . . . . . . . ( end of section Declarations) Statements

    1. statement::=labeled_statement | compound_statement | expression_statement |

    selection_statement | iteration_statement | jump_statement

    Branch2. jump_statement::="goto" identifier";" | "continue" ";" | "break;" | "return"

    expression ";",Structured

    3. loop::=iteration_statement.

    4. iteration_statement::="while" "("expression")" statement | "do" statement

    "while" "("expression")" ";" | for_statement.

    5. for_statement::="for" "("expression ";" expression ";" expression")"

    statement,

    6. selection_statement::=if_statement | "switch" "("expression")" statement,

    7. if_statement::="if ("expression")" statement | "if" "("expression")" statement"else" statement.

    8. expression_statement::= expression ";",

    9. labeled_statement::=identifier ":" statement | "case" constant_expression ":"

    statement | "default" ":" statement,

    Compound10. compound_statement::=block | "{" #statement "}",

    11. block::="{" declaration #declaration #statement "}",

    . . . . . . . . . ( end of section Statements)

    Pre-Processor Commands

    1. preprocess_token::=identifier | constant | string_literal | operator | punctuator |

    each Non-white space not one of the previous,

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    22/24

    2. header_char::=any character except new_line | and | >,

    3. header_name::=#(header_char),

    4. new_line::=new_line character,

    5. Left_paren::=left parenthesis with no white space before it,

    6. control_line::="#include" (#(preprocess_token | header_name) new_line |"#define" identifier #(preprocess_token) new_line | "#define" identifier left_paren

    identifier_list #(preprocess_token) new_line, | "#undef" identifier new_line | "#line"

    preprocess_token new_line | "#error" preprocess_token new_line | "#pragma"

    preprocess_token new_line | "#"new_line,

    7. endif_line::="#endif" new_line,

    8. elif_group::="#elif" constant_expression new_line pp_group,

    9. else_group::="#else" new_line pp_group,

    10. if_group::=("#if" constant_expression | "#ifdef" identifier | "#ifndef"

    identifier) new_line pp_group,

    11. if_part::=if_group #(elif_group) else_group endif_line,

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    23/24

    12. pp_part::=#preprocess_token new_line | if_part | control_

    line,

    13. pp_group::=#(pp_part),

  • 8/9/2019 Expressions Are Made Up by Applying Operators to Expressions.

    24/24

    . . . . . . . . . ( end of section Pre-Processor Commands)

    . . . . . . . . . ( end of section Syntax of The C Programming Language)

    End