Skip to content

Commit

Permalink
Add an internal pseudo-symbol "symbol marker"
Browse files Browse the repository at this point in the history
to let the generator function know when the reducer
finished reducing a particular non-terminal
  • Loading branch information
dmbaturin committed Nov 30, 2024
1 parent d757edb commit a1d65b1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/lib/bnfgen.mli
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Grammar : sig
| Terminal of string
| Nonterminal of string
| Repeat of symbol * (int * int)
| SymbolMarker of string

type rule_alternative = { weight: int; symbols: symbol list }
type rule = string * (rule_alternative list)
Expand Down
8 changes: 7 additions & 1 deletion src/lib/grammar.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(*
* Copyright (c) 2021 Daniil Baturin
* Copyright (c) 2024 Daniil Baturin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -26,6 +26,7 @@ type symbol =
| Terminal of string
| Nonterminal of string
| Repeat of symbol * (int * int)
| SymbolMarker of string

type rule_alternative = { weight: int; symbols: symbol list }
type rule = string * (rule_alternative list)
Expand All @@ -37,6 +38,7 @@ let grammar_error s = raise (Grammar_error s)

let rec string_of_symbol s =
match s with
| SymbolMarker name -> Printf.sprintf "(SymbolMarker <%s>)" name
| Terminal s -> Printf.sprintf "\"%s\"" s
| Nonterminal s -> Printf.sprintf "<%s>" s
| Repeat (s, (min, max)) ->
Expand Down Expand Up @@ -157,6 +159,9 @@ let reduce_symbol ?(debug=false) ?(debug_fun=print_endline) sym_stack grammar =
| [] -> (None, [])
| sym :: syms ->
match sym with
| SymbolMarker name ->
let () = if debug then Printf.ksprintf debug_fun "Finished resolving symbol \"%s\"" name in
(None, syms)
| Terminal t ->
let () = if debug then Printf.ksprintf debug_fun "Emitting terminal \"%s\"" t in
(Some t, syms)
Expand All @@ -171,6 +176,7 @@ let reduce_symbol ?(debug=false) ?(debug_fun=print_endline) sym_stack grammar =
let () =
if debug then Printf.ksprintf debug_fun "Alternative taken: %s" (string_of_rule_rhs_part {weight=1; symbols=new_syms})
in
let new_syms = List.append new_syms [(SymbolMarker name)] in
let syms = List.append new_syms syms in
(None, syms)
| Repeat (s, (min, max)) ->
Expand Down

0 comments on commit a1d65b1

Please sign in to comment.