Advertisement
Guest User

Untitled

a guest
Aug 4th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. gistfile1.pl:
  2.  
  3. use strict; use warnings; use 5.010;
  4.  
  5. use Data::Dumper;
  6.  
  7. my $data = <<'END';
  8. for each order ArtCod ArtSucMov ArtCodMov
  9. ArtFchPrcMes = &Fch
  10. endfor
  11. END
  12.  
  13. my $ast = Parser->parse(\$data);
  14.  
  15. print Dumper $ast;
  16. # Below is a class that wraps the Marpa Parser
  17.  
  18. package Parser;
  19.  
  20. use Marpa::R2;
  21.  
  22. my ($grammar);
  23.  
  24. sub parse {
  25. my ($self, $ref) = @_;
  26. my $recce = Marpa::R2::Scanless::R->new({ grammar => $grammar });
  27. $recce->read($ref);
  28. my $val = $recce->value // die "No parse found";
  29. return $$val;
  30. }
  31.  
  32. BEGIN {
  33. $grammar = Marpa::R2::Scanless::G->new({
  34. bless_package => 'Ast',
  35. source => \<<'END SOURCE',
  36. :default ::= action => [values]
  37. :start ::= StatementList
  38. :discard ~ ws
  39.  
  40. StatementList ::=
  41. ForEachLoop
  42. | AttrAssignment
  43. ForEachLoop ::= 'for each' AttributeList StatementList ('endfor')
  44. AttrAssignment ::= Attribute '=' Expression1
  45. AttributeList ::= AttributeList Attribute
  46. Attribute ::= word
  47. Expression1 ::= variable
  48. variable ::= ampersand word
  49. ampersand ~ '&'
  50. word ~ [\w]+
  51. ws ~ [\s]+
  52. # newline ~ '\n'
  53. END SOURCE
  54. });
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61. $ perl ./gistfile1.pl
  62. Unproductive symbol: AttributeList
  63. Unproductive symbol: ForEachLoop
  64. Error in SLIF parse: No lexemes accepted at line 1, column 1
  65. Rejected lexeme #0: 'for each'; value="for each"; length = 8
  66. * String before error:
  67. * The error was at line 1, column 1, and at character 0x0066 'f', ...
  68. * here: for each order ArtCod ArtSucMov ArtCodMov\n Art
  69. Marpa::R2 exception at ./gistfile1.pl line 25.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement