What would the conversion of this from EBNF to BNF be? Also, what is the leftmost derivation? - bnf

I need to convert this from EBNF to BNF.
<statement> ::= <ident> = <expr>
<statement> ::= IF <expr> THEN <statement> [ ELSE <statement> ] END
<statement> ::= WHILE <expr> DO <statement> END
<statement> ::= BEGIN <statement> {; <statement>} END
Also, I'm stuck on this one:
E -> E+T | E-T | T
T -> T*F | T/F | F
F -> (E) | VAR | INT
VAR -> a | b | c
INT -> 0 | 1 | 2| 3 | 4| 5 | 6 | 7 | 8 | 9
After modifying the grammer to add a ^ operator, What is the leftmost derivation that your grammar assigns to the expression a^2^b*(c+1)? You may find it convenient to sketch the parse tree for this expression first, and then figure out the leftmost derivation from that.
I added G -> F^G | G and then got G 2 G b E as my answer but am not sure if that is correct.

Related

Recursive macro makes infinite recursion

I made a simple macro that returns the taken parameter.
macro_rules! n {
($n:expr) => {{
let val: usize = $n;
match val {
0 => 0,
_ => n!(val - 1),
}
}};
}
When I compile this code with the option external-macro-backtrace, it raises an error:
error: recursion limit reached while expanding the macro `n`
--> src/main.rs:15:18
|
10 | macro_rules! n {
| _-
| |_|
| |
11 | | ($n:expr) => {{
12 | | let val: usize = $n;
13 | | match val {
14 | | 0 => 0,
15 | | _ => n!(val - 1),
| | ^^^^^^^^^^^
| | |
| | in this macro invocation
16 | | }
17 | | }};
18 | | }
| | -
| |_|
| |_in this expansion of `n!`
| in this expansion of `n!`
...
31 | | n!(1);
| | ------ in this macro invocation
|
= help: consider adding a `#![recursion_limit="128"]` attribute to your crate
I changed the recursion_limit to 128 and higher, but the compiler error message just increase as well. Even when I call n!(0) it makes the same error. I think it is infinite recursion, but I can't find the reason.
Well, it really is an infinite recursion. Check what your macro invocation n!(0) will be expanded into:
{
let val: usize = 0;
match val {
0 => 0,
_ => n!(0 - 1),
}
}
...and since there's no way for argument of n! to stop growing negative, it'll repeat (with n!(0 - 1 - 1) in the second match arm, then n!(0 - 1 - 1 - 1) etc.) infinitely.
The key point here is that the macro expansion happens in compile-time, while the match statement you're trying to use to limit the recursion is invoked only at run-time and can't stop anything from appear before that. Unhappily, there's no easy way to do this, since Rust won't evaluate macro arguments (even if it's a constant expression), and so just adding the (0) => {0} branch to the macro won't work, since the macro will be invoked as (for example) n!(1 - 1).

SPARQL count unique value combinations

I have been working on a SPARQL query to find unique value combinations in my graph store. But I dont succeed.
Basically what I try to do is:
a b c
e f g
e r t
a b c
k l m
e f g
a b c
result:
a b c | 3
e f g | 2
e r t | 1
k l m | 1
Tried several constructions, with distincts, group by`s and sub queries but I dont succeed.
Last Try:
SELECT (count (*) as ?n){
SELECT DISTINCT ?value1 ?value2 ?value3 WHERE {
?instance vocab:relate ?value1 .
?instance vocab:relate ?value2 .
?instance vocab:relate ?value3 .
}
}
RDF:
<http://test.example.com/instance1>
a <http://test.example.com#Instance> ;
<http://vocab.example.com/relate>
<http://test.example.com/c> , <http://test.example.com/b> , <http://test.example.com/a> .
<http://test.example.com/instance6>
a <http://test.example.com#Instance> ;
<http://vocab.example.com/relate>
<http://test.example.com/g> , <http://test.example.com/f> , <http://test.example.com/e> .
<http://test.example.com/instance4>
a <http://test.example.com#Instance> ;
<http://vocab.example.com/relate>
<http://test.example.com/c> , <http://test.example.com/b> , <http://test.example.com/a> .
<http://test.example.com/instance2>
a <http://test.example.com#Instance> ;
<http://vocab.example.com/relate>
<http://test.example.com/g> , <http://test.example.com/f> , <http://test.example.com/e> .
<http://test.example.com/instance7>
a <http://test.example.com#Instance> ;
<http://vocab.example.com/relate>
<http://test.example.com/c> , <http://test.example.com/b> , <http://test.example.com/a> .
<http://test.example.com/instance5>
a <http://test.example.com#Instance> ;
<http://vocab.example.com/relate>
<http://test.example.com/m> , <http://test.example.com/l> , <http://test.example.com/k> .
<http://test.example.com/instance3>
a <http://test.example.com#Instance> ;
<http://vocab.example.com/relate>
<http://test.example.com/t> , <http://test.example.com/r> , <http://test.example.com/e> .
AKSW's comment is spot on: you need to add an ordering criteria to the values so that you're not considering all the different possible ways of ordering the values. Also, remember that RDF doesn't have "duplicate" triples, so
:a :p :c, :c, :d
is the same as
:a :p :c, :d
so the appropriate comparison is < as opposed to <=, since without duplicate triples, you'd never have an = case. Also, since the values are IRIs, you need to get their string values before you can compare with <, but the str function will take care of that.
prefix v: <http://vocab.example.com/>
prefix : <http://test.example.com/>
select ?a ?b ?c (count(distinct ?i) as ?count) where {
?i v:relate ?a, ?b, ?c .
filter (str(?a) < str(?b) && str(?b) < str(?c))
}
group by ?a ?b ?c
------------------------
| a | b | c | count |
========================
| :a | :b | :c | 3 |
| :e | :f | :g | 2 |
| :e | :r | :t | 1 |
| :k | :l | :m | 1 |
------------------------

Erlang Towers of Hanoi

Currently stuck on trying to implement the towers of hanoi using a collection. I am trying to follow the example in Java using stacks http://www.sanfoundry.com/java-program-implement-solve-tower-of-hanoi-using-stacks/, but I am getting
-module(toh).
-export([begin/0]).
begin() ->
Pegs = 3,
TowerA = createTowerA(N),
TowerB = [],
TowerC = [],
move(Pegs, TowerA, TowerB, TowerC).
%fills Tower A with integers from Pegs to 1.
createTowerA(0) -> [];
createTowerA(N) when N > 0 ->
[N] ++ createTowerA(N - 1).
%displays the towers
display(A, B, C) ->
io:format("~w\t~w\t~w~n", [A, B, C]).
move(Pegs, TowerA, TowerB, TowerC) ->
if Pegs > 0 ->
move(Pegs, TowerA, TowerC, TowerB),
Temp = lists:last(TowerA),
NewTowerC = C ++ Temp,
NewTowerA = lists:sublist(TowerA, length(TowerA) - 1),
display(NewTowerA, B, NewTowerC),
move(Pegs - 1, B, NewTowerA, NewTowerC);
end
When I try running the code, I get this error.
{"init terminating in do_boot",{undef,[{toh,begin,[],[]},{init,begin_i
t,1,[{file,"init.erl"},{line,1057}]},{init,begin_em,1,[{file,"init.erl"},{line,1
037}]}]}}
Crash dump was written to: erl_crash.dump
init terminating in do_boot ()
Can someone see why this is not working? I'm just trying to follow the sanfoundry example.
This code cannot compile, at least the variable C in move/4 is unbound when used. So it seems that you didn't compile this file before trying to execute it.
Although Erlang used a virtual machine, it must be compiled before execution.
There are other problems than the C variable in this code: you call move/4 recursively in the first line of the if, without using any returned value, this cannot have any effect. Also you are using a if statement with a bad syntax. the correct syntax is:
if
GuardSeq1 ->
Body1;
...;
GuardSeqN ->
BodyN % no semicolon at the end of the last body
end
if you intend to use this, beware that you must always have at least one guard that is true, otherwise the code will crash.
My version [edit: remove useless function, better print]:
-module (toh).
-export([start/1]).
start(N) ->
Game = #{1 => lists:seq(1,N), 2 => [], 3 => []},
display(Game,N),
move(N,Game,1,3,N).
move(1,Game,From,To,Size) ->
[H|NewFrom] = maps:get(From,Game),
NewTo = [H|maps:get(To,Game)],
NewGame = maps:update(From,NewFrom,maps:update(To,NewTo,Game)),
display(NewGame,Size),
NewGame;
move(N,Game,From,To,Size) ->
Other = other(From,To),
Game1 = move(N-1,Game,From,Other,Size),
Game2 = move(1,Game1,From,To,Size),
move(N-1,Game2,Other,To,Size).
display(#{1 := A, 2 := B, 3 := C},D) ->
lists:foreach(fun(X) -> print(X,D) end,lists:zip3(complete(A,D),complete(B,D),complete(C,D))),
io:format("~n~s~n~n",[lists:duplicate(6*D+5,$-)]).
complete(L,D) -> lists:duplicate(D-length(L),0) ++ L.
print({A,B,C},D) -> io:format("~s ~s ~s~n",[elem(A,D),elem(B,D),elem(C,D)]).
elem(I,D) -> lists:duplicate(D-I,$ ) ++ lists:duplicate(I,$_) ++ "|" ++ lists:duplicate(I,$_) ++ lists:duplicate(D-I,$ ).
other(I,J) -> 6-I-J.
In the shell:
Eshell V6.1 (abort with ^G)
1> c(toh).
{ok,toh}
2> toh:start(3).
_|_ | |
__|__ | |
___|___ | |
-----------------------
| | |
__|__ | |
___|___ | _|_
-----------------------
| | |
| | |
___|___ __|__ _|_
-----------------------
| | |
| _|_ |
___|___ __|__ |
-----------------------
| | |
| _|_ |
| __|__ ___|___
-----------------------
| | |
| | |
_|_ __|__ ___|___
-----------------------
| | |
| | __|__
_|_ | ___|___
-----------------------
| | _|_
| | __|__
| | ___|___
-----------------------
#{1 => [],2 => [],3 => [1,2,3]}
3>

Drupal business listing a-z indexing requirements

I want to render something like below in a view.
==================================================================
View: Car parts (Content type)
A | [B] | C | D | E | [F] | [G] | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
BONNET
FRONT BUMPER
FRONT BAR REINFORCEMENT
GUARD LEFT
GUARD RIGHT
==================================================================
How do you create the top A – Z index. The letters in [ ] are anchor links.
I think this discussion could help you:
https://drupal.org/node/403012

Removing Left Recursion from CFG

The following grammar has left recursion:
T -> Tx | TYx | YX | x
X -> xx
Y -> Yy | Yx | y
How do you go about removing left recursion. I read the wikipedia explanation, but I'm fairly new to CFGs so it did not make a lot of sense. Any help is appreciated? A plain english explanation would be even more appreciated.
In this example, you can follow Robert C. Moore's general algorithm to convert a rule with left recursion to a rule with right recursion:
A -> A a1 | A a2 | ... | b1 | b2 | ...
# converts to
A -> b1 A' | b2 A' | ...
A' -> e | a1 A' | a2 A' | ... # where e = epsilon
In our first case: A=T, a1=x, a2=Yx, b1=y, b2=x... (similarly for Y)
T -> YXT' | xT'
T' -> e | xT' | YxT'
X -> xx
Y -> yY'
Y' -> e | yY' | xY'

Resources