Using an If statement in JavaCC - javacc

I am pretty new to JavaCC, and I can't figure out how to create a conditional in a JavaCC grammar.
I have two tokens which are an ARROW ("->") and a RATE ("[double]"). In my grammar a RATE can go before, or after an ARROW.
How do I write the grammar to do basically this:
if nextToken is RATE:
r = Rate()
else if nextToken is ARROW:
ARROW
r = Rate()
etc.
Thanks for your help.

I have figured out an answer. I don't know if this is the best solution, but it is working.
I have:
(R = Rate() <ARROW>
| <ARROW> R = Rate())

Related

Any knows the method addinfocallback() in JuMP#0.18 now is replaced by what?

I found following method addinfocallback() in the document of JuMP beta0.18, but couldn't find any in the latest beta's document.
Did anyone know any?
solutionvalues = Vector{Float64}[]
# build model ``m`` up here
function infocallback(cb)
push!(solutionvalues, JuMP.getvalue(x))
end
addinfocallback(m, infocallback, when = :MIPSol)
solve(m)
There is no equivalent. Here are the docs: https://jump.dev/JuMP.jl/stable/manual/callbacks/
If you want to find multiple MIP solutions, use:
https://jump.dev/JuMP.jl/stable/manual/solutions/#Multiple-solutions

How do I display with summaryCodings() only numbers of codings of a certain codecategory or several codes?

I'm using RQDA right now (for the first time) and I want to have an overview/comparison of the numbers of codings (to see which codes were used comparatively often, which were barely used).
I've tried summaryCodings() but it only gives me an overview of all codes.
How do I specify to make sure only certain codes (or, for example, one code category, and not all codes) are displayed?
I've tried variations like summaryCodings(codename == "xy") or summaryCodings(codecategory == "xx"), getCodingTable(codename == "xy" | codename == "zy").
I'm a beginner so still learning how to manage RQDA (obviously). Thank you for your help in advance!
This code will help you:
setwd("<working path>")
library("RMySQL")
con <- dbConnect(RSQLite::SQLite(), dbname="<database_name.rqda>")
answer <- dbGetQuery( con,'SELECT freecode.name, count(coding.cid) FROM treeCode, freecode, codecat, coding WHERE coding.cid = freecode.id AND treeCode.catid = codecat.catid AND treeCode.cid = freecode.id AND codecat.name = "<your option>" GROUP BY freecode.name')
dbDisconnect(con)
Now, you have the desired answer in 'answer' variable

Is there a way to let the console in RStudio produce time stamps? [duplicate]

I wonder if there is a way to display the current time in the R command line, like in MS DOS, we can use
Prompt $T $P$G
to include the time clock in every prompt line.
Something like
options(prompt=paste(format(Sys.time(), "%H:%M:%S"),"> "))
will do it, but then it is fixed at the time it was set. I'm not sure how to make it update automatically.
Chase points the right way as options("prompt"=...) can be used for this. But his solutions adds a constant time expression which is not what we want.
The documentation for the function taskCallbackManager has the rest:
R> h <- taskCallbackManager()
R> h$add(function(expr, value, ok, visible) {
+ options("prompt"=format(Sys.time(), "%H:%M:%S> "));
+ return(TRUE) },
+ name = "simpleHandler")
[1] "simpleHandler"
07:25:42> a <- 2
07:25:48>
We register a callback that gets evaluated after each command completes. That does the trick. More fancy documentation is in this document from the R developer site.
None of the other methods, which are based on callbacks, will update the prompt unless a top-level command is executed. So, pressing return in the console will not create a change. Such is the nature of R's standard callback handling.
If you install the tcltk2 package, you can set up a task scheduler that changes the option() as follows:
library(tcltk2)
tclTaskSchedule(1000, {options(prompt=paste(Sys.time(),"> "))}, id = "ticktock", redo = TRUE)
Voila, something like the MS DOS prompt.
NB: Inspiration came from this answer.
Note 1: The wait time (1000 in this case) refers to the # of milliseconds, not seconds. You might adjust it downward when sub-second resolution is somehow useful.
Here is an alternative callback solution:
updatePrompt <- function(...) {options(prompt=paste(Sys.time(),"> ")); return(TRUE)}
addTaskCallback(updatePrompt)
This works the same as Dirk's method, but the syntax is a bit simpler to me.
You can change the default character that is displayed through the options() command. You may want to try something like this:
options(prompt = paste(Sys.time(), ">"))
Check out the help page for ?options for a full list of things you can set. It is a very useful thing to know about!
Assuming this is something you want to do for every R session, consider moving that to your .Rprofile. Several other good nuggets of programming happiness can be found hither on that topic.
I don't know of a native R function for doing this, but I know R has interfaces with other languages that do have system time commands. Maybe this is an option?
Thierry mentioned system.time() and there is also proc.time() depending on what you need it for, although neither of these give you the current time.

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

how to prevent the output of R to scroll away in bash? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Equivalent to unix “less” command within R console
I am using R under unix in bash.
Sometimes the output of a command has more lines than the bash.
How do I prevent the output from scrolling away? I.e. is there some equivalent of less and less -S in R?
A way to do this in R is also to redirect to a file:
sink("a_file.txt")
...your_commands...
sink()
I think the page() function is like having | less in an R session. It allows two representations of the object; i) a version you'd get from dput(), and ii) a version you'd get if you print()-ed the object.
dat <- data.frame(matrix(rnorm(2000), ncol = 5))
page(dat, method = "print")
It might be possible to wrap your expression in capture.output, and then page the result to the terminal.
pager <- function(cmd,nlines=10){
output = capture.output(cmd)
pages = seq(1,length(output),by=nlines)
for(p in pages){
f = p
l = min(p+nlines-1,length(output))
cat(paste(output[f:l],"\n"))
readline("*more*")
}
return(invisible(0))
}
Usage: pager(ls()), then hit Return (not space or anything else) at each 'more' prompt.
currently it doesn't return the value. Oh and it fails if there's no output. But you can fix these :)
Or use emacs with ESS and let it all scroll back...
Wouldnt
any_command | more
work fine?
“the bash” has no lines, your terminal has.
You can set the number of lines of your terminal in the settings of that application.
Your question is unclear. If you're talking about using R interactively and accidentally running a command which spits out a huge number of lines, run something like this in your R session: options(max.print=4000)

Resources