letter ::= a | b | ... | z | A | B | ... | Z
digit ::= 0 | 1 | ... | 9
id ::= letter { letter | digit | _ } intcon ::= digit { digit } charcon ::= '
ch'
|'
\n'
|'
\0'
, where ch denotes any printable ASCII character, as specified by isprint(), other than \ (backslash) and ' (single quote).stringcon ::= "{ch}", where ch denotes any printable ASCII character (as specified by isprint()) other than " (double quotes) and the newline character. Comments Comments are as in C, i.e. a sequence of characters preceded by /* and followed by */, and not containing any occurrence of */.
prog : { dcl | func } dcl : type var_decl { ',' var_decl } ';' | [ extern ] type id '(' parm_types ')' { ',' id '(' parm_types ')' } ';' | [ extern ] void id '(' parm_types ')' { ',' id '(' parm_types ')' } ';' var_decl : id [ '[' intcon ']' ] type : char | int parm_types : void | type id [ '[' ']' ] { ',' type id [ '[' ']' ] } func : type id '(' parm_types ')' '{' { type var_decl { ',' var_decl } ';' } { stmt } '}' | void id '(' parm_types ')' '{' { type var_decl { ',' var_decl } ';' } { stmt } '}' stmt : if '(' expr ')' stmt [ else stmt ] | while '(' expr ')' stmt | for '(' [ assg ] ';' [ expr ] ';' [ assg ] ')' stmt | return [ expr ] ';' | assg ';' | id '(' [expr { ',' expr } ] ')' ';' | '{' { stmt } '}' | ';' assg : id [ '[' expr ']' ] = expr expr : '–' expr | '!' expr | expr binop expr | expr relop expr | expr logical_op expr | id [ '(' [expr { ',' expr } ] ')' | '[' expr ']' ] | '(' expr ')' | intcon | charcon | stringcon binop : + | – | * | / relop : == | != | <= | < | >= | > logical_op : && | ||
Operator Associativity !, – (unary) right to left *, / left to right +, – (binary) left to right <, <=, >, >= left to right ==, != left to right && left to right || left to right
A string constant "a_1 ... a_n"
is an array of characters containing n+1 elements, whose first n
elements are the corresponding characters in the string, and whose last
element is the NUL
character \0.
An array of size n consists of n elements, each occupying an amount of storage equal to that required for the type of the array element, laid out contiguously in memory.
If an object of type char is part of an expression, its value is converted (sign extended) to a value of type int before the expression is evaluated.
The order in which the left and right hand sides of an assignment are evaluated is left unspecified.
A value of type char is converted (sign extended) to a 32-bit quantity before it is assigned to an object of type int.
A value of type int is converted (truncated) to an 8-bit quantity, by discarding the top 24 bits, before it is assigned to an object of type char.
The order in which the actual parameters in a function call are evaluated is unspecified.
Scalar values are passed by value, while arrays (and string constants, which are represented as arrays of characters) are passed by reference. An object of type char is converted (sign extended) to a 32-bit quantity before it is passed as an actual parameter to a function. Since a function that has a formal parameter of type char will, in any case, be passed a 32-bit quantity as an actual, it must convert (truncate) the actual to an 8-bit quantity before using it.
Execution returns from a function if either an explicit return statement is executed, or if execution "falls off" the end of the function body. In the latter case, no value is returned.
Execution begins at a procedure named main().