compile_grammar: Compile a Nearley grammar string and return parser and...

Description Usage Arguments Examples

Description

Compile a Nearley grammar string and return parser and railroad functions

Usage

1
compile_grammar(nearley_string)

Arguments

nearley_string

character string of a Nearley grammar or a path to a .ne file

Examples

 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))

CoEDL/tidylex documentation built on May 7, 2019, 10:50 p.m.