The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.
# This example illustrates one way to isolate the lexer and error subs
# The lexer and subs are in module "TailModule.pm"
%{
use base 'TailModule';
%}

%right  '='
%left   '-' '+'
%left   '*' '/'
%left   NEG

%defaultaction { return  "$left $right $op"; }

%%
line: $exp  { print "$exp\n" }
;

exp:        $NUM  { $NUM }            
        |   $VAR  { $VAR }            
        |   VAR.left '='.op exp.right         
        |   exp.left '+'.op exp.right         
        |   exp.left '-'.op exp.right        
        |   exp.left '*'.op exp.right       
        |   exp.left '/'.op exp.right      
        |   '-' $exp %prec NEG { "$exp NEG" }
        |   '(' $exp ')' { $exp }      
;

%%