Description Usage Arguments Examples
Compile a Nearley grammar string and return parser and railroad functions
1 | compile_grammar(nearley_string)
|
nearley_string |
character string of a Nearley grammar or a path to a .ne file |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | # 'Hello world' demo:
parser <- compile_grammar('sequence -> "x" "y" "z"')
parser$parse_str("xyz")
# A more complete demo:
# 1. Read a lexicon and group lines using zoo:na.locf0()
lexicon_df <-
system.file("extdata", "error-french.txt", package = "tidylex") %>%
read_lexicon(regex = "\\\\([a-z]+)", into = "code") %>%
mutate(lx_start = ifelse(code == "lx", line, NA) %>% zoo::na.locf0())
# 2. Define and compile a Nearley grammar to test code sequences
headword_parser <- compile_grammar('
headword -> "lx" "ps" "de" example:?
example -> "xv" "xe"
')
# 3. For each 'lx_start' group, test the sequence of codes against grammar
lexicon_df %>%
group_by(lx_start) %>%
mutate(code_ok = headword_parser$parse_str(code, return_labels = TRUE))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.