There are situations in which one needs to write various kinds of mathematical expressions, such as practicing tests and school exams. There is a variety of methods to produce such expressions, but they are usually based on a database. This paper addresses the production of new expressions using the template ones that can be derived from the evaluation process or entered by users. With special limitations on the values of parameters, some templates can be dynamically constructed for the automatic generation of mathematical expressions and represented in the form of classes. For this purpose, a new type of grammar is proposed. This grammar is similar to Context-Free Grammar, but it empowers the producer to gain control over the generation of rules for different expressions. Our work mainly focuses on generating mathematical expressions in a user-oriented way, using a predefined set of templates of production rules. The production of expressions is not completely random, and is based on the defined subject.