parsing - Canonical mutual exclusive data structure -


note: unrelated concurrency problem of mutual exclusion, couldn't think of better way of describing problem.

i have problem have case want let user select flags, flags mutually exclusive. want describe flags mutually exclusive using data structure, i've thought of has been clunky.

basically, want able specify how flags used so:

[ -fa | -e | -d ] [ -c ] [ -g | -h ]

this should semantically mean, can have 1 of -fa, -e, -d, not 2 or more (however, f can used a, , don't need use both). can either have -c or not, , can have either -g or -h, not both.

here's "best" solution.

map[flag, mutexgroup] (and inverse, map[mutexgroup, list[flag]]) map[mutexgroup, list[mutexgroup]]

what example be

map("f" -> 1, "a" -> 1, "e" -> 2, "d" -> 3, "c" -> 4, "g" -> 5, "h" -> 6) map(1 -> list(2, 3), 2 -> list(1, 3), 3 -> list(1, 2), 4 -> list.empty, 5 -> list(6), 6 -> list(5))

i haven't included map[mutexgroup, list[flag]] brevity.

this solution makes me shudder thinking having work it. there canonical way dealing kind of thing?

you're describing grammar.

the best thing representing grammars abstract syntax tree. tree being canonical structure represents (mututally exclusive) choice.

you can represent syntax trees in many ways, 1 nice approach use algebraic data types, statically guarantee well-formed expressions can constructed. flags form set, using set data type enforce no-duplicates property good.

-- can have 1 of -fa, -e, -d, not 2 or more -- (however, f can used a, , don't need use both). -- can either have -c or not, , can have either -g or -h, not both. --  -- 0 or more flags type flags = set flag  -- flags come in 3 groups data flag         = f1 faed         | f2 c         | f3 gh     -- equality first constructor.   -- 1 of: -f or -a; -e; -d data faed         = fa fa          | e         | d  -- type for: -f ; -a ; -f -a data fa = f         |         | fa  -- -c flag data c  = c  -- either -g or -h data gh = g         | h 

there other ways encode language well, enough start down path of representing language using syntax tree.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -