\name{grammatical_structure}
\alias{grammatical_structure}
\alias{print.grammatical_structure}
\title{Grammatical Structure of a Sumerian Expression}
\description{
Determines and visualizes the grammatical structure of a Sumerian expression. The function groups sub-expressions according to operator binding and composition rules and returns a bracketed string in which each bracket type indicates the grammatical role of the group:

\itemize{
  \item \code{()} -- substantive (S)
  \item \code{<>} -- verb (V)
  \item \code{[]} -- attribute (A)
  \item \code{\{\}} -- sentence (SEN)
}

The result has class \code{"grammatical_structure"} and comes with a \code{print} method that displays the bracket tree with color-coded groups in the console (requires ANSI color support).
}
\usage{
grammatical_structure(s, type, expr = NULL)

\method{print}{grammatical_structure}(x, \dots)
}
\arguments{
  \item{s}{Character string. A Sumerian expression in cuneiform characters.}
  \item{type}{Character vector of grammatical types, one per sub-expression. Each entry is either a base type (\code{"S"}, \code{"V"}, \code{"A"}) or an operator type (e.g.  \code{"Sx->V"}, \code{"xS->A"}).}
  \item{expr}{Character vector of sub-expressions (e.g. the individual signs or sign groups that make up \code{s}). The function matches each \code{expr[k]} in \code{s}, determines the grouping, and returns the result with the original expressions in place.}
  \item{x}{An object of class \code{"grammatical_structure"}.}
  \item{\dots}{Further arguments (currently unused).}
}
\details{
The grouping is performed in two stages. First, \code{\link{add_brackets}} inserts bracket groups based on operator binding strength and pairwise composition rules. Then each group is assigned a bracket type that reflects its grammatical role, as determined by the operator it contains or by the types of its elements.

The \code{print} method displays the resulting string with ANSI colors in the console. Each bracket type and its direct content (nesting level 0) are shown in a distinct color: green for \code{()}, blue for \code{[]}, red for \code{<>}, and yellowish-brown for \code{\{\}}. Bracket pairs that contain only nested sub-groups (no bare symbols at nesting level 0) are shown in light gray.
}
\value{
A character string of class \code{"grammatical_structure"} with typed brackets showing the grammatical grouping. On error, a plain character string containing the error message.
}
\examples{
# Example 1
x    <- "mec3-ki-aj2-ga-ce-er ce du"
expr <- split_sumerian(x)$signs
type <- c("S", "S", "Sx->A", "xS->A",  "S",  "Sx->S", "S", "Sx->V")

grammatical_structure(x, type, expr)

grammatical_structure(as.cuneiform(x), type, as.cuneiform(expr))

# Example 2: An example with a proper name and verb prefixes
x    <- "an-en-ki-en-gan-ig-la"
expr <- c("an-en-ki", "en", "gan", "ig", "la")
type <- c("S", "S", "xV->V", "xV->V", "Vt")
grammatical_structure(as.cuneiform(x), type, as.cuneiform(expr))

}
\seealso{
\code{\link{apply_translation_rules}} for translating (instead of visualizing) the structure,
\code{\link{add_brackets}} for the underlying grouping algorithm,
\code{\link{split_sumerian}} for obtaining the sub-expressions from a Sumerian string
}
