Converting syntax diagram to ebnf - ebnf

I am a bit confused atm.
Should the ebnf form for this syntax graph be:
Prog::= a|b
Or
Prog::= a[b]

Related

Syntax Directed Definition for differentiation of expressions

I've been reading Syntax Directed Definitions from ALSU- Compilers textbook and came across this exercise question. Actually this question asks us to write the productions also with corresponding Semantic rules.
Question from Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey, Ullman- Compilers
I'm pretty sure the productions for the given grammar would be -
E --> E + E
E --> E * E
E --> T
T --> id (which is x in this case)
T --> num
Are these productions correct? (If not plz help with that too)
Can anyone help me in figuring out what could be the semantic actions for corresponding productions
so translated output would differentiated value of input expression?

Encoding this logic sentence in Prolog: G ↔ ~Provable(x)

http://www.swi-prolog.org/pldoc/man?predicate=%5C%2B/1
How would the above expression be encoded in Prolog?
Here it is in English for all the people that know Prolog
by do not know predicate logic:
G has the same truth value as its own unprovability.
Would every translation of the above logic expression always fail an occurs check?
http://www.swi-prolog.org/pldoc/man?predicate=unify_with_occurs_check/2

Double negation in Reverse Polish Notation

I'm writing a boolean Reverse Polish Notation parser and evaluator. When I want to evaluate a double-negation, like !!A, the corresponding RPN is !A!, according to the shunting-yard algorithm. However, when I try to run the evaluation algorithm from Wikipedia it fails, as, when the first ! is found, there is no value to apply the operator to, as expected.
However, if I write the expression as !(!A), it translates to A!! in RPN, which is what I would need.
Is it a problem with conversion to RPN, or a evaluation one? I could always fix it by enforcing parentheses with each negation, but that doesn't seem like an elegant solution...

Is there a standard symbol for "is a"?

Simple question, is there an accepted standard symbol for an "is a" relationship? I know in math there are the ⊆ - subset, ⊂ - proper subset, ∈ - element of symbols, do I just use one of those or is there a more code specific one to use?
This came up while trying to respond to a statement written as sedan === car and I wondered what a better symbol to use for the === was.
As far as I understand, you are looking for something like intensional membership symbol. You don't want to use traditional set-theory "∈" due to its extensional nature.
Right, set-theory "∈" (as well as the set theory itself) is extensional due to these axioms:
∀P∃A∀X (X∈A ⇔ P(X)) — comprehension axiom (in a very naive form),
∀A∀B (∀X (X∈A ⇔ X∈B ) ⇔ A=B ) — axiom of extensionality.
In set theory jargon, one might write something like this:
my_sedan ∈ Cars, or
my_sedan ∈ {x : Car(x)}, or
Car(my_sedan).
There is not accepted standard symbol for "intensional membership". You can try:
my_sedan : Car — in a type theory manner;
my_sedan η Car — as used here or here;
my_sedan a Car — as in RDF 1.1 Turtle Syntax;
my_sedan cop. Car.

Algorithm for regular expression intersection with cfg

Im looking for an algorithm which outputs if the intersection of a regular expression and a contex free grammar is empty or not. I know that this problem is decidable, however, I cannot find any example implementation (in pseudocode).
Can someone provide me with such an algorithm, in .NET if possible but this is not a must. This problem is also called "regular intersection". Googling for it only gives me the geometrical algorithm or the theory about it.
edit:
Anybody. Im really stuck on it, and cannot find anything yet.
Here is a sketch of an approach that occurs to me. I think this should work but it is probably not the best way to do it since it uses the terribly messy conversion from PDA to CFG.
Convert the regular expression into a nondeterministic finite automaton (NFA) and reduce it down to the minimal determinsitic finite automaton (DFA). Convert the context free grammar (CFG) into a pushdown automoton (PDA). These first steps are all well known and fairly simple algorithms.
Take the intersection of the DFA and PDA, which is also a PDA. We will say the DFA has states S1, start state s1, final states F1, and transitions delta1 of the form ((source,trigger),destination), and the PDA has states S2, start state s2, final states F2, and transitons delta2 of the form ((source,trigger,pop),(destination,push)). The new PDA has states S1 X S2, each state labeled by a pair. It has final states F1 X F2, and start state (s1,s2). Now for the transitions.
For each transition d an element of delta2, for each state s an element s1, find the transition t an element of delta1 of the form ((s,d.trigger),?). Make a new transition (((d.source, s), d.trigger, d.pop),((d.destination, t.destination),d.push)).
This new PDA should accept the intersection of the languages produced by the RE and the CFG. To test if the language is empty you will need to convert it back to a CFG. The algorithm for that is messy and large, but it works. Once you have done that, mark each terminal symbol. Then mark each symbol which has a rule where there are only marked symbols on the right hand side, and repeat until you can mark no more symbols. If you can mark the start symbol, the language is not empty. Otherwise, the language is empty.
In fact, there is a simpler algorithm for computing the intersection between a context-free grammar and a regular expression. It does not use push-down automata, which can be costly to obtain from CFG with several conversions.
This solution was presented in:
Y. Ba-Hillel, M. Prles, and E. Shamir. 1965. On formal properties of
simple phrase structure grammars. Z. Phonetik, Sprachwissen. Komm. 15
(I961), 143-172. Y. Bar-Hillel, Language and Information,
Addison-Wesley, Reading, Mass (1965), 116–150.
but you can find a simpler version in:
Richard Beigel and William Gasarch. .. A Proof that the intersection
of a context-free language and a regular language is Context-Free
Which Does Not use pushdown automata.
http://www.cs.umd.edu/~gasarch/BLOGPAPERS/ cfg.pdf (.).
If it help, this solution was implemented in Pyformlang (https://pyformlang.readthedocs.io/), and you can find it on Github for Python (https://github.com/Aunsiels/pyformlang/blob/master/pyformlang/cfg/cfg.py)

Resources