Created
December 24, 2013 18:04
-
-
Save Chase-san/8116359 to your computer and use it in GitHub Desktop.
This is my grammar for my Expression parser. Made for use with Coco/R.
This file contains 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
package org.csdgn.expr.coco; | |
import java.math.BigDecimal; | |
COMPILER CompilationUnit | |
public Expr tree; | |
CHARACTERS | |
digit | |
= "0123456789" | |
. | |
letter | |
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrtsuvwxyz" | |
. | |
TOKENS | |
number | |
= digit { digit } | |
| { digit } "." digit { digit } | |
. | |
ident | |
= letter { digit | letter } | |
. | |
PRODUCTIONS | |
CompilationUnit (. Expr e; .) | |
= Expr <out e> (. tree = e; .) | |
. | |
Expr <out Expr e> (. e = null; .) | |
= IF(isAssignment()) ident (. String ident = t.val; .) | |
"=" CompareExpr <out e> (. e = new Expr.Assign(ident, e); .) | |
| CompareExpr <out e> | |
. | |
PrimaryExpr <out Expr e> (. e = null; .) | |
= "(" Expr <out e> ")" | |
| Literal <out e> | |
. | |
Literal <out Expr e> (. e = null; .) | |
= number (. e = new Expr.Number(new BigDecimal(t.val)); .) | |
| ident (. e = new Expr.Ident(t.val); .) | |
. | |
UnaryExpr <out Expr e> (. e = null; Operator op; .) | |
= UnaryOp <out op> | |
PrimaryExpr <out e> (.e = new Expr.Unary(op, e); .) | |
| PrimaryExpr <out e> | |
. | |
PowerExpr <out Expr e> (. Operator op; Expr e2; .) | |
= UnaryExpr <out e> | |
{ | |
PowerOp <out op> | |
UnaryExpr <out e2> (. e = new Expr.Binary(e, op, e2); .) | |
} | |
. | |
/* TODO support (a)(b) and 2x multiplcation */ | |
ProductExpr <out Expr e> (. Operator op; Expr e2; .) | |
= PowerExpr <out e> | |
{ | |
ProductOp <out op> | |
PowerExpr <out e2> (. e = new Expr.Binary(e, op, e2); .) | |
} | |
. | |
SumExpr <out Expr e> (. Operator op; Expr e2; .) | |
= ProductExpr <out e> | |
{ | |
SumOp <out op> | |
ProductExpr <out e2> (. e = new Expr.Binary(e, op, e2); .) | |
} | |
. | |
CompareExpr <out Expr or> (. Operator op; Expr e2; .) | |
= SumExpr <out e> | |
{ | |
CompareOp <out op> | |
SumExpr <out e2> (. e = new Expr.Binary(e, op, e2); .) | |
} | |
. | |
/* OPERATORS */ | |
UnaryOp <out Operator op> (. op = null; .) | |
= "-" (. op = Operator.NEG; .) | |
. | |
PowerOp <out Operator op> (. op = null; .) | |
= "**" (. op = Operator.POW; .) | |
. | |
ProductOp <out Operator op> (. op = null; .) | |
= "*" (. op = Operator.MUL; .) | |
| "/" (. op = Operator.DIV; .) | |
| "%" (. op = Operator.MOD; .) | |
| "<<" (. op = Operator.LSHF; .) | |
| ">>" (. op = Operator.RSHF; .) | |
| "&" (. op = Operator.AND; .) | |
. | |
SumOp <out Operator op> (. op = null; .) | |
= "+" (. op = Operator.ADD; .) | |
| "-" (. op = Operator.SUB; .) | |
| "|" (. op = Operator.OR; .) | |
| "^" (. op = Operator.XOR; .) | |
. | |
/* Unused (for the moment) */ | |
CompareOp <out Operator op> (. op = null; .) | |
= "==" (. op = Operator.EQ; .) | |
| "!=" (. op = Operator.NE; .) | |
| "<" (. op = Operator.LT; .) | |
| "<=" (. op = Operator.LE; .) | |
| ">" (. op = Operator.GT; .) | |
| ">=" (. op = Operator.GE; .) | |
. | |
END CompilationUnit. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment