0
$\begingroup$

I wanted to define syntax for a graph path considering it may branch out anywhere.

For instance, I have a path going from a to e following b, c, d: a, b, c, d, e.

Let's consider it (may) branches from b now: a, b, (x | y), d, e which actually means three different paths:

1) a, b, d, e

2) a, b, x, d, e

3) a, b, y, d, e

So, each parenthesis actually means "the path might either go through any of the nodes defined in the parenthesis connecting the node just before parenthesis and right after parenthesis or through none of the nodes defined in the parenthesis".

With more inner parenthesis, a single string line may generate even more paths. For instance: a, b, (x | (z | t) | y), d, e which means these five paths:

1) a, b, d, e

2) a, b, x, d, e

3) a, b, y, d, e

4) a, b, z, d, e

5) a, b, t, d, e

6) a, b, z, y, d, e

7) a, b, t, y, d, e

8) a, b, x, z, d, e

9) a, b, x, t, d, e

So, how can I define this syntax mathematically?

$\endgroup$
7
  • 1
    $\begingroup$ A path in what? Are you in a graph, a tree, a list? Your context is not clear. $\endgroup$ Commented Aug 16, 2013 at 17:18
  • $\begingroup$ Can you explain why the second example doesn't also generate a, b, z, d, e, a, b, t, d, e, a, b, z, y, d, e, or a, b, t, y, d, e? $\endgroup$
    – Patrick87
    Commented Aug 16, 2013 at 18:00
  • $\begingroup$ Sorry about the missing items. I updated the explanation and examples. Appreciate your attention ;) $\endgroup$
    – israkir
    Commented Aug 16, 2013 at 20:55
  • 1
    $\begingroup$ Are you familiar with the concept of formal grammars? $\endgroup$
    – Raphael
    Commented Aug 17, 2013 at 12:14
  • 2
    $\begingroup$ A regular grammar will not be sufficient. If you don't see that, I don't think you are familiar with formal grammars/languages (no offense). You should read up on the basics before you try to define a syntax for a set of things as wicked as this. $\endgroup$
    – Raphael
    Commented Aug 17, 2013 at 13:13

1 Answer 1

4
$\begingroup$

You are mixing two things here: syntax and semantics.

First, syntax. What is the language of strings $L_P \subseteq \Sigma^*$ with $\Sigma = \{\mathtt{a}, \dots, \mathtt{z}, \mathtt{)}, \mathtt{(}, \mathtt{,}\}$ that describe a set of paths as you specify them? You can specify $L_P$ by the formal grammar

$\qquad\begin{align} S &\to T\mathtt{,} S \mid \mathtt{(}S\mathtt{)}S \mid T \\ T &\to [\mathtt{a}\dots\mathtt{z}] \end{align}$

Note that the key feature is recursion for the parts inside parentheses. Depending on the parsing algorithm you want to employ, you may have to refactor this grammar.

Next, which set of paths does a given string $p \in L_p$ describe? Note that this is independent of the syntax; we could define anything we want as the meaning of $p$. Your specification is quite precise, and can be described by these by recursive functions:

$\qquad\begin{align} \varphi(t) &= \{t\} \quad, t \in [\mathtt{a}\dots\mathtt{z}], \\ \varphi(t\mathtt{,}w) &= \varphi(t) \cdot \varphi(w) \quad, t \in [\mathtt{a}\dots\mathtt{z}], w \in \Sigma^*, \\ \varphi((v)w) &= (\psi(v), \{\varepsilon\}) \cdot \varphi(w) \quad, v, w \in \Sigma^* \\ \end{align}$

and

$\qquad\begin{align} &\psi(t) = \{t\} \quad, \mathtt{t} \in [\mathtt{a}\dots\mathtt{z}], \\ &\psi(t\mathtt{,}w) = \psi(t) \cup \psi(w) \quad, t \in [\mathtt{a}\dots\mathtt{z}], w \in [\mathtt{a}\dots\mathtt{z},\mathtt{,}]^*, \\ &\psi(v_0(w_1)v_1(w_2) \dots v_{k-1}(w_k)v_k) = \\ &\phantom{=}\bigcup_{i \in [0..k]} (\psi(w_1), \{\varepsilon\}) \cdot \dots \cdot (\psi(w_{i}), \{\varepsilon\}) \cdot \psi(v_i) \cdot (\psi(w_{i+1}), \{\varepsilon\}) \cdot \dots \cdot (\psi(w_k), \{\varepsilon\}) \quad, v_i \in [\mathtt{a}\dots\mathtt{z},\mathtt{,}]^*, w_i \in L_P. \\ \end{align}$

Note how $\varphi(p) \subseteq [\mathtt{a}\dots\mathtt{z}]^*$ is the set of paths described by $p$.

Warning: Your syntax has the troublesome feature that a comma-separated list of path elements has different meaning outside and inside of parenteses (resulting in two functions for the semantics). In combination with your semantics, that means e.g. that you can not describe the set of paths $\{ad, abcd\}$.

Warning: Your semantics have the troublesome feature that any list of atoms in parentheses may produce at most one atom (selection), but if one element of the list is another list we suddenly get more (selection + concatenation). In other words, the semantic of $\mathtt{,}$ inside of parentheses changes depending on context. This is what makes the semantics so horrible. But then, maybe that is how it has to be: you seem to want it that way.

$\endgroup$
7
  • $\begingroup$ For the warnings, I agree! Yes, I think this was the part made confused a lot. So, I'd better to change comma notation inside the parenthesis with bar "|" notation (which probably fits better for the meaning also). $\endgroup$
    – israkir
    Commented Aug 17, 2013 at 13:28
  • $\begingroup$ BTW, I edited the question accordingly. $\endgroup$
    – israkir
    Commented Aug 17, 2013 at 13:31
  • $\begingroup$ @israkir Switching to bars does away with the first warning, not the with the second. $\endgroup$
    – Raphael
    Commented Aug 18, 2013 at 10:51
  • $\begingroup$ I think, the context of inner parenthesis is that "the nodes in parenthesis always define an alternative path adding possible node to the path and concatenating rest according to the parenthesis". So, I couldn't see why bar notation causes problem for the second warning?! $\endgroup$
    – israkir
    Commented Aug 18, 2013 at 15:38
  • $\begingroup$ @israkir Because (a | b ) means "either a, or b, or nothing" but (a | (b)) does mean something entirely else. Particularly troublesome is that in situations like (a | (x) | b) you may choose only one of a and b, but if x is there a has to be before and b after. $\endgroup$
    – Raphael
    Commented Aug 18, 2013 at 16:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.