summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaweł Dybiec <pdybiec@stud.cs.uni.wroc.pl>2018-11-04 22:13:54 +0100
committerPaweł Dybiec <pdybiec@stud.cs.uni.wroc.pl>2018-11-04 22:13:54 +0100
commitfc004d69245b3d7af7326298d2bdbdb259127a68 (patch)
treeb9dc67f03f3e7c294ff749109c902455669b72cf
parentParse basic expressions and statements (diff)
Group operators of same precedence in productions
-rw-r--r--source/mod_student/parser.mly68
1 files changed, 31 insertions, 37 deletions
diff --git a/source/mod_student/parser.mly b/source/mod_student/parser.mly
index 7a6c303..76d7fe6 100644
--- a/source/mod_student/parser.mly
+++ b/source/mod_student/parser.mly
@@ -97,6 +97,24 @@ typ:
base_type:
| T_INT {TEXPR_Int {loc=mkLocation $startpos} }
| T_BOOL {TEXPR_Bool {loc=mkLocation $startpos} }
+op_unary:
+ | OP_MINUS { UNOP_Neg }
+ | OP_NOT { UNOP_Not }
+op_mult_prec:
+ | OP_MULT {BINOP_Mult}
+ | OP_DIV {BINOP_Div}
+ | OP_REM {BINOP_Rem}
+op_add_prec:
+ | OP_PLUS {BINOP_Add}
+ | OP_MINUS {BINOP_Sub}
+op_cmp_prec:
+ | OP_LE {RELOP_Le}
+ | OP_LT {RELOP_Lt}
+ | OP_GE {RELOP_Ge}
+ | OP_GT {RELOP_Gt}
+op_eq_prec:
+ | OP_EQ {RELOP_Eq}
+ | OP_NEQ {RELOP_Ne}
expression:
| node=expression_or {node}
expression_or:
@@ -110,55 +128,31 @@ expression_and:
{EXPR_Binop {tag=mkTag ();loc=mkLocation $startpos;
op=BINOP_And;lhs=left;rhs=right } }
expression_eq:
- | node=expression_rel {node}
- | left=expression_eq OP_EQ right=expression_rel
+ | node=expression_cmp {node}
+ | left=expression_eq op=op_eq_prec right=expression_cmp
{EXPR_Relation {tag=mkTag ();loc=mkLocation $startpos;
op=RELOP_Eq;lhs=left;rhs=right } }
- | left=expression_eq OP_NEQ right=expression_rel
- {EXPR_Relation {tag=mkTag ();loc=mkLocation $startpos;
- op=RELOP_Ne;lhs=left;rhs=right } }
-expression_rel:
+expression_cmp:
| node=expression_add {node}
- | left=expression_rel OP_LE right=expression_add
- {EXPR_Relation {tag=mkTag ();loc=mkLocation $startpos;
- op=RELOP_Le;lhs=left;rhs=right } }
- | left=expression_rel OP_GE right=expression_add
- {EXPR_Relation {tag=mkTag ();loc=mkLocation $startpos;
- op=RELOP_Ge;lhs=left;rhs=right } }
- | left=expression_rel OP_LT right=expression_add
- {EXPR_Relation {tag=mkTag ();loc=mkLocation $startpos;
- op=RELOP_Lt;lhs=left;rhs=right } }
- | left=expression_rel OP_GT right=expression_add
+ | left=expression_cmp op=op_cmp_prec right=expression_add
{EXPR_Relation {tag=mkTag ();loc=mkLocation $startpos;
- op=RELOP_Gt;lhs=left;rhs=right } }
+ op=op;lhs=left;rhs=right } }
expression_add:
| node=expression_mult { node }
- | left=expression_add OP_PLUS right=expression_mult
+ | left=expression_add op=op_add_prec right=expression_mult
{EXPR_Binop {tag=mkTag ();loc=mkLocation $startpos;
- op=BINOP_Add;lhs=left;rhs=right } }
- | left=expression_add OP_MINUS right=expression_mult
- {EXPR_Binop {tag=mkTag ();loc=mkLocation $startpos;
- op=BINOP_Sub;lhs=left;rhs=right } }
+ op=op;lhs=left;rhs=right } }
expression_mult:
| node=expression_unary { node }
- | left=expression_mult OP_MULT right=expression_unary
- {EXPR_Binop {tag=mkTag ();loc=mkLocation $startpos;
- op=BINOP_Mult;lhs=left;rhs=right } }
- | left=expression_mult OP_DIV right=expression_unary
+ | left=expression_mult op=op_mult_prec right=expression_unary
{EXPR_Binop {tag=mkTag ();loc=mkLocation $startpos;
- op=BINOP_Div;lhs=left;rhs=right } }
- | left=expression_mult OP_REM right=expression_unary
- {EXPR_Binop {tag=mkTag ();loc=mkLocation $startpos;
- op=BINOP_Rem;lhs=left;rhs=right } }
+ op=op;lhs=left;rhs=right } }
expression_unary:
- | node=expression_highest { node }
- | OP_MINUS right=expression_unary
- {EXPR_Unop {tag=mkTag ();loc=mkLocation $startpos;
- op=UNOP_Neg;sub=right } }
- | OP_NOT right=expression_unary
+ | node=expression_highest_prec { node }
+ | op=op_unary right=expression_unary
{EXPR_Unop {tag=mkTag ();loc=mkLocation $startpos;
- op=UNOP_Not;sub=right } }
-expression_highest:
+ op=op;sub=right } }
+expression_highest_prec:
| node=expression_integer { node }
| node=expression_identifier { node }
(* TODO Add calls and arrays and other constants *)