Skip to content

Commit 38a0080

Browse files
author
Matthew Stern
committed
topologic parsing
0 parents  commit 38a0080

15 files changed

Lines changed: 654 additions & 0 deletions

AUTHORS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
All contributors arranged by first commit:
2+
3+
Matthew Stern
4+
Benjamin Michalowicz

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Matthew Stern, Benjamin Michalowicz
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# SPDX-License-Identifier: MIT WITH bison-exception WITH swig-exception
2+
# Copyright © 2020 Matthew Stern, Benjamin Michalowicz
3+
4+
CC=gcc
5+
CXX=g++
6+
MCS=$(MCS)
7+
8+
LDFLAGS= -lm -lpthread -ltopologic -L. -ltopologic_parse -pthread #-lfl
9+
CFLAGS=-Wall -Werror -g -fPIC -O2 -ltopologic #-fsanitize=thread
10+
AR=ar
11+
12+
BIN=topologic-parse
13+
14+
FLEX=src/topologic_parser.lex
15+
BISON=src/topologic_parser.y
16+
FLEX_C=$(FLEX:.lex=.yy.c)
17+
BISON_C=$(BISON:.y=.tab.c)
18+
BISON_H=$(BISON:.y=.tab.h)
19+
FLEX_OBJ=$(FLEX_C:.c=.o)
20+
BISON_OBJ=$(BISON_C:.c=.o)
21+
22+
FLEXPP=src/topologic_parser_cpp.lex
23+
BISONPP=src/topologic_parser_cpp.ypp
24+
FLEX_CPP=$(FLEXPP:.lex=.yy.cpp)
25+
BISON_CPP=$(BISONPP:.ypp=.tab.cpp)
26+
BISON_HPP=$(BISONPP:.ypp=.tab.hpp)
27+
FLEX_OBJ_PP=$(FLEX_CPP:.cpp=.o)
28+
BISON_OBJ_PP=$(BISON_CPP:.cpp=.o)
29+
30+
TESTS=$(TEST_SRC:.c=)#ADD MORE AS THEY GO
31+
TEST_SRC=$(wildcard testing/*.c) #ADD MORE IF NEED BE
32+
TEST_OBJ=$(TEST_SRC:.c=.o)
33+
TEST_DIR=testing
34+
35+
all: $(BISON) $(BISON_C) $(BISON_H) $(FLEX) $(FLEX_C) $(BIN) $(TESTS)
36+
37+
$(FLEX_C):
38+
flex $(FLEX)
39+
mv lex.yy.c $(FLEX_C)
40+
$(CC) -fPIC -g -c $(FLEX_C) -o $(FLEX_OBJ) -lfl -ltopologic
41+
$(BISON_C): $(BISON)
42+
bison -d $(BISON) -o $(BISON_C)
43+
$(CC) -fPIC -g -c $(BISON_C) -o $(BISON_OBJ) -ltopologic
44+
45+
$(BIN): $(OBJ) $(BISON_OBJ) $(FLEX_OBJ)
46+
$(AR) rcs libtopologic_parse.a $(OBJ) $(BISON_OBJ) $(FLEX_OBJ)
47+
48+
$(TESTS): $(BIN) $(TEST_OBJ)
49+
$(CC) $(CFLAGS) -o $@ libtopologic_parse.a $(TEST_DIR)/$(@F).o $(LDFLAGS)
50+
51+
cpp: $(BISON_CPP) $(BISON_OBJ_PP) $(BISON_HPP) $(FLEX_CPP) $(FLEX_OBJ_PP)
52+
$(AR) rcs libtopologic_parse.a $(BISON_OBJ_PP) $(FLEX_OBJ_PP)
53+
54+
$(FLEX_CPP):
55+
flex $(FLEXPP)
56+
mv lex.yy.cc $(FLEX_CPP)
57+
$(CXX) -fPIC -g -c $(FLEX_CPP) -o $(FLEX_OBJ_PP) -lfl -ltopologic
58+
$(BISON_CPP): $(BISONPP)
59+
bison -d $(BISONPP) -o $(BISON_CPP)
60+
$(CXX) -fPIC -g -c $(BISON_CPP) -o $(BISON_OBJ_PP) -ltopologic
61+
62+
all:$(BIN)
63+
.PHONY : clean cpp
64+
65+
clean:
66+
rm -f libtopologic_parse.a
67+
rm -f $(TEST_OBJ) $(TESTS)
68+
rm -f $(FLEX_C) $(FLEX_OBJ)
69+
rm -f $(BISON_C) $(BISON_OBJ) $(BISON_H)
70+
rm -f $(FLEX_CPP) $(FLEX_OBJ_PP)
71+
rm -f $(BISON_CPP) $(BISON_OBJ_PP) $(BISON_HPP)
72+
rm -f $(BIN)

README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Topologic_Parse
2+
This is for parsing JSON files for Topologic.
3+
4+
A library to simulate DFAs and Probabilistic DFAs using context switching. This library provides a way to build and define the behavior of a graph. The client is able to define the function each vertex and edge of the graph make and how the graph transitions from one (the first valid edge) or to many states, as well as which states the graph starts in.
5+
6+
The library provides a way for the client to define what information is saved localy to each vertex and edge, as well as which information the vertex and edges originating at that edge share. In addition the client can pass information to each initial state that will be passed and modifyable by vertices at each state transition.
7+
8+
This library can be used to create AI, ML, and simulations (such as of stock options, electron-electron interaction, etc.).
9+
10+
## Installation
11+
### Arch Linux
12+
>yay -S topologic_parse-git
13+
14+
-To use in program use
15+
16+
>#include <topologic/topologic_parse.h>
17+
18+
-Link with
19+
20+
>gcc ... -ltopologic_parse
21+
22+
### Manualy
23+
Build C Library
24+
>make
25+
26+
-creates libtopologic.a
27+
28+
Build C++ Library
29+
>make cpp
30+
31+
-creates libtopologic.a
32+
33+
Clean
34+
>make clean
35+
36+
Debug
37+
>make CFLAGS=-DDEBUG
38+
39+
## TODO
40+
41+
# Errata
42+
## Edge/vertex modification
43+
Modifying/deleting vertices and edges inside f() not using submit request
44+
can lead to undefined behavior or dead locks. This is because in CONTEXT
45+
set to SWITCH or NONE with many starting vertices, can lead to a structure
46+
being NULL’d with its lock destroyed while another thread is holding that lock
47+
or two threads trying to modify each other and thus resulting in a dead lock. It
48+
is up to the client’s discretion to modify directly when in SWITCH or NONE
49+
with many starting vertices. Although in SINGLE, this should be fine. To
50+
mitigate this problem, the client should use submit request which will handle
51+
the requests sequentially with destroying structures last.
52+
53+
## Graph modification
54+
Trying to delete the graph while running will result in undefined behav-
55+
ior. Destroying the graph does not lock any thread and thus will cause race
56+
conditions. The graph should be deleted only once all threads reach a sink.
57+
58+
## Parameter Passing
59+
Parameters passed to edges or vertices functions will be free’d immediately
60+
after use and therefore will cause an error should they try and be accessed. To
61+
mitigate the values may be stored in the edge’s or vertex’s global or shared
62+
variables. Any non standard data type, such as struct, is dependent on the user
63+
to free its content as the library will only free the pointer to the struct and the
64+
pointer to the array of variables.
65+
66+
## Illegal Arguments
67+
Passing wrong values or wrong number of variables to any function will result
68+
in failure. A client should be aware of which vertices connect and what edges
69+
it has and the proper handling required between such connections. Should the
70+
client choose to dynamically add/remove vertices or edges or even modify while
71+
running should be aware of the changes that may occur in the graph and the
72+
resulting change in dependence on proper variable handling.
73+
74+
## Graph Modification (cont.)
75+
It is possible to modify the graph while it is running. To do so the client
76+
should submit request to add a change or pause the graph. Making any changes
77+
externally may result in undefined behavior if done improperly. Modifying the
78+
graph directly while running could result in failure.
79+
Should the number of init vertex args not much the number of vertices in
80+
the start set the program will result in an error.
81+
82+
## Data Structure Errata
83+
Stacks and AVLTree can take non malloc’d data and function normally
84+
within scope of those non malloc’d data. However, in another scope the memory
85+
will be unaddressable and thus should be malloc’d memory instead. The stack
86+
and AVL Tree wll not free the void * data since the void * data structure is
87+
unknown to them and thus the client should free the memory.
88+
89+
## SWITCH_UNSAFE
90+
Should SWITCH_USNAFE be used as the context, then the graph will run like
91+
how it runs in SWITCH, but the variables shared between the edge and the second vertex
92+
b will be passed to the edges function. Trying to read the shared variables may lead to a
93+
race condition in which vertex b or one/some of it's edges are modifying those varibales.
94+
The client is expected to handle such race conditions, and failing to do so can lead to a possibly
95+
unsafe execution. Thus this mode is UNSAFE. It is safe to use the shared variables with vertex b
96+
in context modes NONE and SINGLE since in NONE vertex b cannot also be active, and in SINGLE there
97+
is no threading.
98+

SECURITY.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Security Policy
2+
3+
## Supported Versions
4+
5+
Use this section to tell people about which versions of your project are
6+
currently being supported with security updates.
7+
8+
| Version | Supported |
9+
| ------- | ------------------ |
10+
| 5.1.x | :white_check_mark: |
11+
| 5.0.x | :x: |
12+
| 4.0.x | :white_check_mark: |
13+
| < 4.0 | :x: |
14+
15+
## Reporting a Vulnerability
16+
17+
Use this section to tell people how to report a vulnerability.
18+
19+
Tell them where to go, how often they can expect to get an update on a
20+
reported vulnerability, what to expect if the vulnerability is accepted or
21+
declined, etc.

Topologic_Design_Document.pdf

175 KB
Binary file not shown.

include/test.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef __TOPOLOGIC_TEST__
2+
#define __TOPOLOGIC_TEST__
3+
4+
#include <topologic.h>
5+
#include <assert.h>
6+
#include <string.h>
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
10+
#endif

refman.pdf

310 KB
Binary file not shown.

src/topologic_parser.lex

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* SPDX-License-Identifier: MIT WITH bison-exception */
2+
/* Copyright © 2020 Matthew Stern, Benjamin Michalowicz */
3+
4+
%option noyywrap
5+
6+
%{
7+
#include "topologic_parser.tab.h"
8+
%}
9+
10+
blanks [ \t\n]+
11+
12+
%%
13+
{blanks} { /* IGNORE */ }
14+
"{" return(L_BRACKET);
15+
"}" return(R_BRACKET);
16+
"graph" return(GRAPH);
17+
":" return(COLON);
18+
"vertices" return(VERTICES_);
19+
"edges" return(EDGE_);
20+
"bi_edges" return(BI_EDGE_);
21+
"[" return(L_SQUARE);
22+
"]" return(R_SQUARE);
23+
"," return(COMMA);
24+
"max_state" return(MAX_STATE);
25+
"lvl_verbose" return(LVL_VERBOSE);
26+
"mem_option" return(MEM_OPT);
27+
"context" return(LEX_CONTEXT);
28+
"max_loop" return(MAX_LOOP);
29+
[0-9]+ {yylval.val = atoi(yytext); return(VALUE);}
30+
%%

src/topologic_parser.y

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/* SPDX-License-Identifier: MIT WITH bison-exception */
2+
/* Copyright © 2020 Matthew Stern, Benjamin Michalowicz */
3+
4+
%{
5+
#include <topologic/topologic.h>
6+
void f(int id, struct graph *graph, struct vertex_result *args, void *glbl, void *edge_vars) {}
7+
int edge_f(int id, void *args, void *glbl, const void *const edge_vars_a, const void *const edge_vars_b) {return 0;}
8+
void yyerror(struct graph** graph, const char *s);
9+
extern FILE *yyin;
10+
int yylex(void);
11+
%}
12+
13+
%union {
14+
struct graph *graph;
15+
int val;
16+
};
17+
18+
%parse-param {struct graph** graph}
19+
%token L_BRACKET
20+
%token R_BRACKET
21+
%token GRAPH
22+
%token COLON
23+
%token VERTICES_
24+
%token EDGE_
25+
%token BI_EDGE_
26+
%token L_SQUARE
27+
%token R_SQUARE
28+
%token COMMA
29+
%token MAX_STATE
30+
%token LVL_VERBOSE
31+
%token LEX_CONTEXT
32+
%token MEM_OPT
33+
%token MAX_LOOP
34+
%token <val> VALUE
35+
36+
%start json
37+
%%
38+
json: L_BRACKET GRAPH {*graph = GRAPH_INIT(); if (!(*graph)){fprintf(stderr, "Can't create graph\n"); return -1;}}
39+
COLON L_BRACKET content R_BRACKET
40+
R_BRACKET
41+
;
42+
content: params g
43+
| g
44+
| params
45+
|
46+
;
47+
params: verb COMMA params
48+
| state COMMA params
49+
| mem_opt COMMA params
50+
| context COMMA params
51+
| max_loop COMMA params
52+
| verb
53+
| mem_opt
54+
| context
55+
| state
56+
| max_loop
57+
|
58+
;
59+
state: MAX_STATE COLON VALUE {(*graph)->max_state_changes = $3;}
60+
;
61+
verb: LVL_VERBOSE COLON VALUE {(*graph)->lvl_verbose = $3;}
62+
;
63+
context: LEX_CONTEXT COLON VALUE {(*graph)->context = $3;}
64+
;
65+
mem_opt: MEM_OPT COLON VALUE {(*graph)->mem_option = $3;}
66+
;
67+
max_loop: MAX_LOOP COLON VALUE {(*graph)->max_loop = $3;}
68+
;
69+
g: vs COMMA es COMMA bes
70+
| vs COMMA bes COMMA es
71+
| vs COMMA es
72+
| vs COMMA bes
73+
| vs
74+
;
75+
vs: VERTICES_ COLON L_SQUARE v R_SQUARE
76+
;
77+
v: /* empty */
78+
| VALUE COMMA {if (create_vertex(*graph, f, $1, NULL) < 0) fprintf(stderr, "Failed To Create Vertex %d\n", $1);}
79+
v
80+
| VALUE {if (create_vertex(*graph, f, $1, NULL) < 0) fprintf(stderr, "Failed To Create Vertex %d\n", $1);}
81+
;
82+
es: EDGE_ COLON L_BRACKET e R_BRACKET
83+
;
84+
e: /* empty */
85+
| VALUE COLON VALUE COMMA {struct vertex *a = find((*graph)->vertices, $1); struct vertex *b = find((*graph)->vertices, $3); if (a && b) {if (create_edge(a, b, edge_f, NULL) == NULL) fprintf(stderr, "Failed to create Edge Between %d and %d\n", a->id, b->id);} else fprintf(stderr, "Invalid Vertices a:%p b:%p\n", a, b);}
86+
e
87+
| VALUE COLON VALUE {struct vertex *a = find((*graph)->vertices, $1); struct vertex *b = find((*graph)->vertices, $3); if (a && b) {if (create_edge(a, b, edge_f, NULL) == NULL) fprintf(stderr, "Failed to create Edge Between %d and %d\n", a->id, b->id);} else fprintf(stderr, "Invalid Vertices a:%p b:%p\n", a, b);}
88+
;
89+
bes:BI_EDGE_ COLON L_BRACKET be R_BRACKET
90+
;
91+
be: /* empty */
92+
| VALUE COLON VALUE COMMA {int val = 0; struct vertex *a = find((*graph)->vertices, $1); struct vertex *b = find((*graph)->vertices, $3); if (a && b) { if((val = create_bi_edge(a, b, edge_f, NULL, NULL, NULL) < 0)) fprintf(stderr, "%d: Failed to bi create Edge Between %d and %d\n", val, a->id, b->id);} else fprintf(stderr, "Invalid Vertices a:%p(%d) b:%p(%d)\n", a, $1, b, $3);}
93+
be
94+
| VALUE COLON VALUE {int val = 0; struct vertex *a = find((*graph)->vertices, $1); struct vertex *b = find((*graph)->vertices, $3); if (a && b) { if((val = create_bi_edge(a, b, edge_f, NULL, NULL, NULL) < 0)) fprintf(stderr, "%d: Failed to bi create Edge Between %d and %d\n", val, a->id, b->id);} else fprintf(stderr, "Invalid Vertices a:%p(%d) b:%p(%d)\n", a, $1, b, $3);}
95+
;
96+
%%
97+
98+
void yyerror(struct graph** graph, const char *s) {
99+
fprintf(stderr, "yerror: %s\n", s);
100+
destroy_graph(*graph);
101+
*graph = NULL;
102+
}
103+
104+
struct graph *parse_json(const char *path) {
105+
topologic_debug("%s;%s", "parse_json", path);
106+
FILE *file = fopen(path, "r");
107+
if (!file) {
108+
topologic_debug("%s;%s;%p", "parse_json", "invalid file", (void *) NULL);
109+
return NULL;
110+
}
111+
yyin = file;
112+
struct graph *graph = NULL;
113+
yyparse(&graph);
114+
yyin = NULL;
115+
fclose(file);
116+
topologic_debug("%s;%s;%p", "parse_json", "success", graph);
117+
return graph;
118+
}

0 commit comments

Comments
 (0)