联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp2

您当前位置:首页 >> CS作业CS作业

日期:2018-05-22 04:49

OurC grammar

// 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  VOID    // void

%token  IF      // if

%token  ELSE    // else

%token  WHILE   // while

%token  DO      // do

%token  RETURN  // return

%token  '('

%token  ')'

%token  '['

%token  ']'

%token  '{'

%token  '}'

%token  '+'

%token  '-'

%token  '*'

%token  '/'

%token  '%'

%token  '^'

%token  '>'

%token  '<'

%token  GE      // >=

%token  LE      // <=

%token  EQ      // ==

%token  NEQ     // !=

%token  '&'

%token  '|'

%token  '='

%token  '!'

%token  AND     // &&

%token  OR      // ||

%token  PE      // +=

%token  ME      // -=

%token  TE      // *=

%token  DE      // /=

%token  RE      // %=

%token  PP      // ++

%token  MM      // --

%token  RS      // >>

%token  LS      // <<

%token  ';'

%token  ','

%token  '?'

%token  ':'


/*

* (僅供參考) precedence (lower ones are given higher precedence) and associativity

*/


%left   ','

%right  '=' PE ME TE DE RE

%right  '?'+':'

%left   OR

%left   AND

%left   '|'

%left   '^'

%left   '&'

%left   EQ NEQ

%left   '<' '>' GE LE

%left   '+' '-'

%left   '*' '/' '%'

%right  PP MM sign       // sign is '+' or '-' or '!'

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


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 }


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





版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp