首页 \ 问答 \ 如何在ANTLR中编写Tcl嵌套字符串规则?(How to write the Tcl nested string rule in ANTLR?)

如何在ANTLR中编写Tcl嵌套字符串规则?(How to write the Tcl nested string rule in ANTLR?)

Tcl嵌套字符串可以是这样的:

{abc {xyz foo {hello world}}}

上面的大括号用于包含字符串的内容,它们不是字符串的一部分(类似于双引号)。 并且可以使用"\{""\}"转义它们以将字符串"foo"更改为"foo{}"

{abc {xyz foo\{\} {hello world}}}

对于没有括号转义的那个,我有一个有效的词法规则:

NestedBraces
  :  '{' ( ~('{'|'}') | NestedBraces)* '}'
  ;

我试图找到一种方法来添加转义部分,同时保持嵌套语法,到目前为止还没有成功。


A Tcl nested String can be something like this:

{abc {xyz foo {hello world}}}

The braces above are used to enclose the content of string, they are not part of the string(Similar to double quotes). And they can be escaped using "\{" and "\}" to change string "foo" to "foo{}":

{abc {xyz foo\{\} {hello world}}}

I have a working lexical rule for the one without the brace escaping:

NestedBraces
  :  '{' ( ~('{'|'}') | NestedBraces)* '}'
  ;

I am trying to find a way to add the escaping part while keeping the nested syntax, and haven't succeeded so far.


原文:https://stackoverflow.com/questions/9880507
更新时间:2022-02-22 21:02

最满意答案

一种可能的方法:

# create data.table with open and close times by day of the week
dt_open <- dcast(melt(dt_stadium_hours,
                      measure.vars = 1:14)[, c('day','from.to') := tstrsplit(sub('_','-',variable,fixed=TRUE), split = '-')
                                           ][, variable := NULL],
                 day ~ from.to)

# create a data.table with all the play dates
DT <- data.table(dates = seq.Date(dt_player_start$played_date, 
                                  dt_player_stop$played_date,
                                  by = 'day'))[!dates %in% dt_stadium_closed$close_date]


# create a day-variable with day-abreviations similar to 'dt_open'
DT[, day := substr(tolower(weekdays(dates)),1,3)]

# join with 'dt_open' on 'day'
DT[dt_open, on = 'day', `:=` (from_time = from_time, to_time = to_time)]

# convert hour-values to data-time values
dcols <- c('from_time','to_time')
DT[, (dcols) := lapply(.SD, function(x) as.POSIXct(as.numeric(dates)*86400 + x*3600, origin = '1970-01-01', tz = 'GMT')), .SDcols = dcols]

# replace the first from-date
DT[dates == dt_player_start$played_date, from_time := as.POSIXct(paste(dt_player_start$played_date,dt_player_start$start_time), '%Y-%m-%d %H%M', tz = 'GMT')]

# replace the last to-date
DT[dates == dt_player_stop$played_date, to_time := as.POSIXct(paste(dt_player_stop$played_date,dt_player_stop$stop_time), '%Y-%m-%d %H%M', tz = 'GMT')]

# calculate hours played by day
DT[, played := to_time - from_time]

这给出了以下data.table:

> DT
         dates day           from_time             to_time          played
 1: 2017-04-14 fri 2017-04-14 15:07:00 2017-04-14 21:23:55  6.282093 hours
 2: 2017-04-15 sat 2017-04-15 07:56:34 2017-04-15 21:21:05 13.408704 hours
 3: 2017-04-17 mon 2017-04-17 07:57:54 2017-04-17 21:23:37 13.428606 hours
 4: 2017-04-18 tue 2017-04-18 07:57:54 2017-04-18 21:23:37 13.428606 hours
 5: 2017-04-19 wed 2017-04-19 07:57:54 2017-04-19 21:23:37 13.428606 hours
 6: 2017-04-20 thu 2017-04-20 07:57:54 2017-04-20 21:23:55 13.433586 hours
 7: 2017-04-23 sun 2017-04-23 09:46:00 2017-04-23 16:54:58  7.149255 hours
 8: 2017-04-24 mon 2017-04-24 07:57:54 2017-04-24 21:23:37 13.428606 hours
 9: 2017-04-25 tue 2017-04-25 07:57:54 2017-04-25 21:23:37 13.428606 hours
10: 2017-04-26 wed 2017-04-26 07:57:54 2017-04-26 21:23:37 13.428606 hours
11: 2017-04-27 thu 2017-04-27 07:57:54 2017-04-27 21:23:55 13.433586 hours
12: 2017-04-29 sat 2017-04-29 07:56:34 2017-04-29 21:21:05 13.408704 hours
13: 2017-04-30 sun 2017-04-30 09:46:00 2017-04-30 16:54:58  7.149255 hours
14: 2017-05-01 mon 2017-05-01 07:57:54 2017-05-01 21:23:37 13.428606 hours
15: 2017-05-03 wed 2017-05-03 07:57:54 2017-05-03 21:23:37 13.428606 hours
16: 2017-05-04 thu 2017-05-04 07:57:54 2017-05-04 21:23:55 13.433586 hours
17: 2017-05-05 fri 2017-05-05 07:57:54 2017-05-05 18:42:00 10.734826 hours

现在你可以得到播放时间的总和:

> DT[, sum(played)]
Time difference of 205.8624 hours

A possible approach:

# create data.table with open and close times by day of the week
dt_open <- dcast(melt(dt_stadium_hours,
                      measure.vars = 1:14)[, c('day','from.to') := tstrsplit(sub('_','-',variable,fixed=TRUE), split = '-')
                                           ][, variable := NULL],
                 day ~ from.to)

# create a data.table with all the play dates
DT <- data.table(dates = seq.Date(dt_player_start$played_date, 
                                  dt_player_stop$played_date,
                                  by = 'day'))[!dates %in% dt_stadium_closed$close_date]


# create a day-variable with day-abreviations similar to 'dt_open'
DT[, day := substr(tolower(weekdays(dates)),1,3)]

# join with 'dt_open' on 'day'
DT[dt_open, on = 'day', `:=` (from_time = from_time, to_time = to_time)]

# convert hour-values to data-time values
dcols <- c('from_time','to_time')
DT[, (dcols) := lapply(.SD, function(x) as.POSIXct(as.numeric(dates)*86400 + x*3600, origin = '1970-01-01', tz = 'GMT')), .SDcols = dcols]

# replace the first from-date
DT[dates == dt_player_start$played_date, from_time := as.POSIXct(paste(dt_player_start$played_date,dt_player_start$start_time), '%Y-%m-%d %H%M', tz = 'GMT')]

# replace the last to-date
DT[dates == dt_player_stop$played_date, to_time := as.POSIXct(paste(dt_player_stop$played_date,dt_player_stop$stop_time), '%Y-%m-%d %H%M', tz = 'GMT')]

# calculate hours played by day
DT[, played := to_time - from_time]

This gives the following data.table:

> DT
         dates day           from_time             to_time          played
 1: 2017-04-14 fri 2017-04-14 15:07:00 2017-04-14 21:23:55  6.282093 hours
 2: 2017-04-15 sat 2017-04-15 07:56:34 2017-04-15 21:21:05 13.408704 hours
 3: 2017-04-17 mon 2017-04-17 07:57:54 2017-04-17 21:23:37 13.428606 hours
 4: 2017-04-18 tue 2017-04-18 07:57:54 2017-04-18 21:23:37 13.428606 hours
 5: 2017-04-19 wed 2017-04-19 07:57:54 2017-04-19 21:23:37 13.428606 hours
 6: 2017-04-20 thu 2017-04-20 07:57:54 2017-04-20 21:23:55 13.433586 hours
 7: 2017-04-23 sun 2017-04-23 09:46:00 2017-04-23 16:54:58  7.149255 hours
 8: 2017-04-24 mon 2017-04-24 07:57:54 2017-04-24 21:23:37 13.428606 hours
 9: 2017-04-25 tue 2017-04-25 07:57:54 2017-04-25 21:23:37 13.428606 hours
10: 2017-04-26 wed 2017-04-26 07:57:54 2017-04-26 21:23:37 13.428606 hours
11: 2017-04-27 thu 2017-04-27 07:57:54 2017-04-27 21:23:55 13.433586 hours
12: 2017-04-29 sat 2017-04-29 07:56:34 2017-04-29 21:21:05 13.408704 hours
13: 2017-04-30 sun 2017-04-30 09:46:00 2017-04-30 16:54:58  7.149255 hours
14: 2017-05-01 mon 2017-05-01 07:57:54 2017-05-01 21:23:37 13.428606 hours
15: 2017-05-03 wed 2017-05-03 07:57:54 2017-05-03 21:23:37 13.428606 hours
16: 2017-05-04 thu 2017-05-04 07:57:54 2017-05-04 21:23:55 13.433586 hours
17: 2017-05-05 fri 2017-05-05 07:57:54 2017-05-05 18:42:00 10.734826 hours

Now you can get the sum of the played hours:

> DT[, sum(played)]
Time difference of 205.8624 hours

相关问答

更多
  • 尝试使用Reduce连接+呼叫: d[, num_obs := Reduce(`+`, lapply(.SD,function(x) !is.na(x)))] 如果速度很关键,您可以更多地了解Ananda的建议,以硬编码评估的列数: d[, num_obs := 4 - Reduce("+", lapply(.SD, is.na))] 使用Ananda的更大的data.table d基准测试: fun1 <- function(indt) indt[, num_obs := rowSums(!is.na ...
  • 我希望这能解决你的问题。 string start = "2016-01-07 09:00:00.000"; string end = "2016-01-07 17:00:00.000"; DateTime firstDay = Convert.ToDateTime(start); DateTime lastDay = Convert.ToDateTime(end); if (firstDay > lastDay) { } else { TimeSpan span = lastDay ...
  • 这是因为DateTime.tomorrow没有任何时间价值。 这里: DateTime.tomorrow # => Wed, 22 Apr 2015 如果你通过DateTime的官方文档,你可以看到tomorrow没有方法。 它的基本Date#tomorrow 。 您可以使用.to_time获取默认localtime 00:00:00 DateTime.tomorrow.to_time # => 2015-04-22 00:00:00 +0530 (DateTime.tomorrow.to_time - ...
  • 它确实有效。 只需取两个日期之间的差异,将包含差异的单元格格式化为Number ,然后使用以下公式之一获取所需的单位。 我会假设: A1的价值为2/16/2016 3:20:01 PM B1的价值为2/14/2016 1:20:01 PM 进入C1输入A1 - B1 ,并将其格式化为Number 如果您希望差异为天,请保留C1 。 如果你想要几小时,请使用=24*(A1 - B1) 如果你想要分钟,使用=24*60*(A1 - B1) 如果你想要秒,使用=24*60*60*(A1 - B1) It does ...
  • DATE具有日期和时间元素。 要增加迄今为止的小时数,您只需要做一些数学运算。 例如, SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'; Session altered. SQL> SELECT SYSDATE, SYSDATE + 1/24 FROM dual; SYSDATE SYSDATE+1/24 ------------------- ------------------- 2015-08- ...
  • 我认为你没有正确生成你的测试数据。 函数expand.grid()接受所有参数的笛卡尔乘积。 我不确定为什么在expand.grid()调用中包含Temperature=temp参数; 它复制每个单一组合键的每个温度值,结果产生一个有900万行(这是(10*60*5)^2 )的data.table。 我想你想每个键的温度值,这应该导致10*60*5行: df <- data.table(expand.grid(Location=loc,Date=dates,Model=mods),Temperature=t ...
  • 一种可能的方法: # create data.table with open and close times by day of the week dt_open <- dcast(melt(dt_stadium_hours, measure.vars = 1:14)[, c('day','from.to') := tstrsplit(sub('_','-',variable,fixed=TRUE), split = '-') ...
  • 我们将第二个数据集melt为long格式,按“变量”的子字符串进行分组,即只有'mon','tue'等,得到'value'列的差异,并加入原始数据集,其中分组列是使用substr创建的 days_dt[, grp := tolower(substr(day, 1, 3))][] days_dt[ melt(setDT(weighted_average_time))[, diff(value) , .(grp = sub("_.*", "", variable))], time_diff := ...
  • 我发现java.util.Calendar更干净,这是一个从3月1日(早上8点)到当前时间的小时数: final Calendar first = new Calendar.Builder() .setDate(2016, 2, 1).set(Calendar.AM_PM, 0).set(Calendar.HOUR, 8).build(); final Calendar second = Calendar.getInstance(); int numberOfDays = 0; long ...
  • 一个选项是按行序列分组,我们unlist list列('group'),将list元素paste在一起( toString(..) ),使用cSplit的splitstackshape和direction='long'将其重新splitstackshape为' long'格式,然后使用'grp'作为分组变量获取'value'列的mean 。 library(data.table) library(splitstackshape) a[, grp:= toString(unlist(group)), 1:nr ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)