Grammar Design Patterns

Skip to end of metadata
Go to start of metadata

Implementing precedence rules

It is often useful to create imaginary tokens for the branch nodes.
This requires a more involved syntax to achieve the same result.

Detecting statement terminator

It is common for statements to be optionally terminated by a new line or a semicolon.
(Python, VisualBasic, Bash, etc.)

String containing escaped characters

This is a case of an 'island-grammar'.
At some point the content of the string will need to be transformed.
For example "\u20\u6e\u69\u63\u6f\u64\u65" would be transformed into "Unicode".
When should this transformation be performed?

By the lexer?

It is generally a bad practice to modify the input stream.

An alternative approach that may be useful.

By the parser?

By the renderer?

Where 'bar' is the escaped string.
In this case the renderer itself could be a lexer/parser for the regular language.

Processing regular expressions

Many languages provide support for regular expressions.
This is a case of an 'island-grammar'.

Labels: