Skip to content

Instantly share code, notes, and snippets.

@fujidig
Last active September 4, 2016 11:29
Show Gist options
  • Select an option

  • Save fujidig/38fbbe2fdcbf200cc98b04c4cb98a8b8 to your computer and use it in GitHub Desktop.

Select an option

Save fujidig/38fbbe2fdcbf200cc98b04c4cb98a8b8 to your computer and use it in GitHub Desktop.
/* https://www.lysator.liu.se/c/ANSI-C-grammar-y.html */
%token IDENTIFIER CONSTANT
%token LE_OP GE_OP EQ_OP NE_OP
%token INT
%token IF ELSE WHILE GOTO RETURN
%start translation_unit
%type <unit> translation_unit
%%
primary_expression
: IDENTIFIER { }
| CONSTANT { }
| '(' expression ')' { }
;
postfix_expression
: primary_expression { }
| postfix_expression '(' ')' { }
| postfix_expression '(' argument_expression_list ')' { }
;
argument_expression_list
: assignment_expression { }
| argument_expression_list ',' assignment_expression { }
;
unary_expression
: postfix_expression { }
| '+' unary_expression { }
| '-' unary_expression { }
;
multiplicative_expression
: unary_expression { }
| multiplicative_expression '*' unary_expression { }
| multiplicative_expression '/' unary_expression { }
| multiplicative_expression '%' unary_expression { }
;
additive_expression
: multiplicative_expression { }
| additive_expression '+' multiplicative_expression { }
| additive_expression '-' multiplicative_expression { }
;
relational_expression
: additive_expression { }
| relational_expression '<' additive_expression { }
| relational_expression '>' additive_expression { }
| relational_expression LE_OP additive_expression { }
| relational_expression GE_OP additive_expression { }
;
equality_expression
: relational_expression { }
| equality_expression EQ_OP relational_expression { }
| equality_expression NE_OP relational_expression { }
;
assignment_expression
: equality_expression { }
| unary_expression '=' assignment_expression { }
;
expression
: assignment_expression { }
| expression ',' assignment_expression { }
;
declaration
: type_specifier ';' { }
| type_specifier declarator_list ';' { }
;
declarator_list
: declarator { }
| declarator_list ',' declarator { }
;
type_specifier
: INT { }
;
declarator
: IDENTIFIER { }
;
function_declaration
: type_specifier declarator '(' ')' ';' { }
| type_specifier declarator '(' parameter_type_list ')' ';' { }
| type_specifier declarator '(' identifier_list ')' ';' { }
;
parameter_type_list
: parameter_list { }
;
parameter_list
: parameter_declaration { }
| parameter_list ',' parameter_declaration { }
;
parameter_declaration
: type_specifier declarator { }
| type_specifier { }
;
identifier_list
: IDENTIFIER { }
| identifier_list ',' IDENTIFIER { }
;
statement
: labeled_statement { }
| compound_statement { }
| expression_statement { }
| selection_statement { }
| iteration_statement { }
| jump_statement { }
;
labeled_statement
: IDENTIFIER ':' statement { }
;
compound_statement
: '{' '}' { }
| '{' statement_list '}' { }
| '{' declaration_list '}' { }
| '{' declaration_list statement_list '}' { }
;
declaration_list
: declaration { }
| declaration_list declaration { }
;
statement_list
: statement { }
| statement_list statement { }
;
expression_statement
: ';' { }
| expression ';' { }
;
selection_statement
: IF '(' expression ')' statement { }
| IF '(' expression ')' statement ELSE statement { }
;
iteration_statement
: WHILE '(' expression ')' statement { }
;
jump_statement
: GOTO IDENTIFIER ';' { }
| RETURN ';' { }
| RETURN expression ';' { }
;
translation_unit
: external_declaration { }
| translation_unit external_declaration { }
;
external_declaration
: function_definition { }
| function_declaration { }
;
function_definition
: type_specifier declarator declaration_list compound_statement { }
| type_specifier declarator compound_statement { }
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment