rendered paste body/*
* (c) Все права принадлежат владельцу!
*/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#pragma warning(disable : 4996)
#define BufLen 256
struct Config //Automation configuration
{
int s1; //status
char* tc; // Current tape char
char mc; //Stack char
int s2;
char* RStr; //result str 4 steck
} *Rules;
struct State
{
int s1; //state
char* ts; //string on the type
char* ms; //string in the stack
//int lc; //What`s fuck?
char* c; //stack top character
} SRoot;
unsigned int Rc = 0;
char* st = "eps";
//Это, блядь, надо бы переписать и разобрать!!!!!
void AddRule(int s1, char* cc, char sc, int s2, char* str)
{
Rules = realloc(Rules, sizeof(struct Config)*(Rc + 1));
if (cc == NULL) cc = st;
Rules[Rc].tc = malloc(sizeof(char)*strlen(cc));
strcpy(Rules[Rc].tc, cc);
Rules[Rc].s1 = s1;
Rules[Rc].mc = sc;
if (str == NULL) str = st;
Rules[Rc].RStr = malloc(sizeof(char)*strlen(str));
strcpy(Rules[Rc].RStr, str);
Rules[Rc].s2 = s2;
Rc++;
}
void ShowRules()
{
unsigned int i = 0;
for (; i < Rc; i++)
printf("d(s%d, %s, %c) = (s%d, %s)\n", Rules[i].s1, Rules[i].tc, Rules[i].mc,
Rules[i].s2, Rules[i].RStr);
}
void GramRuleParser(char* str)
{
char *p = str, *token = NULL;
unsigned int i = 0;
while ((p = memchr(p, '|', strlen(p))) != NULL)
p++;
p = memchr(str, '>', strlen(str));
++p;
for (; i < strlen(p); i++)
if ((p[i] != '|') && ((p[i] < 'A') || (p[i] > 'Z')))
{
char tstr[] = {p[i], '\0'};
AddRule(0, tstr, p[i], 0, st);
}
// Establish string and get the first token:
token = strtok( p, "|\n" );
while( token != NULL )
{
char *Rtoken = malloc(sizeof(char)*(strlen(token) + 1));//reverse token
size_t i = 0, slen = strlen(token);
for (; i < slen; i++) Rtoken[slen - 1 - i] = token[i];
Rtoken[slen] = '\0';
/*char *s, *e;
token = "12345678";
for (s = token, e = &token[strlen(token) - 1];
s < e;
*s = *s+*e, *e=*s-*e, *s=*s-*e, s++, e--)*
if (1)
{
int gf= 0;
}*/
AddRule(0, st, str[0], 0, Rtoken);
token = strtok( NULL, "|\n" );
}
}
int Tree(struct State SList)
{
unsigned int i = 0;
if (strlen(SList.ms) > strlen(SList.ts) + 1)
return -1;
for (; i < Rc; i++)
if ((!strcmp(Rules[i].tc, st) && (Rules[i].mc == SList.c[0]))
|| ((Rules[i].tc[0] == SList.ts[0]) && (Rules[i].mc == SList.c[0])))
{
// Уже видимо не на надо!!!! ну нах.. этоТТ Си.... (
struct State NSList;
size_t tc = strlen(SList.ms);
//конструктор копирования мля... :(
NSList.ms = malloc(sizeof(char)*(tc + 1));
strcpy(NSList.ms, SList.ms);
NSList.s1 = SList.s1;
NSList.ts = malloc(sizeof(char)*(strlen(SList.ts) + 1));
strcpy(NSList.ts, SList.ts);
if (tc >= 1) NSList.ms[tc - 1] = '\0';
else NSList.ms[0] = '\0';
if (!strcmp(Rules[i].tc, st))
{
if (strcmp(Rules[i].RStr, st))
NSList.ms = strcat(NSList.ms, Rules[i].RStr);
}
else
{
NSList.ms[tc - 1] = '\0';
NSList.ts++;
}
tc = strlen(NSList.ms);
if (tc >= 1) NSList.c = &NSList.ms[tc - 1];
else NSList.c = 0;
if (!tc)
{
if (!strlen(NSList.ts)) return 1;
}
else
if (Tree(NSList) == 1) return 1;
}
return -1;
}
void Avtomation()
{
FILE* fp = NULL;
char* buf = malloc(sizeof(char)*BufLen);
if ((fp = fopen("string.txt", "r")) == NULL)
{
printf("I can`t open this file!");
return;
}
fgets(buf, BufLen, fp);
SRoot.ts = buf;
SRoot.c = &SRoot.ms[1];
if (Tree(SRoot) == 1) printf("Ice ;)");
else printf("Not Ice :(");
free(buf);
fclose(fp);
}
int main() //main function :)
{
FILE* fp = NULL;
char* buf = malloc(sizeof(char)*BufLen);
char ST[] = {-1, ' ', '\0'}; //My first stack
if ((fp = fopen("Grammatics.txt", "r")) == NULL)
{
printf("I can`t open this file!");
return -1;
}
while (fgets(buf, BufLen, fp) != NULL) //What is do?
{
unsigned int i, k;
if (ST[0] == -1)
{
ST[0] = 'h';
ST[1] = buf[0];
}
// This loop delete space
for (i = 0, k = 0; i < strlen(buf); i++)
if (buf[i] != ' ') buf[k++] = buf[i];
buf[k-1] = '\0';
printf("%s\n", buf); //printing rules :)
GramRuleParser(buf);
}
AddRule(0, st, 'h', 0, st); //Grammatic 4 exit
ShowRules(); //ShowRules
SRoot.ms = malloc(sizeof(char)*(strlen(ST) + 1));
strcpy(SRoot.ms, ST);
Avtomation();// This fuction start my avtomatic :)
// 40 k ought to be enough for anyone (c) :)
// Fucking Garbage Сollector :)))
fclose(fp);
free(buf);
return 0;
}