Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: nfa.h
- * Creator: George Ferguson
- * Created: Thu Sep 1 17:54:41 2016
- * Time-stamp: <Tue Aug 8 11:45:49 EDT 2017 ferguson>
- */
- #ifndef _nfa_h
- #define _nfa_h
- #include <stdbool.h>
- #include "Set.h"
- #include "LinkedList.h"
- #include "LinkedList.h"
- /**
- * The data structure used to represent a nondeterministic finite automaton.
- * @see FOCS Section 10.3
- * @see Comments for DFA in dfa.h
- */
- typedef struct NFA NFA;
- /**
- * Allocate and return a new NFA containing the given number of states.
- */
- extern NFA new_NFA(NFA Q, int nstates);
- extern bool NFA_execute2(NFA nfa, char *input);
- /**
- * Free the given NFA.
- */
- extern void NFA_free(NFA nfa);
- /**
- * Return the number of states in the given NFA.
- */
- extern int NFA_get_size(NFA nfa);
- /**
- * Return the set of next states specified by the given NFA's transition
- * function from the given state on input symbol sym.
- */
- extern Set NFA_get_transitions(NFA nfa, int state, char sym);
- /**
- * For the given NFA, add the state dst to the set of next states from
- * state src on input symbol sym.
- */
- extern void NFA_add_transition(NFA nfa, int src, char sym, int dst,IntHashSet a[][128]);
- /**
- * Add a transition for the given NFA for each symbol in the given str.
- */
- extern void NFA_add_transition_str(NFA nfa, int src, char *str, int dst);
- /**
- * Add a transition for the given NFA for each input symbol.
- */
- extern void NFA_add_transition_all(NFA nfa, int src, int dst,IntHashSet a[][128]);
- /**
- * Set whether the given NFA's state is accepting or not.
- */
- extern void NFA_set_accepting(NFA nfa, int state, bool value);
- /**
- * Return true if the given NFA's state is an accepting state.
- */
- extern bool NFA_get_accepting(NFA nfa, int state);
- /**
- * Run the given NFA on the given input string, and return true if it accepts
- * the input, otherwise false.
- */
- extern bool NFA_execute(NFA nfa, char *input, IntHashSet a[][128]);
- /**
- * Print the given NFA to System.out.
- */
- extern void NFA_print(NFA nfa);
- typedef struct NFA {
- int States; //number of states
- int finStates; // number of accepting states
- int counter; //tracks which state it is right now
- } NFA;
- extern NFA new_NFA(NFA Q, int nstates){ //rows columns for the transition array
- NFA* Qp=&Q;
- Qp->States=nstates;
- return Q;
- }
- extern void NFA_add_transition_all(NFA nfa, int src, int dst, IntHashSet a[][128]){
- for(int i=0;i<128;i++){
- IntHashSet_insert(a[src][i], dst); // equivalent to arr[src][i]=dst
- }
- //
- }
- extern void NFA_add_transition(NFA nfa, int src, char sym, int dst, IntHashSet a[][128]){
- //equivalent to arr[src][sym]=dst. but this is using pointer arithmetic
- IntHashSet_insert(a[src][sym], dst);
- printf("is it reaching for ffs");
- }
- extern void NFA_add_transition_except(NFA nfa, int src, char sym, int dst, IntHashSet a[][128]);
- extern void NFA_add_transition_except(NFA nfa, int src, char sym, int dst, IntHashSet a[][128]){
- //equivalent to arr[src][sym]=dst. but this is using pointer arithmetic
- for(int i=0;i<128;i++){
- if(i!=sym){
- IntHashSet_insert(a[src][i], dst); // equivalent to arr[src][i]=dst
- }
- }
- }
- extern bool NFA_execute(NFA nfa, char *input, IntHashSet a[][128]){
- IntHashSet set1 = new_IntHashSet(100);
- IntHashSet set2 = new_IntHashSet(100);
- IntHashSet_insert(set1, 0);
- for(int i=0; input[i]!='\0';i++){ //loops through the input, and changes the current state according to the transition table
- IntHashSetIterator iterator = IntHashSet_iterator(set1);
- while (IntHashSetIterator_hasNext(iterator)) {
- int element = IntHashSetIterator_next(iterator); //extracting the element from the hashset, which corresponds to the possible transition on the character input[i]
- //put the linked list(the list of possible transition of input[i] in a linked list called temp
- IntHashSetIterator iterator2 = IntHashSet_iterator(a[element][input[i]]);
- while (IntHashSetIterator_hasNext(iterator2)) {
- int data = IntHashSetIterator_next(iterator2);//loop through temp
- printf("the hashset of letter %c, at state %i", input[i], element);
- printf("\n the value that will be added is%i, the character is %c", data, input[i]);
- if(data!=5)IntHashSet_insert(set2, data); //add it to the hashset
- }
- free(iterator2);
- }
- free(iterator);
- IntHashSet_free(set1);
- IntHashSet set1 = new_IntHashSet(100);
- IntHashSet_union(set1, set2);
- /*IntHashSetIterator iterator3 = IntHashSet_iterator(a[1]['o']);
- while (IntHashSetIterator_hasNext(iterator3)) {
- int element = IntHashSetIterator_next(iterator3);
- printf("\n \n checking the set 1 %d ", element);
- } free(iterator3);*/
- IntHashSet_free(set2);
- }
- bool d= false;
- //after everything, we loop through our set1, if it has the accepting state, we accept the string.
- IntHashSetIterator iterator4 = IntHashSet_iterator(set1);
- while (IntHashSetIterator_hasNext(iterator4)) {
- int element = IntHashSetIterator_next(iterator4);
- printf("\n current states are element %i", element);
- if (element==nfa.finStates) d=true;
- }
- free(iterator4);
- IntHashSet_free(set1);
- free(input);
- return d;
- }
- /*extern bool NFA_execute2(NFA nfa, char *input){
- IntHashSet set3 = new_IntHashSet(100);
- IntHashSet set4 = new_IntHashSet(100);
- IntHashSet_insert(set3, 0);
- for(int i=0; input[i]!='\0';i++){ //loops through the input, and changes the current state according to the transition table
- IntHashSetIterator iterator = IntHashSet_iterator(set3);
- while (IntHashSetIterator_hasNext(iterator)) {
- int element = IntHashSetIterator_next(iterator); //extracting the element from the hashset, which corresponds to the possible transition on the character input[i]
- //put the linked list(the list of possible transition of input[i] in a linked list called temp
- LinkedListIterator iterator2 = LinkedList_iterator(*(nfa.ArrayP2 + (element*128 + input[i])));
- while (LinkedListIterator_hasNext(iterator2)) { //loop through temp
- void *data = LinkedListIterator_next(iterator2);
- char *str = (char*)data; //extracting the state that we transitioned to
- int val = atoi(str); //convert it to int so it can be added to the hashset
- printf("\n the value that will be added is%i, the character is %c", val, input[i]);
- if(val!=-1) IntHashSet_insert(set4, val); //add it to the hashset
- }
- free(iterator2);
- }
- free(iterator);
- IntHashSet_free(set3);
- IntHashSet set3 = new_IntHashSet(100);
- IntHashSet_union(set3, set4);
- IntHashSet_free(set4);
- IntHashSet set4 = new_IntHashSet(100);
- }
- bool d= false;
- //after everything, we loop through our set1, if it has the accepting state, we accept the string.
- IntHashSetIterator iterator = IntHashSet_iterator(set3);
- while (IntHashSetIterator_hasNext(iterator)) {
- int element = IntHashSetIterator_next(iterator);
- printf("\n current states are element %i", element);
- if (element==nfa.finStates) d=true;
- }
- free(iterator);
- IntHashSet_free(set3);
- IntHashSet_free(set4);
- free(input);
- return d;
- }*/
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement