Home | History | Annotate | Download | only in expr
      1 grammar Expr;
      2 
      3 options {
      4     language = Perl5;
      5 }
      6 
      7 @header {
      8 }
      9 
     10 @members {
     11     my %memory;
     12 }
     13 
     14 prog:   stat+ ;
     15                 
     16 stat:   expr NEWLINE { print "$expr.value\n"; }
     17     |   ID '=' expr NEWLINE
     18         { $memory{$ID.text} = $expr.value; }
     19     |   NEWLINE
     20     ;
     21 
     22 expr returns [value]
     23     :   e=multExpr { $value = $e.value; }
     24         (   '+' e=multExpr { $value += $e.value; }
     25         |   '-' e=multExpr { $value -= $e.value; }
     26         )*
     27     ;
     28 
     29 multExpr returns [value]
     30     :   e=atom { $value = $e.value; } ('*' e=atom { $value *= $e.value; })*
     31     ; 
     32 
     33 atom returns [value]
     34     :   INT { $value = $INT.text; }
     35     |   ID
     36         {
     37             my $v = $memory{$ID.text};
     38             if (defined $v) {
     39                 $value = $v;
     40             } else {
     41                 print STDERR "undefined variable $ID.text\n";
     42             }
     43         }
     44     |   '(' expr ')' { $value = $expr.value; }
     45     ;
     46 
     47 ID  :   ('a'..'z'|'A'..'Z')+ ;
     48 INT :   '0'..'9'+ ;
     49 NEWLINE:'\r'? '\n' ;
     50 WS  :   (' '|'\t')+ { $self->skip(); } ;
     51