started a graphviz visualization for the LR(0) automaton

This commit is contained in:
Krasimir Angelov
2023-09-19 21:19:54 +02:00
parent 2582719fab
commit dccb9681a2
6 changed files with 47 additions and 3 deletions

View File

@@ -48,6 +48,7 @@ exportPGF opts fmt pgf =
FmtSLF -> single "slf" slfPrinter
FmtRegExp -> single "rexp" regexpPrinter
FmtFA -> single "dot" slfGraphvizPrinter
FmtLR -> single "dot" (\_ -> graphvizLRAutomaton)
where
name = fromMaybe (abstractName pgf) (flag optName opts)
@@ -58,4 +59,3 @@ exportPGF opts fmt pgf =
single :: String -> (PGF -> Concr -> String) -> [(FilePath,String)]
single ext pr = [(concreteName cnc <.> ext, pr pgf cnc) | cnc <- Map.elems (languages pgf)]

View File

@@ -106,6 +106,7 @@ data OutputFormat = FmtPGFPretty
| FmtSLF
| FmtRegExp
| FmtFA
| FmtLR
deriving (Eq,Ord)
data SISRFormat =
@@ -490,7 +491,8 @@ outputFormatsExpl =
(("vxml", FmtVoiceXML),"Voice XML based on abstract syntax"),
(("slf", FmtSLF),"SLF speech recognition format"),
(("regexp", FmtRegExp),"regular expression"),
(("fa", FmtFA),"finite automaton in graphviz format")
(("fa", FmtFA),"finite automaton in graphviz format"),
(("lr", FmtLR),"LR(0) automaton for PMCFG in graphviz format")
]
instance Show OutputFormat where

View File

@@ -3137,3 +3137,30 @@ pgf_align_words(PgfDB *db, PgfConcrRevision revision,
return NULL;
}
PGF_API PgfText *
pgf_graphviz_lr_automaton(PgfDB *db, PgfConcrRevision revision,
PgfExn *err)
{
PGF_API_BEGIN {
DB_scope scope(db, READER_SCOPE);
ref<PgfConcr> concr = db->revision2concr(revision);
PgfPrinter printer(NULL,0,NULL);
printer.puts("digraph {");
for (size_t i = 0; i < concr->lrtable->len; i++) {
ref<PgfLRState> state = vector_elem(concr->lrtable, i);
for (size_t j = 0; j < state->shifts->len; j++) {
ref<PgfLRShift> shift = vector_elem(state->shifts, j);
printer.nprintf(16, " s%zu -> s%zu;\n", i, shift->next_state);
}
}
printer.puts("}");
return printer.get_text();
} PGF_API_END
return NULL;
}

View File

@@ -906,4 +906,8 @@ pgf_align_words(PgfDB *db, PgfConcrRevision revision,
size_t *n_phrases /* out */,
PgfExn* err);
PGF_API PgfText *
pgf_graphviz_lr_automaton(PgfDB *db, PgfConcrRevision revision,
PgfExn *err);
#endif // PGF_H_

View File

@@ -72,7 +72,7 @@ module PGF2 (-- * PGF
graphvizAbstractTree, graphvizParseTree,
Labels, getDepLabels,
graphvizDependencyTree, conlls2latexDoc, getCncDepLabels,
graphvizWordAlignment,
graphvizWordAlignment, graphvizLRAutomaton,
-- * Concrete syntax
ConcName,Concr,languages,concreteName,languageCode,concreteFlag,
@@ -1389,6 +1389,15 @@ graphvizDependencyTree
-> String -- ^ Rendered output in the specified format
graphvizDependencyTree format debug mlab mclab concr t = error "TODO: graphvizDependencyTree"
graphvizLRAutomaton :: Concr -> String
graphvizLRAutomaton c =
unsafePerformIO $
withForeignPtr (c_revision c) $ \c_revision ->
bracket (withPgfExn "graphvizLRAutomaton" (pgf_graphviz_lr_automaton (c_db c) c_revision)) free $ \c_text ->
if c_text == nullPtr
then return ""
else peekText c_text
---------------------- should be a separate module?
-- visualization with latex output. AR Nov 2015

View File

@@ -315,6 +315,8 @@ foreign import ccall pgf_graphviz_parse_tree :: Ptr PgfDB -> Ptr Concr -> Stable
foreign import ccall pgf_graphviz_word_alignment :: Ptr PgfDB -> Ptr (Ptr Concr) -> CSize -> StablePtr Expr -> Ptr PgfPrintContext -> Ptr PgfMarshaller -> Ptr PgfGraphvizOptions -> Ptr PgfExn -> IO (Ptr PgfText)
foreign import ccall pgf_graphviz_lr_automaton :: Ptr PgfDB -> Ptr Concr -> Ptr PgfExn -> IO (Ptr PgfText)
-----------------------------------------------------------------------
-- Texts