Parse::Eyapp a LALR yapp compatible Parser Generator

Discussion in 'Perl Misc' started by Casiano, Jan 30, 2007.

  1. Casiano

    Casiano Guest

    I have released version 1.06550 of Parse::Eyapp in CPAN.

    Parse::Eyapp (the name stands for Extended yapp) is a collection of
    modules that extends Francois Desarmenien Parse::Yapp 1.05. Eyapp
    extends yacc/yapp syntax with functionalities line named attributes,
    EBNF-like expressions, modifiable default action, automatic syntax
    tree building, semi-automatic abstract syntax tree building,
    translation schemes, tree regular expressions, tree transformations,
    scope analysis support, directed acyclic graphs and a few more.

    The following example shows a simple Eyapp program translating
    infix expressions into postfix expressions:

    pl@nereida:~/src/perl/YappWithDefaultAction/examples$ cat -n
    Postfix.eyp
    1 # File Postfix.eyp
    2 %right '='
    3 %left '-' '+'
    4 %left '*' '/'
    5 %left NEG
    6
    7 %defaultaction { return "$left $right $op"; }
    8
    9 %%
    10 line: $exp { print "$exp\n" }
    11 ;
    12
    13 exp: $NUM { $NUM }
    14 | $VAR { $VAR }
    15 | VAR.left '='.op exp.right
    16 | exp.left '+'.op exp.right
    17 | exp.left '-'.op exp.right
    18 | exp.left '*'.op exp.right
    19 | exp.left '/'.op exp.right
    20 | '-' $exp %prec NEG { "$exp NEG" }
    21 | '(' $exp ')' { $exp }
    22 ;
    23
    24 %%
    25
    26 sub _Error {
    27 my($token)=$_[0]->YYCurval;
    28 my($what)= $token ? "input: '$token'" : "end of input";
    29 my @expected = $_[0]->YYExpect();
    30
    31 local $" = ', ';
    32 die "Syntax error near $what. Expected one of these tokens:
    @expected\n";
    33 }
    34
    35 my $x;
    36
    37 sub _Lexer {
    38 my($parser)=shift;
    39
    40 for ($x) {
    41 s/^\s+//;
    42 $_ eq '' and return('',undef);
    43
    44 s/^([0-9]+(?:\.[0-9]+)?)// and return('NUM',$1);
    45 s/^([A-Za-z][A-Za-z0-9_]*)// and return('VAR',$1);
    46 s/^(.)//s and return($1,$1);
    47 }
    48 }
    49
    50 sub Run {
    51 my($self)=shift;
    52 print "Write an expression: ";
    53 $x = <>;
    54 $self->YYParse( yylex => \&_Lexer, yyerror => \&_Error,
    55 #yydebug => 0xFF
    56 );
    57 }

    Have fun


    Casiano
    Casiano, Jan 30, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Demenchuk
    Replies:
    0
    Views:
    486
    Alexander Demenchuk
    Aug 30, 2004
  2. Tommy Nordgren

    Any lalr parser generator for ruby?

    Tommy Nordgren, Dec 22, 2007, in forum: Ruby
    Replies:
    1
    Views:
    133
    Ryan Davis
    Dec 22, 2007
  3. Clint Olsen
    Replies:
    5
    Views:
    145
    Clint Olsen
    Feb 9, 2004
  4. Clint Olsen

    Performance patch to Parse-Yapp 1.05

    Clint Olsen, Feb 9, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    81
    Clint Olsen
    Feb 11, 2004
  5. pantagruel
    Replies:
    0
    Views:
    238
    pantagruel
    Feb 17, 2006
Loading...

Share This Page