Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Coco/R lexer and parser specification for arithmetic expressions. */
- /* 2006-09-14 */
- /* Build with:
- * Coco.exe -namespace Expressions Ex2.ATG
- */
- using System.Collections.Generic;
- COMPILER Expressions
- public int res;
- /*--------------------------------------------------------------------------*/
- CHARACTERS
- letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".
- digit = "0123456789".
- cr = '\r'.
- lf = '\n'.
- tab = '\t'.
- eq = '='.
- TOKENS
- ident = letter {letter | digit}.
- number = digit {digit}.
- equals = eq eq.
- IGNORE cr + lf + tab
- PRODUCTIONS
- /*------------------------------------------------------------------------*/
- BoolExpr<out int n> (. int n1, n2; .)
- = CompareExpr<out n1> (. n = n1; .)
- {
- '&' CompareExpr<out n2> (. n = (n1 == 1 && n2 == 1) ? 1 : 0; .)
- |
- '|' CompareExpr<out n2> (. n = (n1 == 1 || n2 == 1) ? 1 : 0; .)
- }
- .
- CompareExpr<out int n> (. int n1, n2; .)
- = Expr<out n1> (. n = n1; .)
- [
- equals Expr<out n2> (. n = (n1 == n2) ? 1 : 0; .)
- |
- '<' Expr<out n2> (. n = (n1 < n2) ? 1 : 0; .)
- |
- '>' Expr<out n2> (. n = (n1 > n2) ? 1 : 0; .)
- ]
- .
- Expr<out int n> (. int n1, n2; .)
- = Term<out n1> (. n = n1; .)
- {
- '+' Term<out n2> (. n = n+n2; .)
- |
- '-' Term<out n2> (. n = n-n2; .)
- }
- .
- Term<out int n>
- = number (. n = Convert.ToInt32(t.val); .)
- {
- '*' number (. n = n*Convert.ToInt32(t.val); .)
- }
- .
- Expressions (. int n; .)
- /* = Expr<out n> (. res = n; .) */
- = BoolExpr<out n> (. res = n; .)
- .
- END Expressions.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement