diff --git a/examples/base64.c b/examples/base64.c index cdb088ea48c4d77e7386f6cdcdf7f3fac712e74f..ee142e3cbfee5fa7bf2032d4f07882581916d4f7 100644 --- a/examples/base64.c +++ b/examples/base64.c @@ -1,3 +1,13 @@ +// Example parser: Base64, syntax only. +// +// Demonstrates how to construct a Hammer parser that recognizes valid Base64 +// sequences. +// +// Note that no semantic evaluation of the sequence is performed, i.e. the +// byte sequence being represented is not returned, or determined. See +// base64_sem1.c and base64_sem2.c for examples how to attach appropriate +// semantic actions to the grammar. + #include "../src/hammer.h" const HParser* document = NULL; diff --git a/examples/base64_sem1.c b/examples/base64_sem1.c index 8638bb378b9c98da004eba77af5b898aaddc4a4e..92f0b3fcd61b720225744d6aa1168fe0f32f4774 100644 --- a/examples/base64_sem1.c +++ b/examples/base64_sem1.c @@ -1,3 +1,17 @@ +// Example parser: Base64, with fine-grained semantic actions +// +// Demonstrates how to attach semantic actions to grammar rules and piece by +// piece transform the parse tree into the desired semantic representation, +// in this case a sequence of 8-bit values. +// +// Note how the grammar is defined by using the macros H_RULE and H_ARULE. +// Those rules using ARULE get an attached action which must be declared (as +// (a function of type HAction) with a standard name based on the rule name. +// +// This variant of the example uses fine-grained semantic actions that +// transform the parse tree in small steps in a bottom-up fashion. Compare +// base64_sem2.c for an alternative approach using a single top-level action. + #include "../src/hammer.h" #include "../src/internal.h" // for h_carray functions (XXX ?!) #include <assert.h> diff --git a/examples/base64_sem2.c b/examples/base64_sem2.c index 957ac48c05917df2f9cd01bd7211ffb634f43ff1..c57555e5b5484773ecc6dc74d1737bbfbcca569f 100644 --- a/examples/base64_sem2.c +++ b/examples/base64_sem2.c @@ -1,3 +1,18 @@ +// Example parser: Base64, with fine-grained semantic actions +// +// Demonstrates how to attach semantic actions to a grammar and transform the +// parse tree into the desired semantic representation, in this case a sequence +// of 8-bit values. +// +// Note how the grammar is defined by using the macros H_RULE and H_ARULE. +// Those rules using ARULE get an attached action which must be declared (as +// (a function of type HAction) with a standard name based on the rule name. +// +// This variant of the example uses coarse-grained semantic actions, +// transforming the entire parse tree in one big step. Compare base64_sem1.c +// for an alternative approach using a fine-grained piece-by-piece +// transformation. + #include "../src/hammer.h" #include "../src/internal.h" // for h_carray functions (XXX ?!) #include <assert.h>