Syntax error when working on OCaml/LLVM tutorial - functional-programming

I am reading the tutorial of implementing kaleidoscope language on LLVM using ocaml. However, the given code lexer.ml doesn't compile...
There is a syntax error in the second line of the code
let rec lex = parser
(* Skip any whitespace. *)
| [< ' (' ' | '\n' | '\r' | '\t'); stream >] -> lex stream
Why is this happening? Thank you.

This is an old stream syntax, provided by camlp4. See the tutorial. Enabling the syntax support highly depends on your build system. Please, provide more information on that, and I will update the posting.

Related

jq: error (at <stdin>:0): Cannot iterate over null (null)

I've been working with an API call to structure it in JSON format so I might later push it into a database. Then code looks like this:
getPage() {
curl --fail -X GET 'https://api.app.com/v1/test?page=1&pageSize=1000&sort=desc' \
-H 'Authorization: Bearer 123abc456pickupsticks789' \
-H 'cache-control: no-cache'
}
getPage \
| jq -c '.items | .[] | {landing_id: .landing_id, submitted_at: .submitted_at, answers: .answers, email: .hidden.email}' \
> testpush.json
When I run it though, it produces this error: jq: error (at <stdin>:0): Cannot iterate over null (null)
I've looked at solutions such as this one, or this one from this site, and this response.
The common solution seemed to be using a ? in front of [] and I tried it in the jq line towards the bottom, but it still does not work. It just produces an empty json file.
Am I misreading the takeaway from those other answers and not putting my ? in the right place?>
To protect against the possibility that .items is not an array, you could write:
.items | .[]?
or even more robustly:
try .items[]
which is equivalent to (.items[])?.
In summary:
try E is equivalent to try E catch empty
try E is equivalent to (E)?
(Note that the expressions .items[]? and (.items[])? are not identical.)
However none of these will provide protection against input that is invalid JSON.
p.s. In future, please follow the mcve guidelines (http://stackoverflow.com/help/mcve); in the present case, it would have helped if you had provided an illustrative JSON snippet based on the output produced by the curl command.
It is necessary to let JSON know that it can continue after an unexpected value while parsing that array. try or ? are perfect options for that.
Bear in mind that it is either necessary to guarantee the data or let the interpreter to know that it is ok to continue. It may sounds redundant, but it is something like a fail-safe approach to prevent unexpected results that are harder to track/notice.
Also, it is necessary to be aware about the differences for "testing" between ? vs try.
Assuming that $sample meets JSON standards the code bellow will work always:
sample='{"myvar":1,"var2":"foo"}'
jq '{newVar: ((.op[]? | .item) // 0)}' <<< $sample
so, the op array is null for $sample as above, but it is clear to jq that it can continue without asking for your intervention/fix.
But if you do assume ? as the same as try, you may get an error (took me a loot to learn this, and it is not clear in the documentation). As an example of improper use of ? we have:
sample='{"myvar":1,"var2":"foo"}'
jq '{newVar: (.op[].item? // 0)}' <<< $sample
So, as op is null it will lead to an error, because you are telling to jq to ignore an error while retrieving .item, while there is mention about the possibility of an error during the attempt to iterate over null (in this case .op[]), and that attempt happened before that point checking for .item.
On the other hand, try would work in this case:
sample='{"myvar":1,"var2":"foo"}'
jq '{newVar: (try .op[].item catch 0)}' <<< $sample
This is a small use difference that can lead to a large difference in the result

Code Skip Command/Code Loop Command

I'm not sure if there is already a command like this existing, but what about a command like that in a code language:
do this
do that
<point2>
if (something){
GOTO ('point1')
}
do this
do that
<point1>
do this
do that
if (something){
go to ('point2')
}
a command which just leads the program to a point forward or backward in the code
i know you can do this with if clauses and functions and have the same effect
otherwise with this command you can portray code in blocks:
_____________ <-----
| start motor | | Go to command
| if failure -------
|_____________|
|
|
\/
Drive
My questions:
do we need this command? , is it useful in languages like java or php or else? and why is it unset in java? Could it be upgraded or made better and how? is it enough for not using loops anymore? Or has a goto command a major downside? Maybe in compiling or so its performance is bad... ----why dont i use it or find it in any tutorial when it could be a standard command like loops... why????
I'm thankful for a nice discussion about this command and for not writing how many grammar mistakes I made ...
"a command which just leads the program to a point forward or backward in the code" <-- it is called GOTO command. Different programming language may implement it differently.
"nice discussion about this command" <--- After your research, mind sharing which part of the reading materials/reference/code that you don't understand or can't be execute? A sample code and screenshot may help too.. (:

How can I calculate a file checksum in Elixir?

I need to calculate the md5 sum of a file in Elixir, how can this be achieved?
I would expect that something like:
iex(15)> {:ok, f} = File.open "file"
{:ok, #PID<0.334.0>}
iex(16)> :crypto.hash(:md5, f)
** (ArgumentError) argument error
:erlang.iolist_to_binary(#PID<0.334.0>)
(crypto) crypto.erl:225: :crypto.hash/2
But clearly it doesn't work..
The documentation of Mix.Utils tells about read_path/2, but it didn't worked either.
iex(22)> Mix.Utils.read_path("file", [:sha512])
{:ok, "Elixir"} #the expected was {:checksum, "<checksum_value>"}
Is there any library that provides such functionality in a easy way?
In case anyone else finds this question and misses #FredtheMagicWonderDog's comment . . .
Check out this blog posting: http://www.cursingthedarkness.com/2015/04/how-to-get-hash-of-file-in-exilir.html
And here's the relevant code:
File.stream!("./known_hosts.txt",[],2048)
|> Enum.reduce(:crypto.hash_init(:sha256),fn(line, acc) -> :crypto.hash_update(acc,line) end )
|> :crypto.hash_final
|> Base.encode16
#=> "97368E46417DF00CB833C73457D2BE0509C9A404B255D4C70BBDC792D248B4A2"
NB: I'm posting this as community wiki. I'm not trying to get rep points; just trying to ensure the answer isn't buried in comments.
This also does the job:
iex(25)> {:ok, content} = File.read "file"
{:ok, "Elixir"}
iex(26)> :crypto.hash(:md5, content) |> Base.encode16
"A12EB062ECA9D1E6C69FCF8B603787C3"
The md5sum program on the same file returned:
$ md5sum file
a12eb062eca9d1e6c69fcf8b603787c3 file
I have used the information Ryan provided in the comments above, and added the Base.encode16 to reach the final result.
I don't know elixir, but in erlang proper, crypto:hash/2 takes iodata, which a file handle is not. You need to read the file and pass the content to hash(). If you know the file is fairly small, {ok, Content} = file:read_file("file") (or the elixir equivalent) would do the trick.
Besides the #aeliton solution is short and nifty, it has the best performance.

pyparsing - parse scoped variables

So far, I was able to use pyparsing to parse ebnf grammars.
However, I wanted to try the following code sample but could not come up with
a good grammar.
global radius = 5
DrawCircle(radius)
{
radius = 10
DrawCircle(radius)
}
DrawCircle(radius)
The value of radius with in the scope should be 10, 5 otherwise.
Any help would be appreciated ?
Regards
Praveen
I was able to get the parser for the above code by running:
enclosed = Forward()
curls = nestedExpr('{', '}', content=enclosed)
enclosed << (OneOrMore(commands | ',' | curls))
I have a follow up question. I am used to write ebnf grammar using http://pyparsing.wikispaces.com/file/view/ebnf.py
Can I get some help identifying the ebnf for forward or equivalent of the above code ? or should I do outside ebnf ?
Regards
Praveen

Saxon Unexpected token " < e o f >"

I am evaluating Stylus Studio mainly for xquery development against XML payload exchanged using SOAP.
I've inherited a complex xquery (about 1800 lines) and when I try to execute it using Saxon as engine I get this error:
XPST0003: Xquery sintax error in ##:Unexpected token " < e o f >" in path expression.
This query works fine in AcquaLogic, so no really sure it's a bug in the file or Saxon.
Could anyone please give advise on this? At least to understand on which line it finds this token.
Thanks in advance
<EOF> means that Saxon encountered a literal "End of File" byte. Perhaps your path expression is corrupted? Other tools might ignore the EOF if they know the buffer is longer, but on that path lies madness.

Resources