Re: binary shifts in p::rd exe block
[email protected] ("Ron D. Smith") Thu, 05 Jun 2003 11:26:58 -0700
| Newsgroups | perl.recdescent |
|---|---|
| Message-ID | <[email protected]> |
On Thursday, Jun 5, 2003 "h. w. neff" said:
> hi.
> in an attempt to shift & merge returned results, i find i
> cannot use '>>' or '<<' in a p::rd executable block; i
> must instead divide and multiply, respectively.
> e.g. in this code, 'goodrule' works, 'badrule' does not:
>
> sub_rule : /[0-7]/
> goodrule : /reg1/i '=' sub_rule ',' /reg2/i '=' sub_rule ';'
> {
> $main::machineword = $item[3] * 8 | $item[7];
> 1
> }
> badrule : /reg1/i '=' sub_rule ',' /reg2/i '=' sub_rule ';'
> {
> $main::machineword = $item[3] << 3 | $item[7];
> 1
> }
>
> by 'not working' i mean i get undefined sub-rules and
> such as if i was getting file/stream type redirection
> instead of the shifts i want.
I suspect you have not correctly analyzed the problem. Here is my test case:
#!/usr/intel/pkgs/perl/5.005_03/bin/perl
BEGIN {$::RD_HINT=1;
$::RD_TRACE=100;}
use lib "my version of 1.80 of P::RD";
use Parse::RecDescent;
my $grammar = q {
sub_rule : /[0-7]/
goodrule : /reg1/i '=' sub_rule ',' /reg2/i '=' sub_rule ';'
{
$main::machineword = $item[3] * 8 | $item[7];
print "item 3 $item[3] $item[7] $main::machineword\n";
1
}
badrule : /reg1/i '=' sub_rule ',' /reg2/i '=' sub_rule ';'
{
$main::machineword = $item[3] << 3 | $item[7];
print "item 3 $item[3] $item[7] $main::machineword\n";
1
}
};
my $parserRef = new Parse::RecDescent($grammar);
print "Good rule returns:",$parserRef->goodrule("reg1 = 1 , reg2 = 2;"),"
'$main::machineword' \n";
print "Bad rule returns: ",$parserRef->badrule("reg1 = 1 , reg2 = 2;"),"
'$main::machineword'\n";
exit;
I get the following:
unix> test.pl
Parse::RecDescent: Treating "sub_rule :" as a rule declaration
Parse::RecDescent: Treating "/[0-7]/" as a /../ pattern terminal
Parse::RecDescent: Treating "goodrule :" as a rule declaration
Parse::RecDescent: Treating "/reg1/i" as a /../ pattern terminal
Parse::RecDescent: Treating "=" as a literal terminal
Parse::RecDescent: Treating "sub_rule" as a subrule match
Parse::RecDescent: Treating "," as a literal terminal
Parse::RecDescent: Treating "/reg2/i" as a /../ pattern terminal
Parse::RecDescent: Treating "=" as a literal terminal
Parse::RecDescent: Treating "sub_rule" as a subrule match
Parse::RecDescent: Treating ";" as a literal terminal
Parse::RecDescent: Treating "{ $main::machineword = $item[3] * 8 |
$item[7]; print "item 3 $item[3] $item[7]
$main::machineword\n"; 1 }" as an action
Parse::RecDescent: Treating "badrule :" as a rule declaration
Parse::RecDescent: Treating "/reg1/i" as a /../ pattern terminal
Parse::RecDescent: Treating "=" as a literal terminal
Parse::RecDescent: Treating "sub_rule" as a subrule match
Parse::RecDescent: Treating "," as a literal terminal
Parse::RecDescent: Treating "/reg2/i" as a /../ pattern terminal
Parse::RecDescent: Treating "=" as a literal terminal
Parse::RecDescent: Treating "sub_rule" as a subrule match
Parse::RecDescent: Treating ";" as a literal terminal
Parse::RecDescent: Treating "{ $main::machineword = $item[3] << 3 |
$item[7]; print "item 3 $item[3] $item[7]
$main::machineword\n"; 1 }" as an action
printing code (21354) to RD_TRACE
| goodrule |Trying rule: [goodrule] |
| goodrule | |"reg1 = 1 , reg2 =
2;"
| goodrule |Trying production: [/reg1/i '=' sub_rule |
| |',' /reg2/i '=' sub_rule ';'] |
| goodrule |Trying terminal: [/reg1/i] |
| goodrule |>>Matched terminal<< (return value: |
| |[reg1]) |
| goodrule | |" = 1 , reg2 = 2;"
| goodrule |Trying terminal: ['='] |
| goodrule |>>Matched terminal<< (return value: [=]) |
| goodrule | |" 1 , reg2 = 2;"
| goodrule |Trying subrule: [sub_rule] |
| sub_rule |Trying rule: [sub_rule] |
| sub_rule |Trying production: [/[0-7]/] |
| sub_rule |Trying terminal: [/[0-7]/] |
| sub_rule |>>Matched terminal<< (return value: [1]) |
| sub_rule | |" , reg2 = 2;"
| sub_rule |>>Matched production: [/[0-7]/]<< |
| sub_rule |>>Matched rule<< (return value: [1]) |
| sub_rule |(consumed: [ 1]) |
| goodrule |>>Matched subrule: [sub_rule]<< (return |
| |value: [1] |
| goodrule |Trying terminal: [','] |
| goodrule |>>Matched terminal<< (return value: [,]) |
| goodrule | |" reg2 = 2;"
| goodrule |Trying terminal: [/reg2/i] |
| goodrule |>>Matched terminal<< (return value: |
| |[reg2]) |
| goodrule | |" = 2;"
| goodrule |Trying terminal: ['='] |
| goodrule |>>Matched terminal<< (return value: [=]) |
| goodrule | |" 2;"
| goodrule |Trying subrule: [sub_rule] |
| sub_rule |Trying rule: [sub_rule] |
| sub_rule |Trying production: [/[0-7]/] |
| sub_rule |Trying terminal: [/[0-7]/] |
| sub_rule |>>Matched terminal<< (return value: [2]) |
| sub_rule | |";"
| sub_rule |>>Matched production: [/[0-7]/]<< |
| sub_rule |>>Matched rule<< (return value: [2]) |
| sub_rule |(consumed: [ 2]) |
| goodrule |>>Matched subrule: [sub_rule]<< (return |
| |value: [2] |
| goodrule |Trying terminal: [';'] |
| goodrule |>>Matched terminal<< (return value: [;]) |
| goodrule |Trying action |
item 3 1 2 10
| goodrule |>>Matched action<< (return value: [1]) |
| goodrule |>>Matched production: [/reg1/i '=' |
| |sub_rule ',' /reg2/i '=' sub_rule ';']<< |
| goodrule |>>Matched rule<< (return value: [1]) |
| goodrule |(consumed: []) |
Good rule returns:1 '10'
| badrule |Trying rule: [badrule] |
| badrule | |"reg1 = 1 , reg2 =
2;"
| badrule |Trying production: [/reg1/i '=' sub_rule |
| |',' /reg2/i '=' sub_rule ';'] |
| badrule |Trying terminal: [/reg1/i] |
| badrule |>>Matched terminal<< (return value: |
| |[reg1]) |
| badrule | |" = 1 , reg2 = 2;"
| badrule |Trying terminal: ['='] |
| badrule |>>Matched terminal<< (return value: [=]) |
| badrule | |" 1 , reg2 = 2;"
| badrule |Trying subrule: [sub_rule] |
| sub_rule |Trying rule: [sub_rule] |
| sub_rule |Trying production: [/[0-7]/] |
| sub_rule |Trying terminal: [/[0-7]/] |
| sub_rule |>>Matched terminal<< (return value: [1]) |
| sub_rule | |" , reg2 = 2;"
| sub_rule |>>Matched production: [/[0-7]/]<< |
| sub_rule |>>Matched rule<< (return value: [1]) |
| sub_rule |(consumed: [ 1]) |
| badrule |>>Matched subrule: [sub_rule]<< (return |
| |value: [1] |
| badrule |Trying terminal: [','] |
| badrule |>>Matched terminal<< (return value: [,]) |
| badrule | |" reg2 = 2;"
| badrule |Trying terminal: [/reg2/i] |
| badrule |>>Matched terminal<< (return value: |
| |[reg2]) |
| badrule | |" = 2;"
| badrule |Trying terminal: ['='] |
| badrule |>>Matched terminal<< (return value: [=]) |
| badrule | |" 2;"
| badrule |Trying subrule: [sub_rule] |
| sub_rule |Trying rule: [sub_rule] |
| sub_rule |Trying production: [/[0-7]/] |
| sub_rule |Trying terminal: [/[0-7]/] |
| sub_rule |>>Matched terminal<< (return value: [2]) |
| sub_rule | |";"
| sub_rule |>>Matched production: [/[0-7]/]<< |
| sub_rule |>>Matched rule<< (return value: [2]) |
| sub_rule |(consumed: [ 2]) |
| badrule |>>Matched subrule: [sub_rule]<< (return |
| |value: [2] |
| badrule |Trying terminal: [';'] |
| badrule |>>Matched terminal<< (return value: [;]) |
| badrule |Trying action |
item 3 1 2 10
| badrule |>>Matched action<< (return value: [1]) |
| badrule |>>Matched production: [/reg1/i '=' |
| |sub_rule ',' /reg2/i '=' sub_rule ';']<< |
| badrule |>>Matched rule<< (return value: [1]) |
| badrule |(consumed: []) |
Bad rule returns: 1 '10'
I see nothing wrong so your "problem report" has not been reproduced. Can
you recreate this problem *exactly* in a smaller test case?
(Oooh, the next bit is FUGLY...)
> as a result, i've ended up with exe blocks that look like:
>
> {
> $main::Instruction_upper = 0x440000
> | ($item[1] / 2048)
> | $item[4]
> | ($item[8] / 512)
> | ($item[10] * 16)
> | (($item[26] & 0x000010) / 16)
> | ($item[32] / 2)
> ;
> $main::Instruction_lower = 0x000000
> | ($item[14] * 2)
> | ($item[16] / 2048)
> | ($item[20] * 16)
> | ($item[22] / 128)
> | (($item[26] & 0x00000C) * 1048576)
> | (($item[28] & 0xC00000) / 16384)
> | (($item[28] & 0x000003) * 1024)
> | (($item[34] & 0xC00000) / 1024)
> | (($item[34] & 0x000003) * 16384)
> ;
> 1
> }
>
> it does what's desired, but from a maintenance point of view
> it is just not clear, without doing some math, what the
> various shifts are.
> not to mention the increased chances of a typo in these magic
> numbers!
>
> any ideas about where i've gone wrong in trying to use the
> bit shifts?
> or is this a bug in p::rd ?
> thanks.
> hwn
>
--
Intel, Corp.
5000 W. Chandler Blvd.
Chandler, AZ 85226
--
Intel, Corp.
5000 W. Chandler Blvd.
Chandler, AZ 85226