Skip to content

Commit

Permalink
started a graphviz visualization for the LR(0) automaton
Browse files Browse the repository at this point in the history
  • Loading branch information
krangelov committed Sep 19, 2023
1 parent 2582719 commit dccb968
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/compiler/GF/Compile/Export.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)]

4 changes: 3 additions & 1 deletion src/compiler/GF/Infra/Option.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ data OutputFormat = FmtPGFPretty
| FmtSLF
| FmtRegExp
| FmtFA
| FmtLR
deriving (Eq,Ord)

data SISRFormat =
Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions src/runtime/c/pgf/pgf.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 4 additions & 0 deletions src/runtime/c/pgf/pgf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
11 changes: 10 additions & 1 deletion src/runtime/haskell/PGF2.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/haskell/PGF2/FFI.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dccb968

Please sign in to comment.