/usr/share/doc/libparse-recdescent-perl/examples/demo_language.pl is in libparse-recdescent-perl 1.967013+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | #!/usr/bin/perl -ws
#SHARED SYMBOL_TABLE
my %symbol_table = ();
package Operation;
sub new
{
my ($class, %args) = @_;
bless \%args, $class;
}
package Assignment_Op; @ISA = qw( Operation );
sub eval
{
my ($self) = @_;
$symbol_table{$self->{var}->{name}}
= $self->{value}->eval();
}
package Addition_Op; @ISA = qw ( Operation );
sub eval
{
my ($self) = @_;
return $self->{left}->eval() + $self->{right}->eval();
}
package Multiplication_Op; @ISA = qw ( Operation );
sub eval
{
my ($self) = @_;
return $self->{left}->eval() * $self->{right}->eval();
}
package IfThenElse_Op; @ISA = qw ( Operation );
sub eval
{
my ($self) = @_;
if ($self->{condition}->eval() )
{
return $self->{true_expr}->eval();
}
else
{
return $self->{false_expr}->eval();
}
}
package LessThan_Op; @ISA = qw ( Operation );
sub eval
{
my ($self) = @_;
return $self->{left}->eval() < $self->{right}->eval();
}
package Value_Op; @ISA = qw( Operation );
sub eval
{
my ($self) = @_;
return $self->{value};
}
package Variable_Op; @ISA = qw( Operation );
sub eval
{
my ($self) = @_;
return $symbol_table{$self->{name}};
}
package Sequence_Op;
sub new
{
my ($class, $list_ref) = @_;
bless $list_ref, $class;
}
sub eval
{
my ($self) = @_;
my $last_val;
foreach my $statement ( @$self )
{
$last_val = $statement->eval();
}
return $last_val;
}
package main;
use Parse::RecDescent;
my $grammar = q
{
Script: Statement(s) /^$/
{ Sequence_Op->new( $item[1] ) }
Statement: Assignment
| IfThenElse
| Expression
| <error>
Assignment: Variable '<-' Expression
{ Assignment_Op->new( var => $item[1],
value => $item[3]) }
Expression: Product "+" Expression
{ Addition_Op->new( left => $item[1],
right => $item[3] ) }
| Product
Product: Value "*" Product
{ Multiplication_Op->new( left => $item[1],
right => $item[3] ) }
| Value
Value: /\d+/
{ Value_Op->new( value => $item[1] ) }
| Variable
Variable: /(?!if)[a-z]/
{ Variable_Op->new( name => $item[1] ); }
IfThenElse: 'if' Condition 'then' Statement 'else' Statement
{ IfThenElse_Op->new( condition => $item[2],
true_expr => $item[4],
false_expr => $item[6]) }
Condition: Expression '<' Expression
{ LessThan_Op->new( left => $item[1],
right => $item[3] ) }
};
my $parser = Parse::RecDescent->new($grammar)
or die "Bad grammar";
local $/;
my $script = <DATA>;
my $tree = $parser->Script($script)
or die "Bad script";
print $tree->eval();
__DATA__
a <- 1
b <- 2
if a<b then
c <- 3
else
c <- 99
b*c+a
|