Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions CTestTestfile.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# CMake generated Testfile for
# Source directory: /home/compilerkit/CompilerKit
# Build directory: /home/compilerkit/CompilerKit
#
# This file includes the relevent testing commands required for
# testing this directory and lists subdirectories to be tested as well.
ADD_TEST(complement-test "complement-test")
ADD_TEST(nonterminal-test "nonterminal-test")
ADD_TEST(to-graphviz-visitor-test "to-graphviz-visitor-test")
ADD_TEST(kleene-star-test "kleene-star-test")
ADD_TEST(empty-set-test "empty-set-test")
ADD_TEST(automata-test "automata-test")
ADD_TEST(string-builder-visitor-test "string-builder-visitor-test")
ADD_TEST(symbol-test "symbol-test")
ADD_TEST(scanner-test "scanner-test")
ADD_TEST(alternation-test "alternation-test")
ADD_TEST(empty-string-test "empty-string-test")
ADD_TEST(nullable-visitor-test "nullable-visitor-test")
ADD_TEST(terminal-test "terminal-test")
ADD_TEST(visitor-test "visitor-test")
ADD_TEST(convenience-test "convenience-test")
ADD_TEST(concatenation-test "concatenation-test")
ADD_TEST(pushdown-automata-test "pushdown-automata-test")
ADD_TEST(to-nfa-visitor-test "to-nfa-visitor-test")
ADD_TEST(to-grammar-visitor-test "to-grammar-visitor-test")
ADD_TEST(derivative-visitor-test "derivative-visitor-test")
ADD_TEST(grammar-test "grammar-test")
ADD_TEST(production-test "production-test")
24 changes: 10 additions & 14 deletions examples/automata-demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CompilerKitFSM *state_machine ()
CompilerKitFSM* fsm; // Creates pointer for FSM

fsm = compilerkit_FSM_new("A"); // Calls the constructor for the FSM
printf("Object created\r\n");
// compilerkit_FSM_set_start_state(fsm, "A");
compilerkit_FSM_add_transition (fsm, "A", "B", 'd');
compilerkit_FSM_add_transition (fsm, "A", "F", 'f');
Expand All @@ -19,24 +20,18 @@ CompilerKitFSM *state_machine ()
compilerkit_FSM_add_transition (fsm, "F", "G", 's');
compilerkit_FSM_add_transition (fsm, "G", "H", 'm');
compilerkit_FSM_add_transition (fsm, "H", "A", ' ');
compilerkit_FSM_add_accepting_state (fsm, "E");
compilerkit_FSM_add_accepting_state (fsm, "H");
printf("Transitions added\r\n");
compilerkit_FSM_add_end_state (fsm, "F");
compilerkit_FSM_add_end_state (fsm, "E");
compilerkit_FSM_add_end_state (fsm, "H");
printf("End states added\r\n");
return fsm;
}

// Print out the states
void print_states(CompilerKitFSM *fsm)
{
GList *list;

printf ("states: ");
list = compilerkit_FSM_get_states (fsm);
while (list) {
printf ((compilerkit_FSM_is_accepting_state(fsm, list->data) ? "(%s) " : "%s "), list->data);
list = g_list_next(list);
}
printf ("\n");
g_list_free (list);

}

// Match a string.
Expand All @@ -53,6 +48,8 @@ void match_string(CompilerKitFSM *fsm)
while (*str) {
printf ("%s %c\n", state, *str);
state = compilerkit_FSM_get_next_state (fsm, state, *str);
if(state == NULL)
break;
*str++;
}
printf ((compilerkit_FSM_is_accepting_state(fsm, state)) ?
Expand All @@ -68,8 +65,7 @@ int main (int argc, char ** argv)
g_type_init();

fsm = state_machine();
print_states(fsm);
/*print_states(fsm);*/
match_string(fsm);

g_object_unref (fsm); // Decreases the reference count by 1, if count becomes 0, then free memeory.
}
13 changes: 13 additions & 0 deletions examples/nfa-demo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stdio.h>
#include "CompilerKit/nfa.h"

/** @todo Describe what task main will accomplish. */
int main (int argc, char ** argv)
{
CompilerKitNFA* nfa;
g_type_init();

/** @todo Briefly show how to use the methods in CompilerKitNFA to accomplish the task. */

g_object_unref (nfa);
}
48 changes: 24 additions & 24 deletions include/CompilerKit/automata.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ typedef struct _CompilerKitFSM

} CompilerKitFSM;

#define bool int
#define true 1
#define false 0

typedef struct _CompilerKitFSMNode
{
gchar* id;
gchar data;
GList* paths;
bool visited;
bool endState;
} CompilerKitFSMNode;

/**
* @struct CompilerKitFSMClass
* A finite state machine class struct.
Expand All @@ -80,42 +93,29 @@ typedef struct _CompilerKitFSMClass
GType compilerkit_FSM_get_type (void);

/* Public method function prototypes */
CompilerKitFSM *compilerkit_FSM_new (gchar *str);

