From 2dba87f45306b01928028c9c4cb5a7f8812f181c Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Sun, 15 Jun 2014 01:09:06 +0000 Subject: [PATCH] CGrammar: Optimize preproc rules --- PBot/VERSION.pm | 2 +- modules/c2english.pl | 3 +-- modules/c2english/CGrammar.pm | 51 ++++++++++++++++------------------- 3 files changed, 25 insertions(+), 31 deletions(-) diff --git a/PBot/VERSION.pm b/PBot/VERSION.pm index 8845f02e..475a6647 100644 --- a/PBot/VERSION.pm +++ b/PBot/VERSION.pm @@ -13,7 +13,7 @@ use warnings; # These are set automatically by the build/commit script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 640, + BUILD_REVISION => 641, BUILD_DATE => "2014-06-14", }; diff --git a/modules/c2english.pl b/modules/c2english.pl index 82161277..bd92d549 100755 --- a/modules/c2english.pl +++ b/modules/c2english.pl @@ -391,8 +391,7 @@ close $fh; $output = `./c2eng.pl code2eng.c` if not defined $output; if(not $has_function and not $has_main) { - $output =~ s/Let .main. be a function taking no parameters and returning int.\s*To perform the function.\s*//; - $output =~ s/\s*Return 0.\s*$//; + $output =~ s/Let .main. be a function taking no parameters and returning int.\s*To perform the function.\s*(return 0.)?//i; $output =~ s/\s*Return 0.\s*End of function .main..\s*//; $output =~ s/\s*Do nothing.\s*$//; $output =~ s/^\s*(.)/\U$1/; diff --git a/modules/c2english/CGrammar.pm b/modules/c2english/CGrammar.pm index 8fed2de6..56890160 100644 --- a/modules/c2english/CGrammar.pm +++ b/modules/c2english/CGrammar.pm @@ -31,17 +31,17 @@ translation_unit: | preproc[matchrule => 'translation_unit'] preproc: - definition - | undefinition - | inclusion - | line - | error - | pragma - | preproc_conditional[matchrule => $arg{matchrule}] + '#' (definition + | undefinition + | inclusion + | line + | error + | pragma + | preproc_conditional[matchrule => $arg{matchrule}]) definition: macro_definition - | '#' 'define' identifier token_sequence(?) "\n" + | 'define' identifier token_sequence(?) "\n" { my $token_sequence = join('',@{$item{'token_sequence(?)'}}); $return = "Define the macro $item{identifier}"; @@ -50,7 +50,7 @@ definition: } macro_definition: - '#' 'define' identifier '(' ')' token_sequence "\n" + 'define' identifier '(' ')' token_sequence "\n" { my @symbols = @{$item[-5]}; my $last; @@ -66,35 +66,35 @@ macro_definition: } undefinition: - '#' 'undef' identifier "\n" + 'undef' identifier "\n" { @macros = grep { $_ ne $item{identifier} } @macros; $return = "\nAnnul the definition of $item{identifier}.\n"; } inclusion: - '#' 'include' '<' filename '>' "\n" + 'include' '<' filename '>' "\n" { $return = "\nInclude system file $item{filename}.\n"; } - | '#' 'include' '"' filename '"' "\n" + | 'include' '"' filename '"' "\n" { $return = "\nInclude user file $item{filename}.\n"; } - | '#' 'include' token + | 'include' token { $return = "\nImport code noted by the token $item{token}.\n"; } filename: /[_\.\-\w\/]+/ line: - '#' 'line' constant ('"' filename '"' + 'line' constant ('"' filename '"' { $return = "and filename $item{filename}"; } )(?) "\n" { $return = "\nThis is line number $item{constant} " . join('', @{$item[-3]}) . ".\n"; } error: - '#' 'error' token_sequence(?) "\n" + 'error' token_sequence(?) "\n" { $return = "Stop compilation with error \"" . join('', @{$item{'token_sequence(?)'}}) . "\".\n"; } pragma: - '#' 'pragma' token_sequence(?) "\n" + 'pragma' token_sequence(?) "\n" { my $pragma = join('',@{$item{'token_sequence(?)'}}); if ($pragma) { $pragma = ' "$pragma"'; } @@ -109,19 +109,19 @@ preproc_conditional: (elif_parts[matchrule => $rule_name])(?) (else_parts[matchrule => $rule_name])(?) { $return .= join('',@{$item[-2]}) . join('',@{$item[-1]}); } - '#' 'endif' + 'endif' { $return .= "End preprocessor conditional.\n"; } if_line: - '#' 'ifdef' identifier "\n" + 'ifdef' identifier "\n" { $return .= "If the macro $item{identifier} is defined, then ^L"; } - | '#' 'ifndef' identifier "\n" + | 'ifndef' identifier "\n" { $return .= "If the macro $item{identifier} is not defined, then ^L"; } - | '#' 'if' constant_expression "\n" + | 'if' constant_expression "\n" { $return .= "If the preprocessor condition^L $item{constant_expression} is true, then ^L"; } elif_parts: - ('#' 'elif' constant_expression + ('elif' constant_expression { $return .= "Otherwise, if the preprocessor condition $item{constant_expression} is true, then ^L"; } ( )[matchrule => $arg{matchrule}](s?) { $return .= join('',@{$item[-1]}); } @@ -129,7 +129,7 @@ elif_parts: { $return = join('', @{$item[-1]}); } else_parts: - '#' 'else' + 'else' { $rule_name = $arg{matchrule}; } ()[matchrule => $arg{matchrule}](s?) { $return = "Otherwise, ^L" . join('',@{$item[-1]}); } @@ -994,19 +994,14 @@ narrow_closure: primary_expression: '(' expression ')' (...narrow_closure)(?) { - print STDERR "wtf\n"; - print STDERR "expression: [$item{expression}]; text: [$text]\n"; my $expression = $item{expression} ; my $repeats = 1; if ($expression =~ /^the expression (\(+)/) { $repeats = (length $1) + 1; - print STDERR "Got repeats: $repeats from matching [$1]\n"; $expression =~ s/^the expression \(+//; } - print STDERR "repeats: $repeats\n"; - $expression .= ')'; $return = "the expression "; $return .= '(' x $repeats; @@ -1418,5 +1413,5 @@ reserved: | 'unsigned' | 'char' | 'for' | 'if' | 'switch' | 'while' | 'do' | 'case' | 'extern' | 'void' | 'exit' | 'return' | 'auto' | 'break' | 'const' | 'continue' | 'default' | 'else' | 'enum' | 'struct' | 'goto' | 'long' - | 'register' | 'sizeof' | 'static' | 'typedef' | 'union' + | 'register' | 'sizeof' | 'static' | 'typedef' | 'union'