=================================================================== CPW Part 16 (A): Equality and Relational Expressions in the Grammar [TOC] =================================================================== The new operators "``==``", "``!=``", "``>``", "``>=``", "``<``" and "``<=``" should habe the same meaning, associativity and precedence as in C (See __Session 8, Page 4__). Below the corresponding grammar. Note that some of the production rules were simply renamed (e.g. _expr_ into _additive-expr_ and _term_ into _multiplicative-expr_). But essentially just two new production rules, i.e. _equality-expr_ and _relational-expr_, were added. :links: Session 8, Page 4 -> doc:session08/page04#toc3 ---- LATEX --------------------------------------------------------------------- \begin{array}{rcl} \text{input-sequence} & = & \{\; $> \; \text{assignment-expr}\; \texttt{;}\; |\; $< \; \text{assignment-expr}\; \texttt{;}\; |\; \text{expr-statement}\; \}\; \\ \text{expr-statement} & = & \text{assignment-expr}\; \texttt{;}\; \\ \text{assignment-expr} & = & \text{equality-expr}\; [\; \texttt{=}\; \text{assignment-expr}\; ]\; \\ \text{equality-expr} & = & \text{relational-expr}\; \{\; (\; \texttt{"=="}\; |\; \texttt{"!="}\; )\; \text{relational-expr} \} \\ \text{relational-expr} & = & \text{additive-expr}\; \{\; (\; \texttt{">"}\; |\; \texttt{"<"}\; |\; \texttt{">="}\; |\; \texttt{"<="}\; )\; \text{additive-expr} \} \\ \text{additive-expr} & = & \text{multiplicative-expr}\; \{\; (\; \texttt{"+"}\; |\; \texttt{"-"}\; )\; \text{multiplicative-expr} \} \\ \text{multiplicative-expr} & = & \text{unary-expr}\; \{\; (\; \texttt{"*"}\; |\; \texttt{"/"}\; |\; \texttt{"%"} )\; \text{unary-expr} \} \\ \text{unary-expr} & = & \text{primary-expr}\; |\; (\; \texttt{"+"}\; |\; \texttt{"-"}\;)\; \text{unary-expr} \\ \text{primary-expr} & = & \text{identifier}\; \\ & | & \text{dec-literal}\; \\ & | & \text{hex-literal}\; \\ & | & \text{oct-literal}\; \\ & | & \texttt{"("}\; \text{assignment-expr}\; \texttt{")"}\; \\ \end{array} -------------------------------------------------------------------------------- Exercise ======== In _enum ExprKind_ add constants for the new binary operators: ---- CODE (type=c) ------------------------------------------------------------- enum ExprKind { EK_BINARY, // binary expression EK_ADD = EK_BINARY, EK_ASSIGN, EK_EQUAL, EK_NOT_EQUAL, EK_GREATER, EK_GREATER_EQUAL, EK_LESS, EK_LESS_EQUAL, EK_SUB, EK_MUL, EK_DIV, EK_MOD, EK_BINARY_END, // ... }; -------------------------------------------------------------------------------- Update the parser for the new grammar: - Rename parse functions to reflect the naming used in the grammar. E.g rename _parseExpr()_ into _parseAdditiveExpr()_, etc. This is annoying but when a project grows such modifications are part of the game. - Add parse the functions _parseEqualityExpr()_ and _parseRelationalExpr()_ for the corresponding production rules. What Should Work Now ~~~~~~~~~~~~~~~~~~~~ After this update make sure that you can build ``xtest_abc``: ---- SHELL (path=session24/git1/abc, hide) ------------------------------------- make clean -------------------------------------------------------------------------------- ---- SHELL (path=session24/git1/abc,fold) -------------------------------------- make -------------------------------------------------------------------------------- Test the compiler with expressions like in this example (of course you can extend this): ---- CODE (file=session24/git1/abc/test.abc) ----------------------------------- x == y; x != y; x <= y; x >= y; x < y; x > y; -------------------------------------------------------------------------------- If you also want to update const folding use ---- CODE (file=session24/git1/abc/test_cf.abc) -------------------------------- x == y; x != y; x <= y; x >= y; x < y; x > y; 3 == 4; 3 != 4; 3 <= 4; 3 >= 4; 3 < 4; 3 > 4; -------------------------------------------------------------------------------- Expected Error ~~~~~~~~~~~~~~ You will get an assertion failure from the code generator (i.e. in ``loadExpr()``) but you should not get a syntax error: ---- SHELL (path=session24/git1/abc) ------------------------------------------- ./xtest_abc < test.abc -------------------------------------------------------------------------------- So in the next step you should - adapt the implementation of _loadExpr()_. - handle in ``printExprNode()`` the new expression kinds. - handle in ``constFoldBinary()`` the new expression kinds (optional).