Identifiers with subscripts
Note: this exercise is taken from the Compilers
subject at Barcelona School of Informatics (and is based on a similar one from Compilers: Principles, Techniques, and Tools
Design a grammar to describe identifiers with subscripts. The set of tokens is
}. For instance, all the
following are valid inputs:
- The token IDENT recognizes identifiers (i.e., non-empty sequences
of alphabetic characters), and the token NUMBER recognizes unsigned
numbers (i.e., non-empty sequences of digits).
- Subscripts can be identifiers or unsigned numbers. Subscripts that are
identifiers can have subscripts of their own, whereas those that are numbers
cannot. Therefore, Vector[1[i]] is not valid.
- The AST must have the main identifier as root, with one child per
subscript. An identifier subscript must have an analogous subtree.