Parse::Eyapp a LALR yapp compatible Parser Generator

C

Casiano

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top