Posted to tcl by ccbbaa at Fri Oct 26 13:40:41 GMT 2018view raw

  1.  
  2. PEG Calculator (Expression)
  3. Expression <- Term (' '* AddOp ' '* Term)* ;
  4. Term <- Factor (' '* MulOp ' '* Factor)* ;
  5. Fragment <- '(' ' '* Expression ' '* ')' / Number / Var ;
  6. Factor <- Fragment (' '* PowOp ' '* Fragment)* ;
  7. Number <- Sign? Digit+ ;
  8. Var <- '$' ( 'x'/'y'/'z' ) ;
  9.  
  10. Digit <- '0'/'1'/'2'/'3'/'4'/'5'/'6'/'7'/'8'/'9' ;
  11. Sign <- '-' / '+' ;
  12. MulOp <- '*' / '/' ;
  13. AddOp <- '+' / '-' ;
  14. PowOp <- '**'
  15. END;
  16.  
  17. # cli command to compile:
  18. pt generate \
  19. snit -class Calculator -name Grammar "Grammar0.snit" \
  20. peg "Grammar0.peg"
  21.  
  22. # error:
  23. pt failed: Reading peg Grammar0.peg ...
  24. Generating a snit parser ...
  25. Parse error at position 719 (Line 16, column 0).
  26. ... '**'\nEND;\n ...
  27. -----------^
  28. Expected one of
  29. * A string "END"
  30. * A string "leaf"
  31. * A unicode alphabetical character
  32. * The character '_:'
  33.  
  34. # references:
  35. # https://wiki.tcl-lang.org/page/PEG+Example
  36. # NOTE: the same grammar seems to compile cleanly with pgen, only pt does
  37. # not like it
  38.  
  39. % info pa
  40. 8.6.6
  41.  
  42.