Created
July 7, 2011 16:29
-
-
Save wsdookadr/1069911 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl | |
| use strict; | |
| use warnings; | |
| use Parse::Yapp; | |
| use Data::Dumper; | |
| my $grammar=join('',<DATA>); | |
| my $parser_generator = Parse::Yapp->new( input => $grammar ); | |
| my $parser_code=$parser_generator->Output(classname => 'SmallParser'); | |
| eval $parser_code; | |
| my $parser = SmallParser->new; | |
| #my $str= "6+1*3*5+4+8"; | |
| my $str= "1*3 + 4"; | |
| $parser->YYData->{INPUT} = $str; | |
| my $T = $parser->YYParse(yylex => \&SmallParser::Lexer); | |
| print "str=$str\n"; | |
| print Dumper $T; | |
| __DATA__ | |
| %% | |
| input: S; | |
| S : S '+' S | |
| { [$_[2],$_[1],$_[3]] } | |
| | inmultire | |
| | numar | |
| ; | |
| inmultire: inmultire '*' inmultire | |
| { [$_[2],$_[1],$_[3]] } | |
| | numar | |
| ; | |
| %% | |
| sub Error { | |
| my($parser)=shift; | |
| push(@{$parser->YYData->{ERRLINES}}, $parser->YYData->{LINE}); | |
| } | |
| sub Lexer { | |
| my($parser)=shift; | |
| exists($parser->YYData->{LINE}) | |
| or $parser->YYData->{LINE}=1; | |
| $parser->YYData->{INPUT} | |
| or return('',undef); | |
| $parser->YYData->{INPUT}=~s/^[ \t]//; | |
| for ($parser->YYData->{INPUT}) { | |
| s/^([0-9]+(?:\.[0-9]+)?)// | |
| and return('numar',$1); | |
| s/^([A-Za-z][A-Za-z0-9_]*)// | |
| and return('variabila',$1); | |
| s/^(.)//s | |
| and return($1,$1); | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl | |
| use strict; | |
| use warnings; | |
| use Parse::Yapp; | |
| use Data::Dumper; | |
| my $grammar=join('',<DATA>); | |
| my $parser_generator = Parse::Yapp->new( input => $grammar ); | |
| my $parser_code=$parser_generator->Output(classname => 'SmallParser'); | |
| eval $parser_code; | |
| my $parser = SmallParser->new; | |
| $parser->YYData->{INPUT} = "1*3+4"; | |
| my $T = $parser->YYParse(yylex => \&SmallParser::Lexer); | |
| print Dumper $T; | |
| __DATA__ | |
| #%left '-' '+' | |
| #%left '*' '/' | |
| %% | |
| input: S; | |
| S: adunare | |
| | inmultire | |
| | numar | |
| ; | |
| adunare: S '+' S | |
| { [$_[2],$_[1],$_[3]] } | |
| ; | |
| inmultire: S '*' S | |
| { [$_[2],$_[1],$_[3]] } | |
| ; | |
| %% | |
| sub Error { | |
| my($parser)=shift; | |
| push(@{$parser->YYData->{ERRLINES}}, $parser->YYData->{LINE}); | |
| } | |
| sub Lexer { | |
| my($parser)=shift; | |
| exists($parser->YYData->{LINE}) | |
| or $parser->YYData->{LINE}=1; | |
| $parser->YYData->{INPUT} | |
| or return('',undef); | |
| $parser->YYData->{INPUT}=~s/^[ \t]//; | |
| for ($parser->YYData->{INPUT}) { | |
| s/^([0-9]+(?:\.[0-9]+)?)// | |
| and return('numar',$1); | |
| s/^([A-Za-z][A-Za-z0-9_]*)// | |
| and return('variabila',$1); | |
| s/^(.)//s | |
| and return($1,$1); | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl | |
| use strict; | |
| use warnings; | |
| use Parse::Yapp; | |
| use Data::Dumper; | |
| my $grammar=join('',<DATA>); | |
| my $parser_generator = Parse::Yapp->new( input => $grammar ); | |
| my $parser_code=$parser_generator->Output(classname => 'SmallParser'); | |
| eval $parser_code; | |
| my $parser = SmallParser->new; | |
| $parser->YYData->{INPUT} = "1*3+4"; | |
| my $T = $parser->YYParse(yylex => \&SmallParser::Lexer); | |
| print Dumper $T; | |
| __DATA__ | |
| %left '-' '+' | |
| %left '*' '/' | |
| %% | |
| input: S; | |
| S: adunare | |
| | inmultire | |
| | numar | |
| ; | |
| adunare: S '+' S | |
| { [$_[2],$_[1],$_[3]] } | |
| ; | |
| inmultire: S '*' S | |
| { [$_[2],$_[1],$_[3]] } | |
| ; | |
| %% | |
| sub Error { | |
| my($parser)=shift; | |
| push(@{$parser->YYData->{ERRLINES}}, $parser->YYData->{LINE}); | |
| } | |
| sub Lexer { | |
| my($parser)=shift; | |
| exists($parser->YYData->{LINE}) | |
| or $parser->YYData->{LINE}=1; | |
| $parser->YYData->{INPUT} | |
| or return('',undef); | |
| $parser->YYData->{INPUT}=~s/^[ \t]//; | |
| for ($parser->YYData->{INPUT}) { | |
| s/^([0-9]+(?:\.[0-9]+)?)// | |
| and return('numar',$1); | |
| s/^([A-Za-z][A-Za-z0-9_]*)// | |
| and return('variabila',$1); | |
| s/^(.)//s | |
| and return($1,$1); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment