Advertisement
pellekrogholt

Untitled

Nov 15th, 2011
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. /* Coco/R lexer and parser specification for arithmetic expressions. */
  2. /* 2006-09-14 */
  3.  
  4. /* Build with:
  5. * Coco.exe -namespace Expressions Ex2.ATG
  6. */
  7.  
  8. using System.Collections.Generic;
  9.  
  10. COMPILER Expressions
  11. public int res;
  12.  
  13. /*--------------------------------------------------------------------------*/
  14. CHARACTERS
  15. letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".
  16. digit = "0123456789".
  17. cr = '\r'.
  18. lf = '\n'.
  19. tab = '\t'.
  20. eq = '='.
  21.  
  22. TOKENS
  23. ident = letter {letter | digit}.
  24. number = digit {digit}.
  25. equals = eq eq.
  26.  
  27. IGNORE cr + lf + tab
  28.  
  29. PRODUCTIONS
  30.  
  31. /*------------------------------------------------------------------------*/
  32.  
  33. BoolExpr<out int n> (. int n1, n2; .)
  34. = CompareExpr<out n1> (. n = n1; .)
  35. {
  36. '&' CompareExpr<out n2> (. n = (n1 == 1 && n2 == 1) ? 1 : 0; .)
  37. |
  38. '|' CompareExpr<out n2> (. n = (n1 == 1 || n2 == 1) ? 1 : 0; .)
  39. }
  40. .
  41.  
  42. CompareExpr<out int n> (. int n1, n2; .)
  43. = Expr<out n1> (. n = n1; .)
  44. [
  45. equals Expr<out n2> (. n = (n1 == n2) ? 1 : 0; .)
  46. |
  47. '<' Expr<out n2> (. n = (n1 < n2) ? 1 : 0; .)
  48. |
  49. '>' Expr<out n2> (. n = (n1 > n2) ? 1 : 0; .)
  50. ]
  51. .
  52.  
  53. Expr<out int n> (. int n1, n2; .)
  54. = Term<out n1> (. n = n1; .)
  55. {
  56. '+' Term<out n2> (. n = n+n2; .)
  57. |
  58. '-' Term<out n2> (. n = n-n2; .)
  59. }
  60. .
  61.  
  62. Term<out int n>
  63. = number (. n = Convert.ToInt32(t.val); .)
  64. {
  65. '*' number (. n = n*Convert.ToInt32(t.val); .)
  66. }
  67. .
  68.  
  69. Expressions (. int n; .)
  70. /* = Expr<out n> (. res = n; .) */
  71. = BoolExpr<out n> (. res = n; .)
  72. .
  73.  
  74. END Expressions.
  75.  
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement