2014-06-08 08:34:55 +02:00
|
|
|
# C-to-English Grammar
|
|
|
|
#
|
2014-06-07 15:00:07 +02:00
|
|
|
# Warning: work-in-progress. Many things are incomplete or non-functional.
|
|
|
|
#
|
|
|
|
# todo:
|
|
|
|
# 1. the entire syntax for pointers to functions.
|
|
|
|
# 2. preprocessor directives. (getting there)
|
|
|
|
# 4. functions to handle the nesting levels (ordinal number generator and CPP stack)
|
|
|
|
# 6. change returns to prints where appropriate.
|
|
|
|
|
|
|
|
{
|
2014-06-08 15:51:29 +02:00
|
|
|
my @defined_types = ('`FILE`');
|
2014-06-16 04:31:13 +02:00
|
|
|
my ($rule_name, @macros);
|
2014-06-07 15:00:07 +02:00
|
|
|
}
|
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
startrule:
|
|
|
|
translation_unit
|
2014-06-07 15:00:07 +02:00
|
|
|
{
|
|
|
|
my $output = $item[-1];
|
|
|
|
$output =~ s/\^L(\s*.)/\L$1/g; # lowercase specified characters
|
|
|
|
$output =~ s/\^U(\s*.)/\U$1/g; # uppercase specified characters
|
|
|
|
print $output;
|
|
|
|
}
|
2014-06-08 08:34:55 +02:00
|
|
|
startrule(?)
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-10 08:05:14 +02:00
|
|
|
translation_unit:
|
2014-06-10 23:12:09 +02:00
|
|
|
comment
|
2014-06-08 08:34:55 +02:00
|
|
|
| external_declaration
|
|
|
|
| function_definition
|
|
|
|
| function_prototype
|
2014-06-10 23:12:09 +02:00
|
|
|
| preproc[matchrule => 'translation_unit']
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
preproc:
|
2014-06-15 03:09:06 +02:00
|
|
|
'#' (definition
|
|
|
|
| undefinition
|
|
|
|
| inclusion
|
|
|
|
| line
|
|
|
|
| error
|
|
|
|
| pragma
|
|
|
|
| preproc_conditional[matchrule => $arg{matchrule}])
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
definition:
|
2014-06-10 08:05:14 +02:00
|
|
|
macro_definition
|
2014-06-15 03:09:06 +02:00
|
|
|
| 'define' identifier token_sequence(?) <skip: '[ \t]*'> "\n"
|
2014-06-08 08:34:55 +02:00
|
|
|
{
|
|
|
|
my $token_sequence = join('',@{$item{'token_sequence(?)'}});
|
2014-06-10 08:05:14 +02:00
|
|
|
$return = "Define the macro $item{identifier}";
|
2014-06-08 08:34:55 +02:00
|
|
|
$return .= " to mean $token_sequence" if $token_sequence;
|
|
|
|
$return .= ".\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_definition:
|
2014-06-15 03:09:06 +02:00
|
|
|
'define' identifier '(' <leftop: identifier ',' identifier> ')' token_sequence <skip: '[ \t]*'> "\n"
|
2014-06-08 08:34:55 +02:00
|
|
|
{
|
2014-06-10 08:05:14 +02:00
|
|
|
my @symbols = @{$item[-5]};
|
2014-06-08 08:34:55 +02:00
|
|
|
my $last;
|
2014-06-10 08:05:14 +02:00
|
|
|
push @macros, $item{identifier};
|
|
|
|
$return = "Define the macro $item{identifier} ";
|
2014-06-08 08:34:55 +02:00
|
|
|
if ($#symbols > 0) {
|
|
|
|
$last = pop @symbols;
|
2014-06-09 12:00:13 +02:00
|
|
|
$return .= "with the symbols " . join(", ",@symbols) . " and $last ";
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
2014-06-09 12:00:13 +02:00
|
|
|
$return .= "with the symbol $symbols[0] ";
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
2014-06-10 08:05:14 +02:00
|
|
|
$return .= "to use the token sequence `$item{token_sequence}`.\n";
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
undefinition:
|
2014-06-15 03:09:06 +02:00
|
|
|
'undef' identifier <skip: '[ \t]*'> "\n"
|
2014-06-10 08:05:14 +02:00
|
|
|
{
|
|
|
|
@macros = grep { $_ ne $item{identifier} } @macros;
|
|
|
|
$return = "\nAnnul the definition of $item{identifier}.\n";
|
|
|
|
}
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
inclusion:
|
2014-06-15 03:09:06 +02:00
|
|
|
'include' '<' filename '>' <skip: '[ \t]*'> "\n"
|
2014-06-08 08:34:55 +02:00
|
|
|
{ $return = "\nInclude system file $item{filename}.\n"; }
|
2014-06-15 03:09:06 +02:00
|
|
|
| 'include' '"' filename '"' <skip: '[ \t]*'> "\n"
|
2014-06-08 08:34:55 +02:00
|
|
|
{ $return = "\nInclude user file $item{filename}.\n"; }
|
2014-06-15 03:09:06 +02:00
|
|
|
| 'include' token
|
2014-06-08 08:34:55 +02:00
|
|
|
{ $return = "\nImport code noted by the token $item{token}.\n"; }
|
|
|
|
|
|
|
|
filename:
|
|
|
|
/[_\.\-\w\/]+/
|
|
|
|
|
|
|
|
line:
|
2014-06-15 03:09:06 +02:00
|
|
|
'line' constant ('"' filename '"'
|
2014-06-08 08:34:55 +02:00
|
|
|
{ $return = "and filename $item{filename}"; }
|
2014-06-10 08:05:14 +02:00
|
|
|
)(?) <skip: '[ \t]*'> "\n"
|
|
|
|
{ $return = "\nThis is line number $item{constant} " . join('', @{$item[-3]}) . ".\n"; }
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
error:
|
2014-06-15 03:09:06 +02:00
|
|
|
'error' token_sequence(?) <skip: '[ \t]*'> "\n"
|
2014-06-08 08:34:55 +02:00
|
|
|
{ $return = "Stop compilation with error \"" . join('', @{$item{'token_sequence(?)'}}) . "\".\n"; }
|
|
|
|
|
|
|
|
pragma:
|
2014-06-15 03:09:06 +02:00
|
|
|
'pragma' token_sequence(?) <skip: '[ \t]*'> "\n"
|
2014-06-08 08:34:55 +02:00
|
|
|
{
|
2014-06-10 08:05:14 +02:00
|
|
|
my $pragma = join('',@{$item{'token_sequence(?)'}});
|
2014-06-10 19:01:43 +02:00
|
|
|
if ($pragma) { $pragma = ' "$pragma"'; }
|
2014-06-10 08:05:14 +02:00
|
|
|
$return = "Process a compiler-dependent pragma$pragma.\n";
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
preproc_conditional:
|
2014-06-10 08:05:14 +02:00
|
|
|
if_line[matchrule => $arg{matchrule}]
|
2014-06-08 08:34:55 +02:00
|
|
|
{ $rule_name = $arg{matchrule}; }
|
2014-06-10 08:05:14 +02:00
|
|
|
<matchrule: $rule_name>(s?)
|
|
|
|
{ $return = $item{if_line} . join('',@{$item[-1]}); }
|
|
|
|
(elif_parts[matchrule => $rule_name])(?)
|
|
|
|
(else_parts[matchrule => $rule_name])(?)
|
|
|
|
{ $return .= join('',@{$item[-2]}) . join('',@{$item[-1]}); }
|
2014-06-15 03:09:06 +02:00
|
|
|
'endif'
|
2014-06-10 08:05:14 +02:00
|
|
|
{ $return .= "End preprocessor conditional.\n"; }
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
if_line:
|
2014-06-15 03:09:06 +02:00
|
|
|
'ifdef' identifier <skip: '[ \t]*'> "\n"
|
2014-06-10 19:01:43 +02:00
|
|
|
{ $return .= "If the macro $item{identifier} is defined, then ^L"; }
|
2014-06-15 03:09:06 +02:00
|
|
|
| 'ifndef' identifier <skip: '[ \t]*'> "\n"
|
2014-06-10 19:01:43 +02:00
|
|
|
{ $return .= "If the macro $item{identifier} is not defined, then ^L"; }
|
2014-06-15 03:09:06 +02:00
|
|
|
| 'if' constant_expression <skip: '[ \t]*'> "\n"
|
2014-06-10 19:01:43 +02:00
|
|
|
{ $return .= "If the preprocessor condition^L $item{constant_expression} is true, then ^L"; }
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
elif_parts:
|
2014-06-15 03:09:06 +02:00
|
|
|
('elif' constant_expression
|
2014-06-10 23:12:09 +02:00
|
|
|
{ $return .= "Otherwise, if the preprocessor condition $item{constant_expression} is true, then ^L"; }
|
2014-06-08 08:34:55 +02:00
|
|
|
(<matchrule: $rule_name> )[matchrule => $arg{matchrule}](s?)
|
|
|
|
{ $return .= join('',@{$item[-1]}); }
|
|
|
|
)(s)
|
2014-06-10 23:12:09 +02:00
|
|
|
{ $return = join('', @{$item[-1]}); }
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
else_parts:
|
2014-06-15 03:09:06 +02:00
|
|
|
'else'
|
2014-06-08 08:34:55 +02:00
|
|
|
{ $rule_name = $arg{matchrule}; }
|
|
|
|
(<matchrule: $rule_name>)[matchrule => $arg{matchrule}](s?)
|
2014-06-10 19:01:43 +02:00
|
|
|
{ $return = "Otherwise, ^L" . join('',@{$item[-1]}); }
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
token_sequence:
|
|
|
|
token(s)
|
|
|
|
{ $return = join(' ', @{$item[1]}); }
|
|
|
|
|
|
|
|
token:
|
|
|
|
<skip: '[ \t]*'> /\S+/
|
2014-06-10 19:01:43 +02:00
|
|
|
{ $return = $item[-1]; }
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
external_declaration:
|
|
|
|
declaration
|
|
|
|
|
|
|
|
function_definition:
|
2014-06-15 05:57:27 +02:00
|
|
|
declaration_specifiers(?) declarator[context => 'function_definition'] '(' parameter_type_list(?) ')'
|
|
|
|
'{' declaration_list(?) statement_list(?) '}'
|
2014-06-08 08:34:55 +02:00
|
|
|
{
|
|
|
|
my $declaration_specifiers = join('', @{$item{'declaration_specifiers(?)'}});
|
|
|
|
my $parameter_list = join('', @{$item{'parameter_type_list(?)'}});
|
|
|
|
my $declaration_list = join('',@{$item{'declaration_list(?)'}});
|
|
|
|
my $statement_list = join('',@{$item{'statement_list(?)'}});
|
|
|
|
|
|
|
|
my $return_type = $item{declarator};
|
|
|
|
my $name = $item{declarator};
|
|
|
|
|
2014-06-15 05:57:27 +02:00
|
|
|
$name =~ s/`[^`]+$/`/;
|
|
|
|
$return_type =~ s/`.*`\|?//;
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
if ($return_type =~ /\w/ ) {
|
2014-06-15 05:57:27 +02:00
|
|
|
$return_type .= " $declaration_specifiers";
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
|
|
|
$return_type = $declaration_specifiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
$return = "\nLet $name be a function";
|
|
|
|
|
|
|
|
if ($parameter_list) {
|
|
|
|
$return .= " taking $parameter_list";
|
|
|
|
}
|
|
|
|
|
2014-06-10 08:05:14 +02:00
|
|
|
$return .= " and returning $return_type.\nTo perform the function, ^L";
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
if ($declaration_list) {
|
|
|
|
$return .= $declaration_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($statement_list ) {
|
|
|
|
$return .= $statement_list;
|
2014-06-10 08:05:14 +02:00
|
|
|
} else {
|
|
|
|
$return .= "Do nothing.\n";
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# $return .= "End of function $name.\n";
|
|
|
|
# $return .= $item{compound_statement};
|
|
|
|
1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function_prototype:
|
|
|
|
declaration_specifiers(?) declarator[context => 'function_prototype']
|
|
|
|
'(' parameter_type_list(?) ')' ';'
|
|
|
|
{
|
|
|
|
my $declaration_specifiers = join('', @{$item{'declaration_specifiers(?)'}});
|
|
|
|
my $parameter_list = join('', @{$item{'parameter_type_list(?)'}});
|
|
|
|
|
|
|
|
my $return_type = $item{declarator};
|
2014-06-10 23:12:09 +02:00
|
|
|
my $name = $item{declarator};
|
2014-06-08 08:34:55 +02:00
|
|
|
|
2014-06-10 23:12:09 +02:00
|
|
|
$name =~ s/\|.*$//;
|
|
|
|
$return_type =~ s/`.*`\|?//;
|
2014-06-08 08:34:55 +02:00
|
|
|
|
2014-06-10 23:12:09 +02:00
|
|
|
if($return_type) {
|
|
|
|
$return_type .= ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
$return_type .= $declaration_specifiers;
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
$return = "Let $name be a function prototype";
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
if ($parameter_list) {
|
|
|
|
$return .= " taking $parameter_list";
|
|
|
|
}
|
|
|
|
|
|
|
|
$return .= " and returning $return_type.\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
compound_statement:
|
|
|
|
'{' declaration_list(?) statement_list(?) '}'
|
|
|
|
{
|
|
|
|
my $declaration_list = join('',@{$item{'declaration_list(?)'}});
|
|
|
|
my $statement_list = join('',@{$item{'statement_list(?)'}});
|
|
|
|
|
|
|
|
$return = "Begin new block.\n" if not $arg{context};
|
|
|
|
|
|
|
|
if ($declaration_list) {
|
|
|
|
$return .= $declaration_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($statement_list ) {
|
|
|
|
$return .= $statement_list;
|
|
|
|
} else {
|
|
|
|
$return .= "Do nothing and ^L";
|
|
|
|
}
|
|
|
|
|
|
|
|
$return .= "End block.\n" if not $arg{context};
|
|
|
|
|
|
|
|
if ($arg{context}) {
|
2014-06-14 20:41:55 +02:00
|
|
|
$return .= "End $arg{context}.\n" unless $arg{context} eq 'do loop' or $arg{context} eq 'case';
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
1;
|
|
|
|
}
|
|
|
|
|
|
|
|
statement_list:
|
|
|
|
comment(?) preproc[matchrule => 'statement'](?) statement
|
|
|
|
{
|
|
|
|
my $preproc = join('',@{$item{'preproc(?)'}});
|
|
|
|
my $comment = join('',@{$item{'comment(?)'}});
|
|
|
|
|
|
|
|
$return = $item{statement};
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
if ($comment) { $return = $comment . $return; }
|
|
|
|
if ($preproc) { $return = $preproc . $return; }
|
|
|
|
}
|
|
|
|
statement_list(?)
|
|
|
|
{ $return .= join('',@{$item{'statement_list(?)'}}); }
|
|
|
|
|
|
|
|
statement:
|
|
|
|
jump_statement
|
|
|
|
{ $return = $item{jump_statement}; }
|
|
|
|
| compound_statement[context => $arg{context}, name => $arg{context} ]
|
|
|
|
| iteration_statement
|
|
|
|
| selection_statement
|
|
|
|
| labeled_statement
|
|
|
|
| expression_statement
|
|
|
|
|
|
|
|
iteration_statement:
|
|
|
|
'for' '(' <commit> for_initialization(?) ';' for_expression(?) ';' for_increment(?) ')'
|
2014-06-10 10:50:19 +02:00
|
|
|
statement[context => 'for loop']
|
2014-06-08 08:34:55 +02:00
|
|
|
{
|
|
|
|
my $initialization = join('', @{$item{'for_initialization(?)'}});
|
|
|
|
my $item_expression = join('',@{$item{'for_expression(?)'}});
|
|
|
|
my $increment = join('',@{$item{'for_increment(?)'}});
|
|
|
|
|
|
|
|
if ($initialization) {
|
|
|
|
$return .= "Prepare a loop by ^L$initialization, then ^L";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($item_expression) {
|
|
|
|
$return .= "For as long as $item_expression, ^L";
|
|
|
|
} else {
|
|
|
|
$return .= "Repeatedly ^L";
|
|
|
|
}
|
|
|
|
|
|
|
|
$return .= $item{statement} ;
|
|
|
|
|
|
|
|
if ($increment) {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return =~ s/End for loop.$//;
|
2014-06-09 12:00:13 +02:00
|
|
|
$return .= "After each iteration, ^L$increment.\n";
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
| 'while' '(' <commit> expression ')' statement[context => 'while loop']
|
|
|
|
{
|
|
|
|
if($item{expression} =~ /(^\d+$)/) {
|
|
|
|
if($1 == 0) {
|
2014-06-11 10:29:58 +02:00
|
|
|
$return = "Never ^L";
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
2014-06-11 10:29:58 +02:00
|
|
|
$return = "Repeatedly ^L";
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
2014-06-11 10:29:58 +02:00
|
|
|
} else {
|
|
|
|
$return = "While $item{expression}, ^L";
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
|
2014-06-11 10:29:58 +02:00
|
|
|
if($item{statement}) {
|
|
|
|
$return .= $item{statement} . "\n";
|
|
|
|
} else {
|
|
|
|
$return .= "do nothing.\n";
|
|
|
|
}
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
| 'do' statement[context => 'do loop'] 'while' '(' expression ')' ';'
|
2014-06-15 05:57:27 +02:00
|
|
|
{ $return = "Do the following:^L $item{statement}Do this as long as $item{expression}.\n"; }
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
for_initialization:
|
2014-06-10 08:05:14 +02:00
|
|
|
expression[context => 'for loop']
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
for_expression:
|
|
|
|
expression[context => 'for_expression']
|
|
|
|
|
|
|
|
for_increment:
|
|
|
|
expression[context => 'statement']
|
|
|
|
|
|
|
|
selection_statement:
|
|
|
|
'if' <commit> '(' expression[context => 'if block'] ')' statement[context => 'if block']
|
2014-06-12 14:23:16 +02:00
|
|
|
{
|
|
|
|
if($item{expression} =~ /^(\d+)$/) {
|
|
|
|
if($1 == 0) {
|
|
|
|
$item{expression} = "never";
|
|
|
|
} else {
|
|
|
|
$item{expression} = "always";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$return = "If $item{expression} then ^L$item{statement}";
|
|
|
|
}
|
2014-06-08 08:34:55 +02:00
|
|
|
('else' statement[context => 'else block']
|
|
|
|
{ $return = "Otherwise, ^L$item{statement}"; }
|
|
|
|
)(?)
|
|
|
|
{ $return .= join('',@{$item[-1]}); }
|
|
|
|
| 'switch' '(' expression ')' statement[context => 'switch']
|
2014-06-14 20:41:55 +02:00
|
|
|
{ $return = "Given the expression \'$item{expression}\',\n^L$item{statement}"; }
|
2014-06-07 15:00:07 +02:00
|
|
|
|
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
jump_statement:
|
2014-06-14 20:41:55 +02:00
|
|
|
'break' ';'
|
|
|
|
{
|
|
|
|
if($arg{context} eq 'switch' or $arg{context} eq 'case') {
|
|
|
|
$return = "Break case.\n";
|
|
|
|
} elsif(length $arg{context}) {
|
|
|
|
$return = "Break from the $arg{context}.\n";
|
|
|
|
} else {
|
|
|
|
$return = "Break from the current block.\n";
|
|
|
|
}
|
|
|
|
}
|
2014-06-08 08:34:55 +02:00
|
|
|
| 'continue' ';'
|
2014-06-14 20:41:55 +02:00
|
|
|
{ $return = "Return to the top of the current loop.\n"; }
|
2014-06-08 08:34:55 +02:00
|
|
|
| 'return' <commit> expression[context => 'return'](?) ';'
|
|
|
|
{
|
|
|
|
my $item_expression = join('', @{$item{'expression(?)'}});
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
if (length $item_expression) {
|
|
|
|
$return = "Return ^L$item_expression.\n";
|
|
|
|
} else {
|
|
|
|
$return = "Return no value.\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
| 'goto' <commit> identifier ';' comment(?)
|
|
|
|
{
|
|
|
|
$return = "Go to the label named $item{identifier}.\n";
|
2014-06-14 21:13:53 +02:00
|
|
|
$return .= join('', @{$item{'comment(?)'}});
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
expression_statement:
|
|
|
|
expression[context => 'statement'](?) ';'
|
|
|
|
{
|
|
|
|
my $item_expression = join('',@{$item[1]});
|
|
|
|
if (!$item_expression) {
|
2014-06-14 21:13:53 +02:00
|
|
|
if($arg{context} eq 'label') {
|
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
$return = "Do nothing.\n";
|
|
|
|
}
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
|
|
|
$return = $item_expression.".\n" ;
|
|
|
|
}
|
|
|
|
}
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
labeled_statement:
|
2014-06-14 21:13:53 +02:00
|
|
|
identifier ':' statement[context => 'label'] (';')(?)
|
2014-06-14 20:41:55 +02:00
|
|
|
{ $return = "Let there be a label $item{identifier}.\n$item{statement}"; }
|
2014-06-08 08:34:55 +02:00
|
|
|
| 'case' constant_expression ':' statement[context => 'case']
|
2014-06-14 20:41:55 +02:00
|
|
|
{ $return = "When it has the value $item{constant_expression}, ^L$item{statement}"; }
|
2014-06-08 08:34:55 +02:00
|
|
|
| 'default' ':' statement
|
2014-06-14 20:41:55 +02:00
|
|
|
{ $return = "In the default case, ^L$item{statement}"; }
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
expression:
|
|
|
|
<leftop: assignment_expression[context => $arg{context}] ',' assignment_expression[context => $arg{context}]>
|
|
|
|
{
|
2014-06-09 12:00:13 +02:00
|
|
|
$return = join(", then ^L",@{$item[-1]});
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assignment_expression:
|
|
|
|
unary_expression[context => 'assignment_expression']
|
|
|
|
assignment_operator[context => $arg{context}]
|
|
|
|
assignment_expression[context => 'assignment_expression']
|
|
|
|
{
|
|
|
|
my $assignment_expression = $item{assignment_expression};
|
|
|
|
my $assignment_operator = $item{assignment_operator};
|
|
|
|
|
2014-06-10 08:05:14 +02:00
|
|
|
if ($arg{context} eq 'statement' or $arg{context} eq 'for loop') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return .= "${$item{assignment_operator}}[0] $item{unary_expression} ";
|
|
|
|
$return .= "${$item{assignment_operator}}[1] " if $assignment_expression !~ /the result of/;
|
|
|
|
$return .= $assignment_expression;
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
2014-06-12 15:23:03 +02:00
|
|
|
$return = "$item{unary_expression} $assignment_operator $assignment_expression";
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
| conditional_expression[context => $arg{context}]
|
|
|
|
|
|
|
|
conditional_expression:
|
2014-06-15 02:46:49 +02:00
|
|
|
logical_OR_AND_expression[context => $arg{context}] conditional_ternary_expression
|
2014-06-08 08:34:55 +02:00
|
|
|
{
|
2014-06-15 02:46:49 +02:00
|
|
|
if($item{conditional_ternary_expression}) {
|
|
|
|
$return = "depending on if $item{logical_OR_AND_expression} is true then $item{conditional_ternary_expression}";
|
|
|
|
} else {
|
|
|
|
$return = $item{logical_OR_AND_expression};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
conditional_ternary_expression:
|
|
|
|
'?' expression ':' conditional_expression
|
|
|
|
{ $return = "$item{expression} otherwise $item{conditional_expression}"; }
|
2014-06-16 04:31:13 +02:00
|
|
|
| {""}
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
assignment_operator:
|
|
|
|
'='
|
|
|
|
{
|
|
|
|
if ($arg{context} eq 'statement') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['Assign to', 'the value' ];
|
2014-06-10 08:05:14 +02:00
|
|
|
} elsif ($arg{context} eq 'for loop') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['assigning to', 'the value' ];
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
2014-06-12 15:23:03 +02:00
|
|
|
$return = 'which is assigned to be';
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
| '+='
|
|
|
|
{
|
|
|
|
if ($arg{context} eq 'statement') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['Increment','by'];
|
2014-06-10 08:05:14 +02:00
|
|
|
} elsif ($arg{context} eq 'for loop') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['incrementing','by'];
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
2014-06-12 15:23:03 +02:00
|
|
|
$return = 'which is incremented by';
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
| '-='
|
|
|
|
{
|
|
|
|
if ($arg{context} eq 'statement') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['Decrement' , 'by'];
|
2014-06-10 08:05:14 +02:00
|
|
|
} elsif ($arg{context} eq 'for loop') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['decrementing' , 'by'];
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
2014-06-12 15:23:03 +02:00
|
|
|
$return = 'which is decremented by';
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
| '*='
|
|
|
|
{
|
|
|
|
if ($arg{context} eq 'statement') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['Multiply' , 'by'];
|
2014-06-10 08:05:14 +02:00
|
|
|
} elsif ($arg{context} eq 'for loop') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['multiplying' , 'by'];
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
2014-06-12 15:23:03 +02:00
|
|
|
$return = 'which is multiplied by';
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
| '/='
|
|
|
|
{
|
|
|
|
if ($arg{context} eq 'statement') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['Divide' , 'by' ];
|
2014-06-10 08:05:14 +02:00
|
|
|
} elsif ($arg{context} eq 'for loop') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['dividing' , 'by' ];
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
2014-06-12 15:23:03 +02:00
|
|
|
$return = 'which is divided by';
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
| '%='
|
|
|
|
{
|
|
|
|
if ($arg{context} eq 'statement') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['Reduce', 'to modulo '] ;
|
2014-06-10 08:05:14 +02:00
|
|
|
} elsif ($arg{context} eq 'for loop') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['reducing', 'to modulo '] ;
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
2014-06-12 15:23:03 +02:00
|
|
|
$return = 'which is reduced to modulo';
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
| '<<='
|
|
|
|
{
|
|
|
|
if ($arg{context} eq 'statement') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['Bit-shift', 'left by'];
|
2014-06-10 08:05:14 +02:00
|
|
|
} elsif ($arg{context} eq 'for loop') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['bit-shifting', 'left by'];
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
2014-06-12 15:23:03 +02:00
|
|
|
$return = 'which is bit-shifted left by';
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
| '>>='
|
|
|
|
{
|
|
|
|
if ($arg{context} eq 'statement') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['Bit-shift', 'right by'];
|
2014-06-10 08:05:14 +02:00
|
|
|
} elsif ($arg{context} eq 'for loop') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['bit-shifting', 'right by'];
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
2014-06-12 15:23:03 +02:00
|
|
|
$return = 'which is bit-shifted right by';
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
| '&='
|
|
|
|
{
|
|
|
|
if ($arg{context} eq 'statement') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['Bit-wise ANDed', 'by' ];
|
2014-06-10 08:05:14 +02:00
|
|
|
} elsif ($arg{context} eq 'for loop') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['bit-wise ANDing', 'by' ];
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
2014-06-12 15:23:03 +02:00
|
|
|
$return = 'which is bit-wise ANDed by';
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
| '^='
|
|
|
|
{
|
|
|
|
if ($arg{context} eq 'statement') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['Exclusive-OR','by'];
|
2014-06-10 08:05:14 +02:00
|
|
|
} elsif ($arg{context} eq 'for loop') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['exclusive-ORing','by'];
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
2014-06-12 15:23:03 +02:00
|
|
|
$return = 'which is exclusive-ORed by';
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
| '|='
|
|
|
|
{
|
|
|
|
if ($arg{context} eq 'statement') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['Bit-wise ORed', 'by'];
|
2014-06-10 08:05:14 +02:00
|
|
|
} elsif ($arg{context} eq 'for loop') {
|
2014-06-12 14:23:16 +02:00
|
|
|
$return = ['bit-wise ORing', 'by'];
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
2014-06-12 15:23:03 +02:00
|
|
|
$return = 'which is bit-wise ORed by';
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
constant_expression:
|
|
|
|
conditional_expression
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
logical_OR_AND_expression:
|
|
|
|
<leftop:
|
|
|
|
rel_add_mul_shift_expression[context => $arg{context}]
|
|
|
|
log_OR_AND_bit_or_and_eq
|
2014-06-15 02:46:49 +02:00
|
|
|
rel_add_mul_shift_expression[context => 'logical_OR_AND_expression']>
|
2014-06-08 08:34:55 +02:00
|
|
|
{
|
|
|
|
if (defined $arg{context} and $arg{context} eq 'for_expression') { print STDERR "hmm2\n"; }
|
|
|
|
my @ands = @{$item[1]};
|
|
|
|
$return = join ('' , @ands);
|
|
|
|
}
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
log_OR_AND_bit_or_and_eq:
|
2014-06-09 12:00:13 +02:00
|
|
|
'||' { $return = ' or '; }
|
|
|
|
| '&&' { $return = ' and '; }
|
2014-06-10 08:05:14 +02:00
|
|
|
| '|' { $return = ' bitwise ORed by '; }
|
|
|
|
| '&' { $return = ' bitwise ANDed by '; }
|
|
|
|
| '^' { $return = ' bitwise XORed by ';}
|
2014-06-08 08:34:55 +02:00
|
|
|
| '==' { $return = ' is equal to ' ; }
|
|
|
|
| '!=' { $return = ' is not equal to ' ; }
|
|
|
|
|
|
|
|
rel_mul_add_ex_op:
|
|
|
|
'+' { $return = ' plus '; }
|
|
|
|
| '-' { $return = ' minus '; }
|
|
|
|
| '*' { $return = ' times '; }
|
|
|
|
| '/' { $return = ' divided by '; }
|
|
|
|
| '%' { $return = ' modulo '; }
|
|
|
|
| '<<' { $return = ' shifted left by '; }
|
|
|
|
| '>>' { $return = ' shifted right by '; }
|
|
|
|
| '>=' { $return = ' is greater than or equal to '; }
|
|
|
|
| "<=" { $return = ' is less than or equal to '; }
|
|
|
|
| '>' { $return = ' is greater than '; }
|
|
|
|
| '<' { $return = ' is less than '; }
|
|
|
|
|
|
|
|
unary_operator:
|
|
|
|
'&' { $return = 'the address of '; }
|
|
|
|
| '*' { $return = 'the contents of '; }
|
2014-06-12 15:13:45 +02:00
|
|
|
| '+' { $return = ''; }
|
2014-06-08 08:34:55 +02:00
|
|
|
| '-' ...constant { $return = 'negative '; }
|
|
|
|
| '-' { $return = 'minus '; }
|
|
|
|
| '~' { $return = "the one's complement of "; }
|
|
|
|
| '!' { $return = 'the logical negation of '; }
|
|
|
|
|
|
|
|
|
|
|
|
rel_add_mul_shift_expression:
|
|
|
|
cast_expression[context => $arg{context}] ...';'
|
|
|
|
{ $return = $item{cast_expression}; }
|
|
|
|
| <leftop:
|
|
|
|
cast_expression[context => $arg{context}]
|
|
|
|
rel_mul_add_ex_op
|
|
|
|
cast_expression[context => 'add_mul_shift_expression'] >
|
|
|
|
{
|
|
|
|
my @ands = @{$item[1]};
|
|
|
|
$return = join ('' , @ands);
|
|
|
|
}
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
closure:
|
|
|
|
',' | ';' | ')'
|
|
|
|
|
|
|
|
cast_expression:
|
|
|
|
'(' type_name ')' cast_expression[context => 'recast']
|
2014-06-12 14:23:16 +02:00
|
|
|
{ $return = "$item{cast_expression} type-casted as $item{type_name}"; }
|
2014-06-08 08:34:55 +02:00
|
|
|
| unary_expression[context => $arg{context}]
|
|
|
|
{ $return = $item{unary_expression}; }
|
|
|
|
|
|
|
|
declaration_list:
|
2014-06-10 08:05:14 +02:00
|
|
|
preproc[context => 'statement'](?) declaration(s)
|
|
|
|
{ $return = join('', @{$item{'preproc(?)'}}) . join('', @{$item{'declaration(s)'}}); }
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
declaration:
|
2014-06-12 16:19:39 +02:00
|
|
|
function_prototype
|
|
|
|
| declaration_specifiers init_declarator_list(?) ';'
|
2014-06-08 08:34:55 +02:00
|
|
|
{
|
2014-06-08 15:51:29 +02:00
|
|
|
my @init_list = defined $item{'init_declarator_list(?)'}->[0] ? @{$item{'init_declarator_list(?)'}->[0]} : ('');
|
2014-06-08 08:34:55 +02:00
|
|
|
my $init_declaration_list;
|
|
|
|
|
|
|
|
if ($item{declaration_specifiers} =~ s/type definition of //) {
|
|
|
|
if(@init_list > 1) {
|
|
|
|
my $last = pop @init_list;
|
|
|
|
$init_declaration_list = join(', ', @init_list) . ' and ' . $last;
|
|
|
|
push $last, @init_list;
|
|
|
|
} else {
|
|
|
|
$init_declaration_list = $init_list[0];
|
|
|
|
}
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
$return = "Let $init_declaration_list be another name for $item{declaration_specifiers}.\n";
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 15:51:29 +02:00
|
|
|
push @defined_types, @init_list;
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
|
|
|
while(@init_list) {
|
|
|
|
$return .= "Let ";
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
my $first_object = shift @init_list;
|
2014-06-10 19:01:43 +02:00
|
|
|
my @args = split /\|/, $first_object, 3;
|
2014-06-08 08:34:55 +02:00
|
|
|
|
2014-06-10 19:01:43 +02:00
|
|
|
my ($first_qualifier, $first_initializer);
|
|
|
|
my $first_identifier = shift @args;
|
|
|
|
|
|
|
|
if(not length $first_identifier) {
|
|
|
|
$first_identifier = 'there';
|
2014-06-07 15:00:07 +02:00
|
|
|
}
|
|
|
|
|
2014-06-10 19:01:43 +02:00
|
|
|
my @identifiers = ($first_identifier);
|
|
|
|
|
|
|
|
my $next_arg = shift @args;
|
|
|
|
if($next_arg =~ m/initialized/) {
|
|
|
|
$first_initializer = $next_arg;
|
|
|
|
$first_qualifier = shift @args // '';
|
2014-06-08 15:51:29 +02:00
|
|
|
} else {
|
2014-06-10 19:01:43 +02:00
|
|
|
$first_qualifier = $next_arg;
|
|
|
|
$first_initializer = shift @args // '';
|
2014-06-08 15:51:29 +02:00
|
|
|
}
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-14 19:54:20 +02:00
|
|
|
if($first_initializer !~ /^initialized/) {
|
|
|
|
if($first_qualifier =~ /\|initialized/) {
|
|
|
|
my ($fq, $fi) = split /\|/, $first_qualifier, 2;
|
|
|
|
$first_qualifier .= " $fq";
|
|
|
|
$first_initializer = $fi;
|
|
|
|
} elsif($first_initializer =~ /\|initialized/) {
|
|
|
|
my ($fq, $fi) = split /\|/, $first_initializer, 2;
|
|
|
|
$first_qualifier .= " $fq";
|
|
|
|
$first_initializer = $fi;
|
|
|
|
} else {
|
2014-06-16 04:31:13 +02:00
|
|
|
$first_qualifier .= " $first_initializer" if $first_initializer;
|
2014-06-14 19:54:20 +02:00
|
|
|
$first_initializer = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-10 19:01:43 +02:00
|
|
|
my @initializers;
|
|
|
|
if($first_initializer) {
|
|
|
|
push @initializers, [ $first_identifier, $first_initializer ];
|
|
|
|
}
|
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
for(my $i = 0; $i < @init_list; $i++) {
|
2014-06-10 19:01:43 +02:00
|
|
|
@args = split /\|/, $init_list[$i], 3;
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-10 19:01:43 +02:00
|
|
|
my ($qualifier, $initializer);
|
|
|
|
my $identifier = shift @args;
|
|
|
|
$next_arg = shift @args;
|
|
|
|
|
|
|
|
if($next_arg =~ m/initialized/) {
|
|
|
|
$initializer = $next_arg;
|
|
|
|
$qualifier = shift @args // '';
|
|
|
|
} else {
|
|
|
|
$qualifier = $next_arg;
|
|
|
|
$initializer = shift @args // '';
|
2014-06-07 15:00:07 +02:00
|
|
|
}
|
|
|
|
|
2014-06-10 19:01:43 +02:00
|
|
|
next unless $qualifier eq $first_qualifier;
|
|
|
|
|
|
|
|
push @identifiers, $identifier;
|
|
|
|
if($initializer) {
|
|
|
|
push @initializers, [ $identifier, $initializer ];
|
|
|
|
}
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
splice @init_list, $i--, 1;
|
2014-06-10 19:01:43 +02:00
|
|
|
}
|
2014-06-08 08:34:55 +02:00
|
|
|
|
2014-06-10 19:01:43 +02:00
|
|
|
my $and = @identifiers > 1 ? ' and ' : '';
|
|
|
|
my $comma = '';
|
|
|
|
for(my $i = 0; $i < @identifiers; $i++) {
|
|
|
|
if($i == @identifiers - 1) {
|
|
|
|
$return .= "$and$identifiers[$i]";
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
2014-06-10 19:01:43 +02:00
|
|
|
$return .= "$comma$identifiers[$i]";
|
|
|
|
$comma = ', ';
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-10 19:01:43 +02:00
|
|
|
|
|
|
|
$return .= ' be ';
|
|
|
|
|
|
|
|
if($first_qualifier) {
|
2014-06-11 14:50:16 +02:00
|
|
|
if(@identifiers == 1 and $first_qualifier !~ /^(a|an)\s+/) {
|
2014-06-10 19:01:43 +02:00
|
|
|
$return .= $first_qualifier =~ m/^[aeiouy]/ ? 'an ' : 'a ';
|
|
|
|
}
|
|
|
|
$return .= "$first_qualifier $item{declaration_specifiers}";
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
2014-06-10 19:01:43 +02:00
|
|
|
if(@identifiers == 1 and $item{declaration_specifiers} !~ /^(a|an)\s+/) {
|
|
|
|
$return .= $item{declaration_specifiers} =~ m/^[aeiouy]/ ? 'an ' : 'a ';
|
|
|
|
}
|
|
|
|
$return .= "$item{declaration_specifiers}";
|
2014-06-07 15:00:07 +02:00
|
|
|
}
|
2014-06-10 19:01:43 +02:00
|
|
|
|
|
|
|
if(@initializers) {
|
|
|
|
if(@identifiers > 1) {
|
|
|
|
$return .= ".\nInitialize ";
|
|
|
|
|
|
|
|
@initializers = sort { $a->[1] cmp $b->[1] } @initializers;
|
|
|
|
my ($and, $comma);
|
|
|
|
|
|
|
|
for(my $i = 0; $i < @initializers; $i++) {
|
|
|
|
my ($identifier, $initializer) = @{$initializers[$i]};
|
|
|
|
|
|
|
|
if($i < @initializers - 1 and $initializer eq $initializers[$i + 1]->[1]) {
|
|
|
|
$return .= "$comma$identifier";
|
|
|
|
$comma = ', ';
|
|
|
|
$and = ' and ';
|
|
|
|
} else {
|
|
|
|
$initializer =~ s/^initialized to //;
|
|
|
|
$return .= "$and$identifier to $initializer";
|
|
|
|
if($i < @initializers - 2) {
|
2014-06-10 23:12:09 +02:00
|
|
|
$and = $comma = ', ';
|
2014-06-10 19:01:43 +02:00
|
|
|
} else {
|
|
|
|
$and = ' and ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$return .= " $initializers[0]->[1]";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$return .= ".\n";
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
init_declarator_list:
|
|
|
|
<leftop: init_declarator ',' init_declarator>
|
|
|
|
|
|
|
|
init_declarator:
|
|
|
|
declarator[context => 'init_declarator']
|
|
|
|
{
|
|
|
|
$return = $item{declarator};
|
|
|
|
}
|
|
|
|
('=' initializer)(?)
|
|
|
|
{
|
|
|
|
my $init = join('',@{$item[-1]});
|
|
|
|
|
|
|
|
if (length $init) {
|
2014-06-10 19:01:43 +02:00
|
|
|
$return = "$item{declarator}|initialized to $init";
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
initializer:
|
|
|
|
comment(?) assignment_expression comment(?)
|
|
|
|
{
|
|
|
|
$return = $item[2];
|
|
|
|
|
|
|
|
if (join('',@{$item[1]})) {
|
|
|
|
$return = '['.join('',@{$item[1]}).']' . $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (join('',@{$item[1]})) {
|
|
|
|
$return .= join('',@{$item[-1]});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
| '{' comment(?) initializer_list (',' )(?) '}'
|
2014-06-12 15:13:45 +02:00
|
|
|
{ $return = 'the set ' . $item{'initializer_list'}; }
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
initializer_list:
|
|
|
|
<leftop: initializer ',' initializer >
|
|
|
|
{
|
|
|
|
my @inits = @{$item[1]};
|
|
|
|
|
|
|
|
if ($#inits >1) {
|
|
|
|
my $init = pop @inits;
|
|
|
|
$return = join(', ',@inits) . ', and ' .$init;
|
|
|
|
} elsif ($#inits == 1) {
|
|
|
|
$return = $inits[0] . ' and ' . $inits[1];
|
|
|
|
} else {
|
|
|
|
$return = $inits[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unary_expression:
|
|
|
|
postfix_expression[context => $arg{context}]
|
|
|
|
{ $return = $item{postfix_expression}; }
|
|
|
|
| '++' unary_expression
|
|
|
|
{
|
|
|
|
if ($arg{context} eq 'statement' ) {
|
|
|
|
$return = "pre-increment $item{unary_expression}";
|
|
|
|
} else {
|
|
|
|
$return = "the pre-incremented $item{unary_expression}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
| '--' unary_expression
|
|
|
|
{
|
|
|
|
if ($arg{context} eq 'statement' ) {
|
|
|
|
$return = "Pre-decrement $item{unary_expression}";
|
|
|
|
} else {
|
|
|
|
$return = "the pre-decremented $item{unary_expression}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
| unary_operator cast_expression[context => $arg{context}]
|
|
|
|
{ $return = $item{unary_operator} . $item{cast_expression}; }
|
|
|
|
|'sizeof' unary_expression
|
|
|
|
{ $return = "the size of $item{unary_expression}"; }
|
|
|
|
|'sizeof' '(' type_name ')'
|
2014-06-16 04:31:13 +02:00
|
|
|
{ $return = "the size of the type $item{type_name}"; }
|
2014-06-08 08:34:55 +02:00
|
|
|
|
2014-06-16 04:31:13 +02:00
|
|
|
postfix_productions:
|
|
|
|
'(' argument_expression_list(?) ')' postfix_productions[context => 'function call'](?)
|
2014-06-08 08:34:55 +02:00
|
|
|
{
|
2014-06-16 04:31:13 +02:00
|
|
|
my $postfix = $item[-1]->[0];
|
|
|
|
|
|
|
|
if(not defined $arg{context} or $arg{context} ne 'statement') {
|
2014-06-15 05:57:27 +02:00
|
|
|
$return = "the result of the function $arg{primary_expression}";
|
2014-06-10 10:50:19 +02:00
|
|
|
} else {
|
2014-06-15 05:57:27 +02:00
|
|
|
$return = "Call the function $arg{primary_expression} ";
|
2014-06-10 10:50:19 +02:00
|
|
|
}
|
2014-06-08 08:34:55 +02:00
|
|
|
|
2014-06-10 10:50:19 +02:00
|
|
|
# To discriminate between macros and functions.
|
|
|
|
foreach (@macros) {
|
2014-06-16 04:31:13 +02:00
|
|
|
if ($arg{primary_expression} eq $_) {
|
2014-06-10 10:50:19 +02:00
|
|
|
$return =~ s/Call/Insert/;
|
|
|
|
$return =~ s/function/macro/;
|
|
|
|
}
|
|
|
|
}
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-10 10:50:19 +02:00
|
|
|
my $arg_exp_list = join('',@{$item{'argument_expression_list(?)'}});
|
|
|
|
if ($arg_exp_list) {
|
|
|
|
$return .= " with argument$arg_exp_list";
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
2014-06-15 05:57:27 +02:00
|
|
|
|
2014-06-16 04:31:13 +02:00
|
|
|
if($postfix) {
|
2014-06-16 23:13:30 +02:00
|
|
|
$return =~ s/^(Call|Insert)/the result of/;
|
2014-06-16 04:31:13 +02:00
|
|
|
$return = "$postfix $return";
|
2014-06-15 05:57:27 +02:00
|
|
|
}
|
2014-06-16 04:31:13 +02:00
|
|
|
1;
|
2014-06-15 05:57:27 +02:00
|
|
|
}
|
2014-06-16 04:31:13 +02:00
|
|
|
| # array reference and plain expression
|
2014-06-08 08:34:55 +02:00
|
|
|
( '[' expression[context => 'array_address'] ']'
|
|
|
|
{ $return = $item{expression}; }
|
2014-06-16 04:31:13 +02:00
|
|
|
)(s) postfix_productions[context => 'array_address'](?)
|
2014-06-08 08:34:55 +02:00
|
|
|
{
|
|
|
|
my $item_expression = '';
|
2014-06-16 04:31:13 +02:00
|
|
|
if (@{$item[-2]}) {
|
|
|
|
$item_expression = join(' and ', @{$item[-2]});
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
|
2014-06-16 04:31:13 +02:00
|
|
|
my $postfix = $item[-1]->[0];
|
|
|
|
|
2014-06-12 15:13:45 +02:00
|
|
|
if (length $item_expression) {
|
|
|
|
if($item_expression =~ /^\d+$/) {
|
|
|
|
$item_expression++;
|
|
|
|
my ($last_digit) = $item_expression =~ /(\d)$/;
|
|
|
|
if($last_digit == 1) {
|
|
|
|
$item_expression .= 'st';
|
|
|
|
} elsif($last_digit == 2) {
|
|
|
|
$item_expression .= 'nd';
|
|
|
|
} elsif($last_digit == 3) {
|
|
|
|
$item_expression .= 'rd';
|
|
|
|
} else {
|
|
|
|
$item_expression .= 'th';
|
|
|
|
}
|
2014-06-16 04:31:13 +02:00
|
|
|
if($arg{context} eq 'function call') {
|
|
|
|
$return = "the $item_expression element of";
|
|
|
|
} else {
|
|
|
|
$return = "the $item_expression element of $arg{primary_expression}";
|
|
|
|
}
|
2014-06-12 15:13:45 +02:00
|
|
|
} elsif($item_expression =~ /^-\s*\d+$/) {
|
|
|
|
$item_expression *= -1;
|
|
|
|
my $plural = $item_expression == 1 ? '' : 's';
|
2014-06-16 04:31:13 +02:00
|
|
|
$return = "the location $item_expression element$plural backwards from where $arg{primary_expression} points";
|
2014-06-12 15:13:45 +02:00
|
|
|
} else {
|
2014-06-16 04:31:13 +02:00
|
|
|
$return = "the element of $arg{primary_expression} at location $item_expression";
|
2014-06-12 15:13:45 +02:00
|
|
|
}
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
|
2014-06-16 04:31:13 +02:00
|
|
|
if($postfix) {
|
|
|
|
$return = "$postfix $return";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
| ('.' identifier)(s) postfix_productions[context => 'struct access'](?)
|
2014-06-08 08:34:55 +02:00
|
|
|
{
|
2014-06-16 04:31:13 +02:00
|
|
|
my $identifier = join('',@{$item[-2]});
|
|
|
|
my $postfix = $item[-1]->[0];
|
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
if ($identifier) {
|
2014-06-16 04:31:13 +02:00
|
|
|
if($arg{context} eq 'array_address') {
|
|
|
|
$return = "member $identifier of";
|
|
|
|
} else {
|
|
|
|
$return = "the member $identifier of $arg{primary_expression}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($postfix) {
|
|
|
|
$return = "$postfix->[0] $return $postfix->[1]";
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-16 04:31:13 +02:00
|
|
|
| ('->' identifier)(s) postfix_productions[context => 'struct access'](?)
|
2014-06-08 08:34:55 +02:00
|
|
|
{
|
2014-06-16 04:31:13 +02:00
|
|
|
my $identifier = join('',@{$item[-2]});
|
|
|
|
my $postfix = $item[-1]->[0];
|
|
|
|
|
2014-06-08 17:59:43 +02:00
|
|
|
if ($identifier) {
|
2014-06-16 04:31:13 +02:00
|
|
|
$return = "the member $identifier of the structure pointed to by $arg{primary_expression}";
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
2014-06-16 04:31:13 +02:00
|
|
|
|
|
|
|
if($postfix) {
|
|
|
|
$return = "$postfix->[0] $return $postfix->[1]";
|
|
|
|
}
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
2014-06-16 04:31:13 +02:00
|
|
|
| ('++')(s)
|
2014-06-08 08:34:55 +02:00
|
|
|
{
|
|
|
|
my $increment = join('',@{$item[-1]});
|
|
|
|
if ($increment) {
|
|
|
|
if ($arg{context} eq 'statement') {
|
2014-06-16 04:31:13 +02:00
|
|
|
$return = "increment $arg{primary_expression} by one";
|
|
|
|
} elsif($arg{context} eq 'struct access') {
|
|
|
|
$return = ['increment', 'by one'];
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
2014-06-10 08:05:14 +02:00
|
|
|
$return = "$return which is incremented by one";
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-16 04:31:13 +02:00
|
|
|
| ('--')(s)
|
2014-06-08 08:34:55 +02:00
|
|
|
{
|
|
|
|
my $increment = join('',@{$item[-1]});
|
|
|
|
if ($increment) {
|
|
|
|
if ($arg{context} eq 'statement') {
|
2014-06-16 04:31:13 +02:00
|
|
|
$return = "decrement $arg{primary_expression} by one";
|
|
|
|
} elsif($arg{context} eq 'struct access') {
|
|
|
|
$return = ['decrement', 'by one'];
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
2014-06-10 08:05:14 +02:00
|
|
|
$return = "$return which is decremented by one";
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# having done the simplest cases, we go to the catch all for left recursions.
|
|
|
|
| primary_expression postfix_suffix(s)
|
|
|
|
{
|
2014-06-16 04:31:13 +02:00
|
|
|
# is this ever reached?
|
|
|
|
print STDERR "Untested code!\n";
|
2014-06-08 08:34:55 +02:00
|
|
|
$return = $item{primary_expression} . "'s " . join('',@{$item{'postfix_suffix(s)'}});
|
|
|
|
}
|
2014-06-16 04:31:13 +02:00
|
|
|
| {""}
|
|
|
|
|
|
|
|
postfix_expression:
|
|
|
|
primary_expression[context => $arg{context}] postfix_productions[primary_expression => $item[1], context => $arg{context}]
|
|
|
|
{
|
|
|
|
my $postfix_productions = $item{'postfix_productions'};
|
|
|
|
|
|
|
|
if(length $postfix_productions) {
|
|
|
|
$return = $postfix_productions;
|
|
|
|
} elsif(length $item{primary_expression}) {
|
|
|
|
$return = $item{primary_expression};
|
|
|
|
} else {
|
|
|
|
$return = undef;
|
|
|
|
}
|
|
|
|
}
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
postfix_suffix:
|
|
|
|
('[' expression ']')(s)
|
|
|
|
| '.' identifier
|
|
|
|
| '->' identifier
|
|
|
|
| '++'
|
|
|
|
| '--'
|
|
|
|
|
|
|
|
argument_expression_list:
|
2014-06-16 04:31:13 +02:00
|
|
|
<leftop: assignment_expression[context => 'function argument'] ',' assignment_expression[context => 'function argument']>
|
2014-06-08 08:34:55 +02:00
|
|
|
{
|
|
|
|
my @arg_exp_list = @{$item[1]};
|
|
|
|
my $last = '';
|
2014-06-15 05:57:27 +02:00
|
|
|
if (@arg_exp_list > 2) {
|
2014-06-08 08:34:55 +02:00
|
|
|
$last = pop @arg_exp_list;
|
2014-06-08 17:59:43 +02:00
|
|
|
$return = 's ' . join(', ', @arg_exp_list) . ", and $last";
|
2014-06-15 05:57:27 +02:00
|
|
|
} elsif (@arg_exp_list == 2 ) {
|
2014-06-08 17:59:43 +02:00
|
|
|
$return = "s $arg_exp_list[0] and $arg_exp_list[1]";
|
2014-06-15 05:57:27 +02:00
|
|
|
} else {
|
|
|
|
if ($arg_exp_list[0]) {
|
|
|
|
$return = " $arg_exp_list[0]";
|
|
|
|
} else {
|
|
|
|
$return = '';
|
|
|
|
}
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
2014-06-15 05:57:27 +02:00
|
|
|
}
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
narrow_closure:
|
2014-06-15 05:57:27 +02:00
|
|
|
';' | ',' | '->'
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
primary_expression:
|
|
|
|
'(' expression ')' (...narrow_closure)(?)
|
|
|
|
{
|
|
|
|
my $expression = $item{expression} ;
|
|
|
|
my $repeats = 1;
|
|
|
|
|
2014-06-15 02:46:49 +02:00
|
|
|
if ($expression =~ /^the expression (\(+)/) {
|
|
|
|
$repeats = (length $1) + 1;
|
|
|
|
$expression =~ s/^the expression \(+//;
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
2014-06-15 02:46:49 +02:00
|
|
|
|
|
|
|
$expression .= ')';
|
|
|
|
$return = "the expression ";
|
|
|
|
$return .= '(' x $repeats;
|
|
|
|
$return .= $expression;
|
|
|
|
}
|
2014-06-08 08:34:55 +02:00
|
|
|
| constant
|
|
|
|
| string
|
|
|
|
| identifier
|
2014-06-16 04:31:13 +02:00
|
|
|
| {} # nothing
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
declarator:
|
|
|
|
direct_declarator
|
|
|
|
| pointer direct_declarator
|
2014-06-10 19:01:43 +02:00
|
|
|
{ $return = "$item{direct_declarator}|$item{pointer}"; }
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
direct_declarator:
|
|
|
|
identifier[context => 'direct_declarator'] array_declarator(s?)
|
|
|
|
{
|
|
|
|
if(@{$item{'array_declarator(s?)'}}) {
|
2014-06-11 10:29:58 +02:00
|
|
|
$return = "$item{identifier}|" . join('', @{$item{'array_declarator(s?)'}});
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
2014-06-08 15:51:29 +02:00
|
|
|
$return = "$item{identifier}";
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
| '(' declarator ')' array_declarator(s)
|
2014-06-14 19:54:20 +02:00
|
|
|
{ $return = "$item{declarator} " . join('', @{$item{'array_declarator(s)'}}); }
|
2014-06-08 08:34:55 +02:00
|
|
|
| '(' declarator array_declarator(s) ')'
|
2014-06-11 10:29:58 +02:00
|
|
|
{ $return = $item{'declarator'} . join('', @{$item{'array_declarator(s)'}}) }
|
2014-06-08 08:34:55 +02:00
|
|
|
| '(' declarator ')'
|
|
|
|
{ $return = $item{declarator}; }
|
|
|
|
|
|
|
|
array_declarator:
|
|
|
|
( '[' assignment_expression(?) ']'
|
|
|
|
{
|
|
|
|
if (@{$item{'assignment_expression(?)'}}) {
|
2014-06-08 15:51:29 +02:00
|
|
|
$return = 'size '. join('',@{$item{'assignment_expression(?)'}}) . ' ';
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
2014-06-08 15:51:29 +02:00
|
|
|
$return = 'unspecified size ';
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)(s?)
|
|
|
|
{
|
|
|
|
my @array = @{$item[-1]};
|
|
|
|
if (@array) {
|
2014-06-11 14:50:16 +02:00
|
|
|
$return .= 'an array of ' . join('of an array of ' , @array) . 'of';
|
2014-06-08 08:34:55 +02:00
|
|
|
} else {
|
|
|
|
undef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
identifier_list:
|
|
|
|
(identifier ',')(s?) identifier
|
|
|
|
{
|
|
|
|
my @identifier_list = @{$item[1]};
|
|
|
|
if ($#identifier_list > 1) {
|
|
|
|
$return = join(', ', @identifier_list) . ', and ' . $item{identifier};
|
|
|
|
} elsif ($#identifier_list == 1) {
|
|
|
|
$return = $identifier_list[1] . ' and ' . $item{identifier};
|
|
|
|
} else {
|
|
|
|
$return = $item{identifier};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parameter_type_list:
|
|
|
|
<skip: '[ \t]*'> parameter_list
|
|
|
|
| parameter_list ',' '...' # FIXME: never reached
|
|
|
|
{ $return = $item{parameter_list} . ', and possibly other arguments'; }
|
|
|
|
|
|
|
|
parameter_list:
|
|
|
|
<leftop: parameter_declaration ',' parameter_declaration>
|
|
|
|
{
|
|
|
|
my @parameter_list = @{$item[1]};
|
|
|
|
if ($#parameter_list > 1) {
|
|
|
|
$return = pop(@parameter_list);
|
|
|
|
$return = join(', ', @parameter_list) . ', and ' . $return;
|
|
|
|
} elsif ($#parameter_list == 1) {
|
|
|
|
$return = $parameter_list[0] . ' and ' .$parameter_list[1];
|
|
|
|
} else {
|
|
|
|
if(ref $parameter_list[0] eq 'ARRAY') {
|
|
|
|
my $list = join('', @{ $parameter_list[0] });
|
|
|
|
if(not $list) {
|
|
|
|
$return = "no parameters";
|
|
|
|
} else {
|
|
|
|
$return = $list;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$return = $parameter_list[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parameter_declaration:
|
|
|
|
declaration_specifiers declarator
|
|
|
|
{ $return = $item{declaration_specifiers} . ' ' . $item{declarator}; }
|
|
|
|
| /,?\.\.\./
|
|
|
|
{ $return = "variadic parameters"; }
|
|
|
|
| declaration_specifiers abstract_declarator(?)
|
|
|
|
| ''
|
|
|
|
{ $return = "unspecified parameters"; }
|
|
|
|
|
|
|
|
abstract_declarator:
|
|
|
|
pointer
|
|
|
|
| pointer(?) direct_abstract_declarator
|
|
|
|
{ $return = join('',@{$item{'pointer(?)'}}) . $item{direct_abstract_declarator}; }
|
|
|
|
|
|
|
|
direct_abstract_declarator:
|
|
|
|
'(' abstract_declarator ')'
|
|
|
|
| '[' ']'
|
|
|
|
| '[' constant_expression ']'
|
|
|
|
| DAD '[' ']'
|
|
|
|
| DAD '[' constant_expression ']'
|
|
|
|
| '(' ')'
|
|
|
|
| '(' parameter_type_list ')'
|
|
|
|
| DAD '(' ')'
|
|
|
|
| DAD '(' parameter_type_list ')'
|
|
|
|
|
|
|
|
DAD: # macro for direct_abstract_declarator
|
|
|
|
( '(' abstract_declarator ')' )(s?)
|
|
|
|
( '[' ']' )(s?)
|
|
|
|
( '[' constant_expression ']' )(s?)
|
|
|
|
( '(' ')' )(s?)
|
|
|
|
( '(' parameter_type_list ')' )(s?)
|
|
|
|
|
|
|
|
identifier:
|
|
|
|
...!reserved identifier_word
|
|
|
|
|
|
|
|
pointer:
|
|
|
|
'*' type_qualifier_list(s) pointer(?)
|
2014-06-07 15:00:07 +02:00
|
|
|
{
|
|
|
|
$return = 'a pointer to a ' . join('', @{$item{'type_qualifier_list(s)'}});
|
|
|
|
$return .= ' ' . join('', @{$item{'pointer(?)'}}) if @{$item{'pointer(?)'}};
|
|
|
|
}
|
2014-06-08 08:34:55 +02:00
|
|
|
| ('*')(s)
|
2014-06-07 15:00:07 +02:00
|
|
|
{
|
|
|
|
my $size = $#{$item[1]} +1 ;
|
|
|
|
if ($size > 1) {
|
|
|
|
while($size-- > 1) {
|
|
|
|
$return .= 'pointer to ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$return .= 'pointer to';
|
|
|
|
}
|
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
type_qualifier_list:
|
|
|
|
type_qualifier(s)
|
|
|
|
{ $return = join(' ', @{$item{'type_qualifier(s)'}}); }
|
2014-06-07 15:00:07 +02:00
|
|
|
|
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
declaration_specifiers:
|
|
|
|
comment(?) type_specifier ...identifier
|
|
|
|
{ $return = join('', @{$item{'comment(?)'}}) . $item{type_specifier}; }
|
|
|
|
| comment(?) storage_class_specifier declaration_specifiers(?)
|
|
|
|
{
|
|
|
|
my $decl_spec = join(' ', @{$item{'declaration_specifiers(?)'}});
|
|
|
|
$return = join('',@{$item{'comment(?)'}}) . $item{storage_class_specifier} ;
|
|
|
|
if ($decl_spec) { $return .= ' ' . $decl_spec; }
|
|
|
|
}
|
|
|
|
| comment(?) type_specifier declaration_specifiers(?)
|
|
|
|
{
|
|
|
|
my $decl_spec = join(' ', @{$item{'declaration_specifiers(?)'}});
|
|
|
|
$return = join('',@{$item{'comment(?)'}}) . $item{type_specifier};
|
|
|
|
if ($decl_spec) { $return .= ' ' . $decl_spec; }
|
|
|
|
}
|
|
|
|
| comment(?) type_qualifier declaration_specifiers(?)
|
|
|
|
{
|
|
|
|
my $decl_spec = $return = join('',@{$item{'comment(?)'}}) . $item{type_qualifier} . ' ' . join(' ',@{$item{'declaration_specifiers(?)'}});
|
|
|
|
}
|
|
|
|
|
|
|
|
storage_class_specifier:
|
|
|
|
'auto'
|
|
|
|
{ $return = "(auto)"; }
|
|
|
|
| 'extern'
|
|
|
|
{ $return = "(declared elsewhere)"; }
|
|
|
|
| 'static'
|
|
|
|
{ $return = "(this declaration is not to be shared)"; }
|
|
|
|
| 'register'
|
|
|
|
{ $return = "(suggestion to be as fast as possible)"; }
|
|
|
|
| 'typedef'
|
|
|
|
{ $return = 'type definition of'; }
|
|
|
|
|
|
|
|
type_qualifier:
|
|
|
|
'const'
|
|
|
|
{ $return = "constant"; }
|
|
|
|
| 'volatile'
|
|
|
|
|
|
|
|
type_specifier:
|
|
|
|
'double'
|
|
|
|
| 'short'
|
2014-06-12 14:23:16 +02:00
|
|
|
| 'long'
|
2014-06-08 08:34:55 +02:00
|
|
|
| 'char'
|
|
|
|
| 'int'
|
|
|
|
| 'float'
|
|
|
|
| 'void'
|
|
|
|
| 'signed'
|
|
|
|
| 'unsigned'
|
|
|
|
| struct_or_union_specifier
|
|
|
|
| enum_specifier
|
|
|
|
| typedef_name
|
|
|
|
|
|
|
|
typedef_name:
|
|
|
|
identifier
|
|
|
|
{
|
|
|
|
my $answer = 0;
|
|
|
|
foreach (@defined_types) {
|
|
|
|
if ($item{identifier} eq $_) {
|
|
|
|
$answer = 1;
|
2014-06-09 12:18:26 +02:00
|
|
|
$return = ($item{identifier} =~ m/^`[aeiouy]/ ? 'an ' : 'a ') . $item{identifier};
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$answer) { undef $answer; }
|
|
|
|
$answer;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct_or_union_specifier:
|
|
|
|
comment(?) struct_or_union identifier(?) '{' struct_declaration_list '}'
|
|
|
|
{
|
|
|
|
my $identifier = join('',@{$item{'identifier(?)'}});
|
2014-06-08 15:51:29 +02:00
|
|
|
$return = join('',@{$item{'comment(?)'}}) . $item{struct_or_union};
|
|
|
|
if ($identifier) { $return .= " tagged $identifier"; }
|
2014-06-16 23:13:30 +02:00
|
|
|
my $plural = $item{struct_declaration_list} =~ / and / ? 's' : '';
|
|
|
|
$return .= " with member$plural $item{struct_declaration_list}";
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
| struct_or_union identifier
|
|
|
|
{
|
2014-06-16 23:13:30 +02:00
|
|
|
$item{struct_or_union} =~ s/^(a|an)//;
|
|
|
|
$return = $item{identifier} =~ m/^`[aeiouy]/ ? 'an' : 'a';
|
|
|
|
$return .= " $item{identifier} $item{struct_or_union}";
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct_declaration_list:
|
|
|
|
struct_declaration(s)
|
|
|
|
{
|
|
|
|
my $finaldec;
|
|
|
|
my @declarations = @{$item{'struct_declaration(s)'}};
|
|
|
|
if ($#declarations > 1) {
|
|
|
|
$finaldec = pop @declarations;
|
|
|
|
$return = join(', ', @declarations ) . ', and ' . $finaldec ;
|
|
|
|
} elsif ($#declarations == 1) {
|
|
|
|
$return = join(' and ', @declarations);
|
|
|
|
} else {
|
|
|
|
$return = $declarations[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct_declaration:
|
2014-06-08 15:51:29 +02:00
|
|
|
specifier_qualifier_list ';'
|
|
|
|
| comment(?) specifier_qualifier_list struct_declarator_list ';'
|
2014-06-08 08:34:55 +02:00
|
|
|
{ $return = join('', @{$item{'comment(?)'}}) . $item{specifier_qualifier_list} . ' ' . $item{struct_declarator_list}; }
|
|
|
|
|
|
|
|
type_name:
|
|
|
|
specifier_qualifier_list abstract_declarator(?)
|
2014-06-12 14:23:16 +02:00
|
|
|
{ $return = $item{specifier_qualifier_list}. join('',@{$item{'abstract_declarator(?)'}}); }
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
specifier_qualifier_list:
|
|
|
|
type_specifier specifier_qualifier_list(?)
|
2014-06-12 14:23:16 +02:00
|
|
|
{
|
|
|
|
$return = $item{type_specifier};
|
|
|
|
$return .= ' ' . join('', @{$item{'specifier_qualifier_list(?)'}}) if @{$item{'specifier_qualifier_list(?)'}};
|
|
|
|
}
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
struct_declarator_list:
|
|
|
|
struct_declarator
|
|
|
|
| struct_declarator ',' struct_declarator_list
|
|
|
|
{ $return = $item{struct_declarator} . join('',@{$item{struct_declarator_list}}); }
|
|
|
|
|
|
|
|
struct_declarator:
|
|
|
|
declarator
|
|
|
|
| declarator(?) ':' constant_expression
|
|
|
|
{ $return = join('',@{$item{'declarator(?)'}}) . " a bit field $item{constant_expression}"; }
|
|
|
|
|
|
|
|
struct_or_union:
|
|
|
|
comment(?) ('struct'
|
|
|
|
{ $return = 'a structure'; }
|
2014-06-08 15:51:29 +02:00
|
|
|
| 'union'
|
|
|
|
{ $return = 'an union'; }
|
|
|
|
) comment(?)
|
2014-06-08 08:34:55 +02:00
|
|
|
{
|
|
|
|
shift @item;
|
|
|
|
foreach (@item) {
|
|
|
|
if (ref($_)) {
|
|
|
|
$return .= join('',@{$_});
|
|
|
|
} else {
|
|
|
|
$return .= $_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum_specifier:
|
|
|
|
'enum' identifier(?) '{' enumerator_list '}'
|
|
|
|
{
|
2014-06-09 12:00:13 +02:00
|
|
|
$return .= 'an enumeration';
|
2014-06-08 08:34:55 +02:00
|
|
|
|
2014-06-09 12:18:26 +02:00
|
|
|
if (@{$item{'identifier(?)'}}){
|
|
|
|
$return .= ' of ' . join('',@{$item{'identifier(?)'}});
|
|
|
|
}
|
|
|
|
|
2014-06-09 12:00:13 +02:00
|
|
|
my @enumerator_list = @{$item{enumerator_list}};
|
|
|
|
|
|
|
|
if(@enumerator_list == 1) {
|
2014-06-09 12:18:26 +02:00
|
|
|
$return .= " comprising $enumerator_list[0]";
|
2014-06-09 12:00:13 +02:00
|
|
|
} else {
|
|
|
|
my $last = pop @enumerator_list;
|
2014-06-09 12:18:26 +02:00
|
|
|
$return .= ' comprising ' . join(', ', @enumerator_list) . " and $last";
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-06-09 12:00:13 +02:00
|
|
|
| 'enum' identifier
|
2014-06-09 12:18:26 +02:00
|
|
|
{ $return = "an enumeration of type $item{identifier}"; }
|
2014-06-09 12:00:13 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
enumerator_list:
|
2014-06-09 12:00:13 +02:00
|
|
|
<leftop:enumerator ',' enumerator>
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
enumerator:
|
|
|
|
identifier ( '=' constant_expression )(?)
|
|
|
|
{
|
|
|
|
$return = $item[1];
|
|
|
|
if (@{$item[-1]}) {
|
|
|
|
$return .= 'marking ' . join('', @{$item[-1]});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
comment:
|
|
|
|
comment_c
|
|
|
|
| comment_cxx
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
comment_c:
|
2014-06-11 10:29:58 +02:00
|
|
|
m{/\*[^*]*\*+([^/*][^*]*\*+)*/}s
|
2014-06-08 08:34:55 +02:00
|
|
|
{
|
|
|
|
$return = $item[1];
|
|
|
|
$return =~ s|^/\*+\s*||;
|
|
|
|
$return =~ s|\s*\*+/$||;
|
|
|
|
$return =~ s/"/\\"/g;
|
|
|
|
$return = "\nA comment: \"$return\".\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
comment_cxx:
|
|
|
|
m{//(.*?)\n}
|
|
|
|
{
|
|
|
|
$return = $item[1];
|
|
|
|
$return =~ s|^//\s*||;
|
|
|
|
$return =~ s/\n*$//;
|
2014-06-11 10:54:12 +02:00
|
|
|
$return =~ s/"/\\"/g;
|
2014-06-08 08:34:55 +02:00
|
|
|
$return = "\nQuick comment: \"$return\".\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
constant:
|
2014-06-16 23:13:30 +02:00
|
|
|
/-?[0-9]*\.[0-9]*[lf]{0,2}/i
|
2014-06-08 08:34:55 +02:00
|
|
|
{
|
2014-06-16 23:13:30 +02:00
|
|
|
if ($item[1] =~ s/f$//i) {
|
2014-06-08 08:34:55 +02:00
|
|
|
$return = "the floating point number $item[1]";
|
2014-06-16 23:13:30 +02:00
|
|
|
} elsif ($item[1] =~ s/l$//i) {
|
|
|
|
$return = "long double $item[1]";
|
|
|
|
} else {
|
2014-06-07 15:00:07 +02:00
|
|
|
$return = $item[1];
|
|
|
|
}
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
2014-06-16 23:13:30 +02:00
|
|
|
| /0x[0-9a-f]+[lu]{0,2}/i
|
2014-06-08 08:34:55 +02:00
|
|
|
{
|
2014-06-16 23:13:30 +02:00
|
|
|
$return .= 'unsigned ' if $item[1] =~ s/[Uu]//;
|
|
|
|
$return .= 'long ' if $item[1] =~ s/[Ll]//;
|
|
|
|
$return = "the $return" . "hexadecimal number $item[1]";
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
2014-06-16 23:13:30 +02:00
|
|
|
| /0\d+[lu]{0,2}/i
|
|
|
|
{
|
|
|
|
$return .= 'unsigned ' if $item[1] =~ s/[Uu]//;
|
|
|
|
$return .= 'long ' if $item[1] =~ s/[Ll]//;
|
|
|
|
$return = "the $return" . "octal number $item[1]";
|
|
|
|
}
|
|
|
|
|/-?[0-9]+[lu]{0,2}/i # integer constant
|
2014-06-08 08:34:55 +02:00
|
|
|
{
|
|
|
|
$return = $item[-1];
|
2014-06-16 23:13:30 +02:00
|
|
|
$return = "long $return" if $return =~ s/[Ll]//;
|
|
|
|
$return = "unsigned $return" if $return =~ s/[Uu]//;
|
2014-06-08 08:34:55 +02:00
|
|
|
}
|
2014-06-11 09:42:16 +02:00
|
|
|
| /(?:\'((?:\\\'|(?!\').)*)\')/ # character constant
|
2014-06-10 10:50:19 +02:00
|
|
|
{
|
|
|
|
my $constant = $item[1];
|
|
|
|
|
2014-06-11 09:42:16 +02:00
|
|
|
if($constant eq q('\n')) {
|
2014-06-10 10:50:19 +02:00
|
|
|
$return = 'a newline';
|
2014-06-11 09:42:16 +02:00
|
|
|
} elsif($constant eq q('\f')) {
|
|
|
|
$return = 'a form-feed character';
|
|
|
|
} elsif($constant eq q('\t')) {
|
2014-06-10 10:50:19 +02:00
|
|
|
$return = 'a tab';
|
2014-06-11 09:42:16 +02:00
|
|
|
} elsif($constant eq q('\v')) {
|
|
|
|
$return = 'a vertical tab';
|
|
|
|
} elsif($constant eq q('\b')) {
|
|
|
|
$return = 'an alert character';
|
|
|
|
} elsif($constant eq q('\r')) {
|
|
|
|
$return = 'a carriage-return';
|
|
|
|
} elsif($constant eq q('\b')) {
|
|
|
|
$return = 'a backspace character';
|
|
|
|
} elsif($constant eq q('\'')) {
|
|
|
|
$return = 'a single-quote';
|
2014-06-10 10:50:19 +02:00
|
|
|
} else {
|
|
|
|
$return = $constant;
|
|
|
|
}
|
|
|
|
}
|
2014-06-08 08:34:55 +02:00
|
|
|
# | enumeration_constant
|
|
|
|
# needs more.
|
|
|
|
|
|
|
|
integer_constant:
|
|
|
|
/[0-9]+/
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
identifier_word:
|
|
|
|
/[a-z_\$][a-z0-9_]*/i
|
2014-06-08 15:51:29 +02:00
|
|
|
{ $return = "`$item[-1]`"; }
|
2014-06-08 08:34:55 +02:00
|
|
|
|
|
|
|
string:
|
2014-06-11 09:42:16 +02:00
|
|
|
/(?:\"(?:\\\"|(?!\").)*\")/
|
2014-06-07 15:00:07 +02:00
|
|
|
|
2014-06-08 08:34:55 +02:00
|
|
|
reserved:
|
|
|
|
'int' | 'double' | 'short' | 'volatile' | 'register' | 'float' | 'signed'
|
|
|
|
| 'unsigned' | 'char' | 'for' | 'if' | 'switch' | 'while' | 'do' | 'case'
|
|
|
|
| 'extern' | 'void' | 'exit' | 'return' | 'auto' | 'break' | 'const'
|
|
|
|
| 'continue' | 'default' | 'else' | 'enum' | 'struct' | 'goto' | 'long'
|
2014-06-15 03:09:06 +02:00
|
|
|
| 'register' | 'sizeof' | 'static' | 'typedef' | 'union'
|
2014-06-07 15:00:07 +02:00
|
|
|
|