OurC grammar 2016-05-05 (: 2016-07-15)
:
「 OurC grammar 2010-04-04」,,、。
“ ”。 2010-04-04、、。
2016-05-05 OurC grammar :
* expression basic_expression, ','。
* basic_expression unary_expression , operator, operator
。
* " operator", conditional operator, '?' ':'。
* unary_expression :
(a) sign('+', '-', '!') ;
(b) sign ;
(c) PP/MM( '++' '--')。
* ID ID'[' expression ']' (a)(b)(c)。 case (a) (b),
※ ID'(' actual_parameter_list ')' : function
※ '(' expression ')'
※ CONSTANT
* ID ID'[' expression ']' (a)(b)(c), PP/MM 。
* 「 sign」「 PP/MM」:
※ sign ID ID'[' expression ']'、 PP/MM ID
ID'['expression ']'。。
※ PP/MM ID ID'[' expression ']'。
※ sign 。
: romce_and_romloe
rest_of_maybe_conditional_exp_and_rest_of_maybe_logical_OR_exp
,, romce_and_romloe。
/*
* OurC - the grammar (May 5th, 2016)
*
* : 'a++b' error, '++' 'PP'
* 'a+ +b' error。
*
*/
// the lexical part
%token Identifier
%token Constant // e.g., 35, 35.67, 'a', "Hi, there", true, false
// .35, 35., 0023
%token INT // int
%token FLOAT // float
%token CHAR // char
%token BOOL // bool
%token STRING // string '
%token '=
%token LE // >
%token LS // ' GE LE
%left '+' '-'
%left '*' '/' '%'
%right PP MM sign // sign is '+' or '-' or '!'
3
%% // the syntactical part (in EBNF)
user_input
: ( definition | statement ) { definition | statement }
definition
: VOID Identifier function_definition_without_ID
| type_specifier Identifier function_definition_or_declarators
type_specifier
: INT | CHAR | FLOAT | STRING | BOOL
function_definition_or_declarators
: function_definition_without_ID
| rest_of_declarators
rest_of_declarators
: [ '[' Constant ']' ]
{ ',' Identifier [ '[' Constant ']' ] } ';'
function_definition_without_ID
: '(' [ VOID | formal_parameter_list ] ')' compound_statement
formal_parameter_list
: type_specifier [ '' ] Identifier [ '[' Constant ']' ]
{ ',' type_specifier [ '' ] Identifier [ '[' Constant ']' ] }
compound_statement
: '{' { declaration | statement } '}'
declaration
: type_specifier Identifier rest_of_declarators
statement
: ';' // the null statement
| expression ';' /* expression here should not be empty */
| RETURN [ expression ] ';'
| compound_statement
| IF '(' expression ')' statement [ ELSE statement ]
| WHILE '(' expression ')' statement
| DO statement WHILE '(' expression ')' ';'
expression
: basic_expression { ',' basic_expression }
basic_expression
: Identifier rest_of_Identifier_started_basic_exp
| ( PP | MM ) Identifier rest_of_PPMM_Identifier_started_basic_exp
| sign { sign } signed_unary_exp romce_and_romloe
| ( Constant | '(' expression ')' ) romce_and_romloe
rest_of_Identifier_started_basic_exp
: [ '[' expression ']' ]
( assignment_operator basic_expression
|
[ PP | MM ] romce_and_romloe
)
| '(' [ actual_parameter_list ] ')' romce_and_romloe
4
rest_of_PPMM_Identifier_started_basic_exp
: [ '[' expression ']' ] romce_and_romloe
sign
: '+' | '-' | '!'
actual_parameter_list
: basic_expression { ',' basic_expression }
assignment_operator
: '=' | TE | DE | RE | PE | ME
rest_of_maybe_conditional_exp_and_rest_of_maybe_logical_OR_exp // romce_and_romloe
: rest_of_maybe_logical_OR_exp [ '?' basic_expression ':' basic_expression ]
rest_of_maybe_logical_OR_exp
: rest_of_maybe_logical_AND_exp { OR maybe_logical_AND_exp }
maybe_logical_AND_exp
: maybe_bit_OR_exp { AND maybe_bit_OR_exp }
rest_of_maybe_logical_AND_exp
: rest_of_maybe_bit_OR_exp { AND maybe_bit_OR_exp }
maybe_bit_OR_exp
: maybe_bit_ex_OR_exp { '|' maybe_bit_ex_OR_exp }
rest_of_maybe_bit_OR_exp
: rest_of_maybe_bit_ex_OR_exp { '|' maybe_bit_ex_OR_exp }
maybe_bit_ex_OR_exp
: maybe_bit_AND_exp { '^' maybe_bit_AND_exp }
rest_of_maybe_bit_ex_OR_exp
: rest_of_maybe_bit_AND_exp { '^' maybe_bit_AND_exp }
maybe_bit_AND_exp
: maybe_equality_exp { '' maybe_equality_exp }
rest_of_maybe_bit_AND_exp
: rest_of_maybe_equality_exp { '' maybe_equality_exp }
maybe_equality_exp
: maybe_relational_exp
{ ( EQ | NEQ ) maybe_relational_exp}
rest_of_maybe_equality_exp
: rest_of_maybe_relational_exp
{ ( EQ | NEQ ) maybe_relational_exp }
maybe_relational_exp
: maybe_shift_exp
{ ( '' | LE | GE ) maybe_shift_exp }
rest_of_maybe_relational_exp
: rest_of_maybe_shift_exp
{ ( '' | LE | GE ) maybe_shift_exp }
maybe_shift_exp
: maybe_additive_exp { ( LS | RS ) maybe_additive_exp }
rest_of_maybe_shift_exp
: rest_of_maybe_additive_exp { ( LS | RS ) maybe_additive_exp }
5
maybe_additive_exp
: maybe_mult_exp { ( '+' | '-' ) maybe_mult_exp }
rest_of_maybe_additive_exp
: rest_of_maybe_mult_exp { ( '+' | '-' ) maybe_mult_exp }
maybe_mult_exp
: unary_exp rest_of_maybe_mult_exp
rest_of_maybe_mult_exp
: { ( '*' | '/' | '%' ) unary_exp } /* could be empty ! */
unary_exp
: sign { sign } signed_unary_exp
| unsigned_unary_exp
| ( PP | MM ) Identifier [ '[' expression ']' ]
signed_unary_exp
: Identifier [ '(' [ actual_parameter_list ] ')'
|
'[' expression ']'
]
| Constant
| '(' expression ')'
unsigned_unary_exp
: Identifier [ '(' [ actual_parameter_list ] ')'
|
[ '[' expression ']' ] [ ( PP | MM ) ]
]
| Constant
| '(' expression ')'