void compilerkit_FSM_set_start_state (CompilerKitFSM* self,
gchar *state);
gchar *compilerkit_FSM_get_start_state (CompilerKitFSM* self);
CompilerKitFSM *compilerkit_FSM_new (gchar *str);

void compilerkit_FSM_add_state (CompilerKitFSM *self,
gchar *state);
void compilerkit_FSM_set_start_state(CompilerKitFSM *self, gchar* id);

GList *compilerkit_FSM_get_states (CompilerKitFSM *self);
gchar* compilerkit_FSM_get_start_state(CompilerKitFSM *self);

GList *compilerkit_FSM_get_transitions (CompilerKitFSM *self);
void compilerkit_FSM_add_transition(CompilerKitFSM* self, gchar* parentID, gchar* id, gchar value);

gboolean compilerkit_FSM_has_state (CompilerKitFSM *self,
gchar *state);
bool compilerkit_FSM_has_state(CompilerKitFSM* self, gchar* id);

void compilerkit_FSM_add_end_state(CompilerKitFSM* self,gchar* id);

void compilerkit_FSM_add_transition (CompilerKitFSM* self,
gchar *from_state,
gchar *to_state,
gchar input);
void compilerkit_FSM_node_reset_visited(CompilerKitFSMNode* node);

gchar *compilerkit_FSM_get_next_state (CompilerKitFSM *self,
gchar *from_state,
gchar transition);
CompilerKitFSMNode* compilerkit_FSM_node_find(CompilerKitFSMNode* node, gchar* id);

void compilerkit_FSM_add_accepting_state (CompilerKitFSM* self,
gchar *state);
bool compilerkit_FSM_node_add(CompilerKitFSM *self, gchar* parentID, gchar* id, gchar value);

GList *compilerkit_FSM_get_accepting_states (CompilerKitFSM *self);
void compilerkit_FSM_node_destroy_all(CompilerKitFSMNode* node);

gboolean compilerkit_FSM_is_accepting_state (CompilerKitFSM *self,
gchar *state);
gchar* compilerkit_FSM_get_next_state(CompilerKitFSM* self, gchar* state, gchar value);

void compilerkit_FSM_merge (CompilerKitFSM *self,
CompilerKitFSM *other);

G_END_DECLS
#endif
86 changes: 86 additions & 0 deletions include/CompilerKit/nfa.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Copyright (C) 2012 The CompilerKit contributors.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef INCLUDE_CompilerKit_nfa_h__
#define INCLUDE_CompilerKit_nfa_h__

#include <glib-object.h>
G_BEGIN_DECLS
#define COMPILERKIT_TYPE_NFA (compilerkit_nfa_get_type ())
#define COMPILERKIT_NFA(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), COMPILERKIT_TYPE_NFA, CompilerKitNFA))
#define COMPILERKIT_IS_NFA(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), COMPILERKIT_TYPE_NFA))
#define COMPILERKIT_NFA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), COMPILERKIT_TYPE_NFA, CompilerKitNFAClass))
#define COMPILERKIT_IS_NFA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), COMPILERKIT_TYPE_NFA))
#define COMPILERKIT_NFA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), COMPILERKIT_TYPE_NFA, CompilerKitNFAClass))

typedef struct _CompilerKitNFAPrivate CompilerKitNFAPrivate;

/**
* @struct CompilerKitNFA
* @todo Briefly describe this struct. (Remove the todo).
*
* Defines all public fields. Private fields live behind an opaque pointer.
* @see #_CompilerKitNFAPrivate for private fields.
* @see #CompilerKitNFAClass for virtual public methods.
* @example nfa-demo.c
* This is an example of how to use the CompilerKitNFA struct.
*/
typedef struct _CompilerKitNFA
{
/** Base instance (GObject) */
GObject parent_instance;

/** @todo Define public fields here */

/** Opaque pointer to private fields */
CompilerKitNFAPrivate *priv;

} CompilerKitNFA;

/**
* @struct CompilerKitNFAClass
* @todo Briefly describe this struct. (Remove the todo).
*
* This struct declares the virtual public methods.
* @see #CompilerKitNFA for a list of fields.
*/
typedef struct _CompilerKitNFAClass
{
/** Base class (GObjectClass) */
GObjectClass parent_class;

/** @todo Virtual public methods (function pointers) go here */
// void (*method_name) (CompilerKitNFA *self, ...);
} CompilerKitNFAClass;

/**
* @fn compilerkit_nfa_get_type
* Returns the runtime type information for CompilerKitNFA. Macro COMPILERKIT_TYPE_NFA uses it.
* @pre None
* @param None
* @return GType (runtime type information)
*/
GType compilerkit_nfa_get_type (void);

/** Public method function prototypes
* @todo Add function prototypes here for both virtual and non-virtual public methods.
* @see http://developer.gnome.org/gobject/stable/howto-gobject-methods.html
*/
CompilerKitNFA* compilerkit_nfa_new (void);

G_END_DECLS
#endif /* INCLUDE_CompilerKit_nfa_h__ */
Loading