The easiest way of doing these things is to first formulate the grammar as (E)BNF, including operator priorities, and then translating that to the format your favourite parser generator (yacc/bison, Boost.Spirit, antlr, ...) expects.

Operator priorities can be built into the grammar by correct grouping. For example, the grammar of basic algebra looks something like this:

Code:

start := add_expression
add_expression := mul_expression
| mul_expression '+' add_expression
| mul_expression '-' add_expression
mul_expression := term
| term '*' mul_expression
| term '/' mul_expression
term := '(' add_expression ')'
| number

This grammar builds a correctly prioritized expression tree (dot before dash, left to right) from any simple algebraic expression: in "1 + 2 * 3" the parse tree is this:

Code:

add_expression
-> mul_expression
-> term
-> number ("1")
-> '+'
-> add_expression
-> mul_expression
-> term
-> number ("2")
-> '*'
-> term
-> number ("3")

This leads to this expression tree:

Code:

'+'
-> 1
-> '*'
-> 2
-> 3

Because expression trees are evaluated depth-first, the multiplication is correctly applied before the addition.