Unary operators and the Shunting Yard algorithm 0 ▲ The Boston Diaries 1 hour ago · Tech · hide · 0 comments I use the Shunting Yard algorithm to handle precedence when parsing expressions in my assembler. It's great because not only is it simple to implement, but it simplifies the code in a hand-written recursive descent parser. The BNF is effectively: ; BNF per RFC-5234 expr = factor *(op factor) op = '*' ; just the basic ops for now / '/' ; adding more is just adding / '+' ; them to this definition / '-' factor = literal / var / '(' expr ')' literal = DIGIT+ var = (ALPHA / '_') (ALPHA / DIGIT / '_')* When expressing this BNF via a recursive descent parser, the function handling expr is where the Shunting Yard algorithm is used, providing precedence handling. In my implementation, the function handling op returns the precedence and associativity from a table: static struct optable const cops[] = { [OP_EXP] = { OP_EXP , AS_RIGHT , 1000 } , [OP_MUL] = { OP_MUL , AS_LEFT , 900 } , [OP_DIV] = { OP_DIV , AS_LEFT , 900 } , [OP_MOD] = { OP_MOD , AS_LEFT , 900 } , [OP_ADD] = { OP_ADD , AS_LEFT ,… No comments yet. Log in to reply on the Fediverse. Comments will appear here.