It's important to consider where to place action symbols when constructing a grammar as they perform a key role in the language, providing clues to how and when a parser should take action.
The general best practices for placing action symbols in a grammar are:
Consider the parser’s point of view. Since the parser is responsible for recognizing text and responding to it, it’s important to consider how placing action symbols might help the parser do this efficiently. Depending on the type of language, this might require action symbols to be placed before or after a token (e.g. for an XML parser, the action symbol "start" would need to be placed before an XML tag like <book>).
Consider the grammar’s structure. By keeping action symbols in consistent locations throughout the grammar, it becomes easier for a parser to recognize the various structures with greater clarity. Parsers should be able to recognize patterns such as when "start" is associated with a symbol like <book>, or when "end" is associated with a symbol like </book>.
Make use of indentation. To further help parsers recognize when action symbols should be taken, it’s helpful to take advantage of indentation. For example, an XML parser should be able to recognize when "start" is associated with an indented line like:
<book>
This way, the parser knows to place the corresponding "end" on the next line that is de-indented:
</book>
These are the general best practices for placing action symbols in a grammar. By keeping in mind the parser’s point of view and taking advantage of grammar structure and indentation, you can help ensure that your parsing process is as efficient as possible.