Parcourir la source

symbolTable in .leg

main
lquint il y a 3 ans
Parent
révision
192e8a0ca5
3 fichiers modifiés avec 492 ajouts et 107 suppressions
  1. BIN
      parse
  2. +328
    -95
      parse.c
  3. +164
    -12
      parse.leg

BIN
parse Voir le fichier


+ 328
- 95
parse.c Voir le fichier

@ -3,7 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define YYRULECOUNT 11
#define YYRULECOUNT 15
#line 1 "parse.leg"
@ -12,10 +12,17 @@
#include <stdlib.h>
#include "inputBuffer.c"
;
enum op { String, Query, Star, Plus, Or, And, Class, Dot, Exc } ;
enum op { String, Query, Star, Plus, Or, And, Class, Dot, Exc, Id } ;
typedef union Node Node;
typedef struct Symbol Symbol;
typedef struct Array Array;
typedef struct SymbolTable SymbolTable;
struct Array {
Node **elements;
int length;
};
struct String { enum op type; char *string; int len; };
struct Query { enum op type; Node *children[1]; };
@ -24,8 +31,9 @@ struct Plus { enum op type; Node *children[1]; };
struct Or { enum op type; Node *children[2]; };
struct And { enum op type; Node *children[2]; };
struct Class { enum op type; char *stringValue; int len; };
struct Dot { enum op type;};
struct Dot { enum op type; };
struct Exc { enum op type; Node *children[1]; };
struct Id { enum op type; Symbol *symbol; };
union Node {
enum op type;
@ -38,8 +46,30 @@ union Node {
struct Class Class;
struct Dot Dot;
struct Exc Exc;
struct Id Id;
};
struct Symbol{
char* name;
Node *rule;
};
struct SymbolTable {
Symbol **elements;
int length;
};
#define SymbolTable_initialiser {0,0}
SymbolTable symbolTable= SymbolTable_initialiser;
Symbol *createSymbol(char *name) {
Symbol *symbol= calloc(1, sizeof(Symbol));
symbol->name= strdup(name);
return symbol;
}
#define new(type) mkNode(sizeof(struct type),type)
@ -58,6 +88,15 @@ Node *mkString(char *s)
return node;
}
Node *mkId(char *s){
Node *node= new(Id);
node->Id.symbol=createSymbol(s);
return node;
}
Node *mkQuery(Node *n)
{
Node *node= new(Query);
@ -116,6 +155,16 @@ Node *mkExc(Node *n)
return node;
}
Node *_checktype(Node *object, enum op type)
{
if (object->type == type) return object;
fprintf(stderr, "\naccesing type %i as if it were a %i\n", object->type, type);
exit(1);
return 0;
}
#define get(object, type, member) (_checktype(object, type)->type.member)
void print(Node *node)
{
switch (node->type) {
@ -154,6 +203,9 @@ void print(Node *node)
printf("!");
print(node->Exc.children[0]);
return;
case Id:
printf("%s\n",get(node,Id,symbol)->name);
return;
}
abort();
}
@ -175,6 +227,102 @@ else { \
result=1; \
}}
void move(SymbolTable *symbolTable, int a) {
symbolTable->length++;
symbolTable->elements= realloc(symbolTable->elements,sizeof(Node*) * symbolTable->length);
printf("begin\n");
for (int i= symbolTable->length-1; i >a; i--) {
symbolTable->elements[i] = symbolTable->elements[i-1];
printf("\n");
for(int i= 0; i<symbolTable->length; i++) {
printf("number : %i, name : %s, value : a\n",i, symbolTable->elements[i]->name);
}
}
printf("end\n");
}
void addSymbolSorted(SymbolTable *symbolTable, Symbol *symbol) {
int a=0;
int b= symbolTable->length-1;
int c;
//1 <b-a => no infinite loop with a and c = 0 et b =1
while (1 < b - a ) {
c= (a+b)/2;
if (strcmp(symbolTable->elements[c]->name, symbol->name) < 0) {
a= c;
} else {
b= c;
}
}
//b>-1 <=> symboleTable->length > 0
// this if is here to know if we put the symbol before or after a
if (b>-1 && strcmp(symbolTable->elements[a]->name, symbol->name) < 0) {
a++;
}
//we move all the symbols after a
move(symbolTable, a);
//we put the symbol at the right place
symbolTable->elements[a]->name = symbol->name;
}
int findSymbol(SymbolTable *symbolTable, char *name) {
for (int i= 0; i < symbolTable->length;i++) {
if (strcmp(symbolTable->elements[i]->name, name) == 0) {
return 1;
}
}
Symbol *symbol = createSymbol(name);
addSymbolSorted(symbolTable, symbol);
return 0;
}
Node *findSymbolSorted(SymbolTable *symbolTable, char *name) {
int a=0;
int b= symbolTable->length-1;
int c;
//1 <b-a => no infinite loop with a and c = 0 et b =1
while (1 < b - a ) {
c= (a+b)/2;
if (strcmp(symbolTable->elements[c]->name, name) < 0) {
a= c;
} else {
if (strcmp(symbolTable->elements[c]->name, name) > 0) {
b= c;
} else {
return symbolTable->elements[c]->rule;
}
}
}
//b>-1 <=> symboleTable->length > 0
// this if is here to know if we put the symbol before or after a
if (b>-1 && strcmp(symbolTable->elements[a]->name, name) < 0) {
a++;
}
//we move all the symbols after a
move(symbolTable, a);
//we put the symbol at the right place
symbolTable->elements[a]= createSymbol(name);
return 0;
}
void setRule(SymbolTable *symbolTable, char *name, Node *rule) {
for (int i= 0; i < symbolTable->length;i++) {
if (strcmp(symbolTable->elements[i]->name, name) == 0) {
symbolTable->elements[i]->rule= rule;
break;
}
}
}
void arrayAppend(Array *array, Node *statement) {
array->length++;
array->elements= realloc(array->elements,sizeof(Node*) * array->length);
array->elements[array->length-1] = statement;
}
#define YYSTYPE Node *
YYSTYPE yylval = 0;
@ -447,16 +595,20 @@ YY_LOCAL(void) yySet(yycontext *yy, char *text, int count) { yy->__val[count]=
#define YYACCEPT yyAccept(yy, yythunkpos0)
YY_RULE(int) yy_space(yycontext *yy); /* 11 */
YY_RULE(int) yy_dot(yycontext *yy); /* 10 */
YY_RULE(int) yy_class(yycontext *yy); /* 9 */
YY_RULE(int) yy_string(yycontext *yy); /* 8 */
YY_RULE(int) yy_atom(yycontext *yy); /* 7 */
YY_RULE(int) yy_postfix(yycontext *yy); /* 6 */
YY_RULE(int) yy_prefix(yycontext *yy); /* 5 */
YY_RULE(int) yy_and(yycontext *yy); /* 4 */
YY_RULE(int) yy_or(yycontext *yy); /* 3 */
YY_RULE(int) yy__(yycontext *yy); /* 2 */
YY_RULE(int) yy_space(yycontext *yy); /* 15 */
YY_RULE(int) yy_rule(yycontext *yy); /* 14 */
YY_RULE(int) yy_dot(yycontext *yy); /* 13 */
YY_RULE(int) yy_class(yycontext *yy); /* 12 */
YY_RULE(int) yy_string(yycontext *yy); /* 11 */
YY_RULE(int) yy_atom(yycontext *yy); /* 10 */
YY_RULE(int) yy_postfix(yycontext *yy); /* 9 */
YY_RULE(int) yy_prefix(yycontext *yy); /* 8 */
YY_RULE(int) yy_and(yycontext *yy); /* 7 */
YY_RULE(int) yy_or(yycontext *yy); /* 6 */
YY_RULE(int) yy_expression(yycontext *yy); /* 5 */
YY_RULE(int) yy__(yycontext *yy); /* 4 */
YY_RULE(int) yy_id(yycontext *yy); /* 3 */
YY_RULE(int) yy_declaration(yycontext *yy); /* 2 */
YY_RULE(int) yy_start(yycontext *yy); /* 1 */
YY_ACTION(void) yy_1_dot(yycontext *yy, char *yytext, int yyleng)
@ -466,7 +618,7 @@ YY_ACTION(void) yy_1_dot(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_dot\n"));
{
#line 198
#line 352
__=mkDot();
}
#undef yythunkpos
@ -480,7 +632,7 @@ YY_ACTION(void) yy_1_class(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_class\n"));
{
#line 196
#line 350
__=mkClass(yytext) ;
}
#undef yythunkpos
@ -494,13 +646,43 @@ YY_ACTION(void) yy_1_string(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_string\n"));
{
#line 194
#line 348
__ = mkString(yytext) ;
}
#undef yythunkpos
#undef yypos
#undef yy
}
YY_ACTION(void) yy_1_id(yycontext *yy, char *yytext, int yyleng)
{
#define __ yy->__
#define yypos yy->__pos
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_id\n"));
{
#line 346
__ = mkId(yytext) ;
}
#undef yythunkpos
#undef yypos
#undef yy
}
YY_ACTION(void) yy_1_rule(yycontext *yy, char *yytext, int yyleng)
{
#define i yy->__val[-1]
#define __ yy->__
#define yypos yy->__pos
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_rule\n"));
{
#line 344
__ = mkId(yytext) ;
}
#undef yythunkpos
#undef yypos
#undef yy
#undef i
}
YY_ACTION(void) yy_4_postfix(yycontext *yy, char *yytext, int yyleng)
{
#define s yy->__val[-1]
@ -509,7 +691,7 @@ YY_ACTION(void) yy_4_postfix(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_4_postfix\n"));
{
#line 190
#line 340
__ = s;
}
#undef yythunkpos
@ -525,7 +707,7 @@ YY_ACTION(void) yy_3_postfix(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_3_postfix\n"));
{
#line 189
#line 339
s = mkPlus(s) ;
}
#undef yythunkpos
@ -541,7 +723,7 @@ YY_ACTION(void) yy_2_postfix(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_2_postfix\n"));
{
#line 188
#line 338
s = mkStar(s) ;
}
#undef yythunkpos
@ -557,7 +739,7 @@ YY_ACTION(void) yy_1_postfix(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_postfix\n"));
{
#line 187
#line 337
s = mkQuery(s) ;
}
#undef yythunkpos
@ -573,7 +755,7 @@ YY_ACTION(void) yy_2_prefix(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_2_prefix\n"));
{
#line 185
#line 335
__ = p;
}
#undef yythunkpos
@ -589,7 +771,7 @@ YY_ACTION(void) yy_1_prefix(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_prefix\n"));
{
#line 184
#line 334
__ = mkExc(p);
}
#undef yythunkpos
@ -606,7 +788,7 @@ YY_ACTION(void) yy_2_and(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_2_and\n"));
{
#line 182
#line 332
__ = p ;
}
#undef yythunkpos
@ -624,7 +806,7 @@ YY_ACTION(void) yy_1_and(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_and\n"));
{
#line 181
#line 331
__ = mkAnd(p, a); ;
}
#undef yythunkpos
@ -642,7 +824,7 @@ YY_ACTION(void) yy_2_or(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_2_or\n"));
{
#line 179
#line 329
__ = a ;
}
#undef yythunkpos
@ -660,7 +842,7 @@ YY_ACTION(void) yy_1_or(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_or\n"));
{
#line 178
#line 328
__ = mkOr(o, a) ;
}
#undef yythunkpos
@ -669,21 +851,23 @@ YY_ACTION(void) yy_1_or(yycontext *yy, char *yytext, int yyleng)
#undef o
#undef a
}
YY_ACTION(void) yy_1_start(yycontext *yy, char *yytext, int yyleng)
YY_ACTION(void) yy_1_declaration(yycontext *yy, char *yytext, int yyleng)
{
#define o yy->__val[-1]
#define e yy->__val[-1]
#define i yy->__val[-2]
#define __ yy->__
#define yypos yy->__pos
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_start\n"));
yyprintf((stderr, "do yy_1_declaration\n"));
{
#line 176
yylval = o ;
#line 326
setRule(&symbolTable,get(i, Id, symbol)->name, e) ;
}
#undef yythunkpos
#undef yypos
#undef yy
#undef o
#undef e
#undef i
}
YY_RULE(int) yy_space(yycontext *yy)
@ -708,155 +892,206 @@ YY_RULE(int) yy_space(yycontext *yy)
yyprintf((stderr, " fail %s @ %s\n", "space", yy->__buf+yy->__pos));
return 0;
}
YY_RULE(int) yy_rule(yycontext *yy)
{ int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyDo(yy, yyPush, 1, 0);
yyprintf((stderr, "%s\n", "rule")); if (!yy_id(yy)) goto l9; yyDo(yy, yySet, -1, 0);
{ int yypos10= yy->__pos, yythunkpos10= yy->__thunkpos; if (!yymatchChar(yy, '=')) goto l10; goto l9;
l10:; yy->__pos= yypos10; yy->__thunkpos= yythunkpos10;
} yyDo(yy, yy_1_rule, yy->__begin, yy->__end);
yyprintf((stderr, " ok %s @ %s\n", "rule", yy->__buf+yy->__pos)); yyDo(yy, yyPop, 1, 0);
return 1;
l9:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "rule", yy->__buf+yy->__pos));
return 0;
}
YY_RULE(int) yy_dot(yycontext *yy)
{ int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos;
yyprintf((stderr, "%s\n", "dot")); if (!yy__(yy)) goto l9; if (!yymatchChar(yy, '.')) goto l9; if (!yy__(yy)) goto l9; yyDo(yy, yy_1_dot, yy->__begin, yy->__end); if (!yy__(yy)) goto l9;
yyprintf((stderr, "%s\n", "dot")); if (!yy__(yy)) goto l11; if (!yymatchChar(yy, '.')) goto l11; if (!yy__(yy)) goto l11; yyDo(yy, yy_1_dot, yy->__begin, yy->__end); if (!yy__(yy)) goto l11;
yyprintf((stderr, " ok %s @ %s\n", "dot", yy->__buf+yy->__pos));
return 1;
l9:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
l11:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "dot", yy->__buf+yy->__pos));
return 0;
}
YY_RULE(int) yy_class(yycontext *yy)
{ int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos;
yyprintf((stderr, "%s\n", "class")); if (!yymatchChar(yy, '[')) goto l10; if (!yy__(yy)) goto l10; yyText(yy, yy->__begin, yy->__end); {
yyprintf((stderr, "%s\n", "class")); if (!yymatchChar(yy, '[')) goto l12; if (!yy__(yy)) goto l12; yyText(yy, yy->__begin, yy->__end); {
#define yytext yy->__text
#define yyleng yy->__textlen
if (!(YY_BEGIN)) goto l10;
if (!(YY_BEGIN)) goto l12;
#undef yytext
#undef yyleng
}
l11:;
{ int yypos12= yy->__pos, yythunkpos12= yy->__thunkpos;
{ int yypos13= yy->__pos, yythunkpos13= yy->__thunkpos; if (!yymatchChar(yy, ']')) goto l13; goto l12;
l13:; yy->__pos= yypos13; yy->__thunkpos= yythunkpos13;
} if (!yy_string(yy)) goto l12; goto l11;
l12:; yy->__pos= yypos12; yy->__thunkpos= yythunkpos12;
l13:;
{ int yypos14= yy->__pos, yythunkpos14= yy->__thunkpos;
{ int yypos15= yy->__pos, yythunkpos15= yy->__thunkpos; if (!yymatchChar(yy, ']')) goto l15; goto l14;
l15:; yy->__pos= yypos15; yy->__thunkpos= yythunkpos15;
} if (!yy_string(yy)) goto l14; goto l13;
l14:; yy->__pos= yypos14; yy->__thunkpos= yythunkpos14;
} yyText(yy, yy->__begin, yy->__end); {
#define yytext yy->__text
#define yyleng yy->__textlen
if (!(YY_END)) goto l10;
if (!(YY_END)) goto l12;
#undef yytext
#undef yyleng
} if (!yymatchChar(yy, ']')) goto l10; yyDo(yy, yy_1_class, yy->__begin, yy->__end); if (!yy__(yy)) goto l10;
} if (!yymatchChar(yy, ']')) goto l12; yyDo(yy, yy_1_class, yy->__begin, yy->__end); if (!yy__(yy)) goto l12;
yyprintf((stderr, " ok %s @ %s\n", "class", yy->__buf+yy->__pos));
return 1;
l10:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
l12:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "class", yy->__buf+yy->__pos));
return 0;
}
YY_RULE(int) yy_string(yycontext *yy)
{ int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos;
yyprintf((stderr, "%s\n", "string")); if (!yymatchChar(yy, '"')) goto l14; yyText(yy, yy->__begin, yy->__end); {
yyprintf((stderr, "%s\n", "string")); if (!yymatchChar(yy, '"')) goto l16; yyText(yy, yy->__begin, yy->__end); {
#define yytext yy->__text
#define yyleng yy->__textlen
if (!(YY_BEGIN)) goto l14;
if (!(YY_BEGIN)) goto l16;
#undef yytext
#undef yyleng
}
l15:;
{ int yypos16= yy->__pos, yythunkpos16= yy->__thunkpos; if (!yymatchClass(yy, (unsigned char *)"\377\377\377\377\373\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377")) goto l16; goto l15;
l16:; yy->__pos= yypos16; yy->__thunkpos= yythunkpos16;
l17:;
{ int yypos18= yy->__pos, yythunkpos18= yy->__thunkpos; if (!yymatchClass(yy, (unsigned char *)"\377\377\377\377\373\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377")) goto l18; goto l17;
l18:; yy->__pos= yypos18; yy->__thunkpos= yythunkpos18;
} yyText(yy, yy->__begin, yy->__end); {
#define yytext yy->__text
#define yyleng yy->__textlen
if (!(YY_END)) goto l14;
if (!(YY_END)) goto l16;
#undef yytext
#undef yyleng
} if (!yymatchChar(yy, '"')) goto l14; yyDo(yy, yy_1_string, yy->__begin, yy->__end); if (!yy__(yy)) goto l14;
} if (!yymatchChar(yy, '"')) goto l16; yyDo(yy, yy_1_string, yy->__begin, yy->__end); if (!yy__(yy)) goto l16;
yyprintf((stderr, " ok %s @ %s\n", "string", yy->__buf+yy->__pos));
return 1;
l14:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
l16:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "string", yy->__buf+yy->__pos));
return 0;
}
YY_RULE(int) yy_atom(yycontext *yy)
{ int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos;
yyprintf((stderr, "%s\n", "atom"));
{ int yypos18= yy->__pos, yythunkpos18= yy->__thunkpos; if (!yy_string(yy)) goto l19; goto l18;
l19:; yy->__pos= yypos18; yy->__thunkpos= yythunkpos18; if (!yy_class(yy)) goto l20; goto l18;
l20:; yy->__pos= yypos18; yy->__thunkpos= yythunkpos18; if (!yy_dot(yy)) goto l17;
{ int yypos20= yy->__pos, yythunkpos20= yy->__thunkpos; if (!yy_string(yy)) goto l21; goto l20;
l21:; yy->__pos= yypos20; yy->__thunkpos= yythunkpos20; if (!yy_class(yy)) goto l22; goto l20;
l22:; yy->__pos= yypos20; yy->__thunkpos= yythunkpos20; if (!yy_dot(yy)) goto l23; goto l20;
l23:; yy->__pos= yypos20; yy->__thunkpos= yythunkpos20; if (!yy_rule(yy)) goto l19;
}
l18:;
l20:;
yyprintf((stderr, " ok %s @ %s\n", "atom", yy->__buf+yy->__pos));
return 1;
l17:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
l19:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "atom", yy->__buf+yy->__pos));
return 0;
}
YY_RULE(int) yy_postfix(yycontext *yy)
{ int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyDo(yy, yyPush, 1, 0);
yyprintf((stderr, "%s\n", "postfix")); if (!yy_atom(yy)) goto l21; yyDo(yy, yySet, -1, 0);
{ int yypos22= yy->__pos, yythunkpos22= yy->__thunkpos;
{ int yypos24= yy->__pos, yythunkpos24= yy->__thunkpos; if (!yymatchChar(yy, '?')) goto l25; if (!yy__(yy)) goto l25; yyDo(yy, yy_1_postfix, yy->__begin, yy->__end); goto l24;
l25:; yy->__pos= yypos24; yy->__thunkpos= yythunkpos24; if (!yymatchChar(yy, '*')) goto l26; if (!yy__(yy)) goto l26; yyDo(yy, yy_2_postfix, yy->__begin, yy->__end); goto l24;
l26:; yy->__pos= yypos24; yy->__thunkpos= yythunkpos24; if (!yymatchChar(yy, '+')) goto l22; if (!yy__(yy)) goto l22; yyDo(yy, yy_3_postfix, yy->__begin, yy->__end);
yyprintf((stderr, "%s\n", "postfix")); if (!yy_atom(yy)) goto l24; yyDo(yy, yySet, -1, 0);
{ int yypos25= yy->__pos, yythunkpos25= yy->__thunkpos;
{ int yypos27= yy->__pos, yythunkpos27= yy->__thunkpos; if (!yymatchChar(yy, '?')) goto l28; if (!yy__(yy)) goto l28; yyDo(yy, yy_1_postfix, yy->__begin, yy->__end); goto l27;
l28:; yy->__pos= yypos27; yy->__thunkpos= yythunkpos27; if (!yymatchChar(yy, '*')) goto l29; if (!yy__(yy)) goto l29; yyDo(yy, yy_2_postfix, yy->__begin, yy->__end); goto l27;
l29:; yy->__pos= yypos27; yy->__thunkpos= yythunkpos27; if (!yymatchChar(yy, '+')) goto l25; if (!yy__(yy)) goto l25; yyDo(yy, yy_3_postfix, yy->__begin, yy->__end);
}
l24:; goto l23;
l22:; yy->__pos= yypos22; yy->__thunkpos= yythunkpos22;
l27:; goto l26;
l25:; yy->__pos= yypos25; yy->__thunkpos= yythunkpos25;
}
l23:; yyDo(yy, yy_4_postfix, yy->__begin, yy->__end);
l26:; yyDo(yy, yy_4_postfix, yy->__begin, yy->__end);
yyprintf((stderr, " ok %s @ %s\n", "postfix", yy->__buf+yy->__pos)); yyDo(yy, yyPop, 1, 0);
return 1;
l21:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
l24:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "postfix", yy->__buf+yy->__pos));
return 0;
}
YY_RULE(int) yy_prefix(yycontext *yy)
{ int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyDo(yy, yyPush, 1, 0);
yyprintf((stderr, "%s\n", "prefix"));
{ int yypos28= yy->__pos, yythunkpos28= yy->__thunkpos; if (!yymatchChar(yy, '!')) goto l29; if (!yy__(yy)) goto l29; if (!yy_postfix(yy)) goto l29; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_1_prefix, yy->__begin, yy->__end); goto l28;
l29:; yy->__pos= yypos28; yy->__thunkpos= yythunkpos28; if (!yy_postfix(yy)) goto l27; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_2_prefix, yy->__begin, yy->__end);
{ int yypos31= yy->__pos, yythunkpos31= yy->__thunkpos; if (!yymatchChar(yy, '!')) goto l32; if (!yy__(yy)) goto l32; if (!yy_postfix(yy)) goto l32; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_1_prefix, yy->__begin, yy->__end); goto l31;
l32:; yy->__pos= yypos31; yy->__thunkpos= yythunkpos31; if (!yy_postfix(yy)) goto l30; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_2_prefix, yy->__begin, yy->__end);
}
l28:;
l31:;
yyprintf((stderr, " ok %s @ %s\n", "prefix", yy->__buf+yy->__pos)); yyDo(yy, yyPop, 1, 0);
return 1;
l27:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
l30:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "prefix", yy->__buf+yy->__pos));
return 0;
}
YY_RULE(int) yy_and(yycontext *yy)
{ int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyDo(yy, yyPush, 2, 0);
yyprintf((stderr, "%s\n", "and"));
{ int yypos31= yy->__pos, yythunkpos31= yy->__thunkpos; if (!yy_prefix(yy)) goto l32; yyDo(yy, yySet, -2, 0); if (!yy_and(yy)) goto l32; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_1_and, yy->__begin, yy->__end); goto l31;
l32:; yy->__pos= yypos31; yy->__thunkpos= yythunkpos31; if (!yy_prefix(yy)) goto l30; yyDo(yy, yySet, -2, 0); yyDo(yy, yy_2_and, yy->__begin, yy->__end);
{ int yypos34= yy->__pos, yythunkpos34= yy->__thunkpos; if (!yy_prefix(yy)) goto l35; yyDo(yy, yySet, -2, 0); if (!yy_and(yy)) goto l35; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_1_and, yy->__begin, yy->__end); goto l34;
l35:; yy->__pos= yypos34; yy->__thunkpos= yythunkpos34; if (!yy_prefix(yy)) goto l33; yyDo(yy, yySet, -2, 0); yyDo(yy, yy_2_and, yy->__begin, yy->__end);
}
l31:;
l34:;
yyprintf((stderr, " ok %s @ %s\n", "and", yy->__buf+yy->__pos)); yyDo(yy, yyPop, 2, 0);
return 1;
l30:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
l33:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "and", yy->__buf+yy->__pos));
return 0;
}
YY_RULE(int) yy_or(yycontext *yy)
{ int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyDo(yy, yyPush, 2, 0);
yyprintf((stderr, "%s\n", "or"));
{ int yypos34= yy->__pos, yythunkpos34= yy->__thunkpos; if (!yy_and(yy)) goto l35; yyDo(yy, yySet, -2, 0); if (!yy__(yy)) goto l35; if (!yymatchChar(yy, '|')) goto l35; if (!yy__(yy)) goto l35; if (!yy_or(yy)) goto l35; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_1_or, yy->__begin, yy->__end); goto l34;
l35:; yy->__pos= yypos34; yy->__thunkpos= yythunkpos34; if (!yy_and(yy)) goto l33; yyDo(yy, yySet, -2, 0); yyDo(yy, yy_2_or, yy->__begin, yy->__end);
{ int yypos37= yy->__pos, yythunkpos37= yy->__thunkpos; if (!yy_and(yy)) goto l38; yyDo(yy, yySet, -2, 0); if (!yy__(yy)) goto l38; if (!yymatchChar(yy, '|')) goto l38; if (!yy__(yy)) goto l38; if (!yy_or(yy)) goto l38; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_1_or, yy->__begin, yy->__end); goto l37;
l38:; yy->__pos= yypos37; yy->__thunkpos= yythunkpos37; if (!yy_and(yy)) goto l36; yyDo(yy, yySet, -2, 0); yyDo(yy, yy_2_or, yy->__begin, yy->__end);
}
l34:;
l37:;
yyprintf((stderr, " ok %s @ %s\n", "or", yy->__buf+yy->__pos)); yyDo(yy, yyPop, 2, 0);
return 1;
l33:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
l36:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "or", yy->__buf+yy->__pos));
return 0;
}
YY_RULE(int) yy__(yycontext *yy)
{
yyprintf((stderr, "%s\n", "_"));
l37:;
{ int yypos38= yy->__pos, yythunkpos38= yy->__thunkpos; if (!yy_space(yy)) goto l38; goto l37;
l38:; yy->__pos= yypos38; yy->__thunkpos= yythunkpos38;
l40:;
{ int yypos41= yy->__pos, yythunkpos41= yy->__thunkpos; if (!yy_space(yy)) goto l41; goto l40;
l41:; yy->__pos= yypos41; yy->__thunkpos= yythunkpos41;
}
yyprintf((stderr, " ok %s @ %s\n", "_", yy->__buf+yy->__pos));
return 1;
}
YY_RULE(int) yy_id(yycontext *yy)
{ int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos;
yyprintf((stderr, "%s\n", "id")); yyText(yy, yy->__begin, yy->__end); {
#define yytext yy->__text
#define yyleng yy->__textlen
if (!(YY_BEGIN)) goto l42;
#undef yytext
#undef yyleng
} if (!yymatchClass(yy, (unsigned char *)"\000\000\000\000\000\000\000\000\376\377\377\377\377\377\377\007\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")) goto l42;
l43:;
{ int yypos44= yy->__pos, yythunkpos44= yy->__thunkpos; if (!yymatchClass(yy, (unsigned char *)"\000\000\000\000\000\000\377\003\376\377\377\377\377\377\377\007\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")) goto l44; goto l43;
l44:; yy->__pos= yypos44; yy->__thunkpos= yythunkpos44;
} yyText(yy, yy->__begin, yy->__end); {
#define yytext yy->__text
#define yyleng yy->__textlen
if (!(YY_END)) goto l42;
#undef yytext
#undef yyleng
} if (!yy__(yy)) goto l42; yyDo(yy, yy_1_id, yy->__begin, yy->__end);
yyprintf((stderr, " ok %s @ %s\n", "id", yy->__buf+yy->__pos));
return 1;
l42:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "id", yy->__buf+yy->__pos));
return 0;
}
YY_RULE(int) yy_declaration(yycontext *yy)
{ int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyDo(yy, yyPush, 2, 0);
yyprintf((stderr, "%s\n", "declaration")); if (!yy_id(yy)) goto l45; yyDo(yy, yySet, -2, 0); if (!yymatchChar(yy, '=')) goto l45; if (!yy__(yy)) goto l45; if (!yy_expression(yy)) goto l45; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_1_declaration, yy->__begin, yy->__end);
yyprintf((stderr, " ok %s @ %s\n", "declaration", yy->__buf+yy->__pos)); yyDo(yy, yyPop, 2, 0);
return 1;
l45:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "declaration", yy->__buf+yy->__pos));
return 0;
}
YY_RULE(int) yy_start(yycontext *yy)
{ int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyDo(yy, yyPush, 1, 0);
yyprintf((stderr, "%s\n", "start")); if (!yy__(yy)) goto l39; if (!yy_or(yy)) goto l39; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_1_start, yy->__begin, yy->__end);
yyprintf((stderr, " ok %s @ %s\n", "start", yy->__buf+yy->__pos)); yyDo(yy, yyPop, 1, 0);
{ int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos;
yyprintf((stderr, "%s\n", "start")); if (!yy_declaration(yy)) goto l46;
l47:;
{ int yypos48= yy->__pos, yythunkpos48= yy->__thunkpos; if (!yy_declaration(yy)) goto l48; goto l47;
l48:; yy->__pos= yypos48; yy->__thunkpos= yythunkpos48;
}
yyprintf((stderr, " ok %s @ %s\n", "start", yy->__buf+yy->__pos));
return 1;
l39:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
l46:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "start", yy->__buf+yy->__pos));
return 0;
}
@ -908,18 +1143,12 @@ YY_PARSE(yycontext *) YYRELEASE(yycontext *yyctx)
}
#endif
#line 205 "parse.leg"
#line 359 "parse.leg"
Node *_checktype(Node *object, enum op type)
{
if (object->type == type) return object;
fprintf(stderr, "\naccesing type %i as if it were a %i\n", object->type, type);
exit(1);
return 0;
}
#define get(object, type, member) (_checktype(object, type)->type.member)
int execute(Node *node, InputBuffer *in)
{
@ -989,6 +1218,10 @@ int execute(Node *node, InputBuffer *in)
advance(in, 1);
return 1;
}
case Id: {
Symbol *symbol= get(node, Id, symbol);
return execute(symbol->rule, in);
}
}
printf("this cannot happen\n");
abort();

+ 164
- 12
parse.leg Voir le fichier

@ -5,10 +5,17 @@
#include <stdlib.h>
#include "inputBuffer.c"
;
enum op { String, Query, Star, Plus, Or, And, Class, Dot, Exc } ;
enum op { String, Query, Star, Plus, Or, And, Class, Dot, Exc, Id } ;
typedef union Node Node;
typedef struct Symbol Symbol;
typedef struct Array Array;
typedef struct SymbolTable SymbolTable;
struct Array {
Node **elements;
int length;
};
struct String { enum op type; char *string; int len; };
struct Query { enum op type; Node *children[1]; };
@ -17,8 +24,9 @@ struct Plus { enum op type; Node *children[1]; };
struct Or { enum op type; Node *children[2]; };
struct And { enum op type; Node *children[2]; };
struct Class { enum op type; char *stringValue; int len; };
struct Dot { enum op type;};
struct Dot { enum op type; };
struct Exc { enum op type; Node *children[1]; };
struct Id { enum op type; Symbol *symbol; };
union Node {
enum op type;
@ -31,8 +39,30 @@ union Node {
struct Class Class;
struct Dot Dot;
struct Exc Exc;
struct Id Id;
};
struct Symbol{
char* name;
Node *rule;
};
struct SymbolTable {
Symbol **elements;
int length;
};
#define SymbolTable_initialiser {0,0}
SymbolTable symbolTable= SymbolTable_initialiser;
Symbol *createSymbol(char *name) {
Symbol *symbol= calloc(1, sizeof(Symbol));
symbol->name= strdup(name);
return symbol;
}
#define new(type) mkNode(sizeof(struct type),type)
@ -51,6 +81,15 @@ Node *mkString(char *s)
return node;
}
Node *mkId(char *s){
Node *node= new(Id);
node->Id.symbol=createSymbol(s);
return node;
}
Node *mkQuery(Node *n)
{
Node *node= new(Query);
@ -109,6 +148,16 @@ Node *mkExc(Node *n)
return node;
}
Node *_checktype(Node *object, enum op type)
{
if (object->type == type) return object;
fprintf(stderr, "\naccesing type %i as if it were a %i\n", object->type, type);
exit(1);
return 0;
}
#define get(object, type, member) (_checktype(object, type)->type.member)
void print(Node *node)
{
switch (node->type) {
@ -147,6 +196,9 @@ void print(Node *node)
printf("!");
print(node->Exc.children[0]);
return;
case Id:
printf("%s\n",get(node,Id,symbol)->name);
return;
}
abort();
}
@ -168,12 +220,110 @@ else { \
result=1; \
}}
void move(SymbolTable *symbolTable, int a) {
symbolTable->length++;
symbolTable->elements= realloc(symbolTable->elements,sizeof(Node*) * symbolTable->length);
printf("begin\n");
for (int i= symbolTable->length-1; i >a; i--) {
symbolTable->elements[i] = symbolTable->elements[i-1];
printf("\n");
for(int i= 0; i<symbolTable->length; i++) {
printf("number : %i, name : %s, value : a\n",i, symbolTable->elements[i]->name);
}
}
printf("end\n");
}
void addSymbolSorted(SymbolTable *symbolTable, Symbol *symbol) {
int a=0;
int b= symbolTable->length-1;
int c;
//1 <b-a => no infinite loop with a and c = 0 et b =1
while (1 < b - a ) {
c= (a+b)/2;
if (strcmp(symbolTable->elements[c]->name, symbol->name) < 0) {
a= c;
} else {
b= c;
}
}
//b>-1 <=> symboleTable->length > 0
// this if is here to know if we put the symbol before or after a
if (b>-1 && strcmp(symbolTable->elements[a]->name, symbol->name) < 0) {
a++;
}
//we move all the symbols after a
move(symbolTable, a);
//we put the symbol at the right place
symbolTable->elements[a]->name = symbol->name;
}
int findSymbol(SymbolTable *symbolTable, char *name) {
for (int i= 0; i < symbolTable->length;i++) {
if (strcmp(symbolTable->elements[i]->name, name) == 0) {
return 1;
}
}
Symbol *symbol = createSymbol(name);
addSymbolSorted(symbolTable, symbol);
return 0;
}
Node *findSymbolSorted(SymbolTable *symbolTable, char *name) {
int a=0;
int b= symbolTable->length-1;
int c;
//1 <b-a => no infinite loop with a and c = 0 et b =1
while (1 < b - a ) {
c= (a+b)/2;
if (strcmp(symbolTable->elements[c]->name, name) < 0) {
a= c;
} else {
if (strcmp(symbolTable->elements[c]->name, name) > 0) {
b= c;
} else {
return symbolTable->elements[c]->rule;
}
}
}
//b>-1 <=> symboleTable->length > 0
// this if is here to know if we put the symbol before or after a
if (b>-1 && strcmp(symbolTable->elements[a]->name, name) < 0) {
a++;
}
//we move all the symbols after a
move(symbolTable, a);
//we put the symbol at the right place
symbolTable->elements[a]= createSymbol(name);
return 0;
}
void setRule(SymbolTable *symbolTable, char *name, Node *rule) {
for (int i= 0; i < symbolTable->length;i++) {
if (strcmp(symbolTable->elements[i]->name, name) == 0) {
symbolTable->elements[i]->rule= rule;
break;
}
}
}
void arrayAppend(Array *array, Node *statement) {
array->length++;
array->elements= realloc(array->elements,sizeof(Node*) * array->length);
array->elements[array->length-1] = statement;
}
#define YYSTYPE Node *
YYSTYPE yylval = 0;
%}
start = - o:or { yylval = o }
start = declaration+
declaration = i:id '=' - e:expression { setRule(&symbolTable,get(i, Id, symbol)->name, e) }
or = a:and - "|" - o:or {$$ = mkOr(o, a) }
| a:and { $$ = a }
@ -189,7 +339,11 @@ postfix = s:atom ( "?" - { s = mkQuery(s) }
| "+" - { s = mkPlus(s) }
)? { $$ = s}
atom = string | class | dot
atom = string | class | dot | rule
rule = i:id !'=' { $$ = mkId(yytext) }
id = < [a-zA-z_][a-zA-z_0-9]* > - { $$ = mkId(yytext) }
string = '"' < [^"]* > '"' { $$ = mkString(yytext) } -
@ -204,15 +358,9 @@ space = [ \t] | '\n' '\r'* | '\r' '\n'*
%%
Node *_checktype(Node *object, enum op type)
{
if (object->type == type) return object;
fprintf(stderr, "\naccesing type %i as if it were a %i\n", object->type, type);
exit(1);
return 0;
}
#define get(object, type, member) (_checktype(object, type)->type.member)
int execute(Node *node, InputBuffer *in)
{
@ -282,6 +430,10 @@ int execute(Node *node, InputBuffer *in)
advance(in, 1);
return 1;
}
case Id: {
Symbol *symbol= get(node, Id, symbol);
return execute(symbol->rule, in);
}
}
printf("this cannot happen\n");
abort();

Chargement…
Annuler
Enregistrer