Ejemplo n.º 1
0
void grammar4Test(){
	//Gramatica4 = ({S, B, C}, {a,b,c}, S,
	//{S->B | a,
	//B->cS | \,
	//C->bC | \})

	GrammarADT grammar4 = newGrammar();
	grammar4 = newGrammar();
	setDistinguished(grammar4, 'S');
	char * nonterminals1 = malloc(sizeof(char)*3);
	nonterminals1[0] = 'S';
	nonterminals1[1] = 'B';
	nonterminals1[2] = 'C';
	setNonTerminals(grammar4,nonterminals1,3);
	char * term1 = malloc(sizeof(char)*3);
	term1[0] = 'a';
	term1[1] = 'b';
	term1[2] = 'c';
	setTerminals(grammar4,term1,3);

	ProductionADT prod1 = newProduction('S',LAMDA,'B');
	ProductionADT prod2 = newProduction('S','a',LAMDA);
	ProductionADT prod3 = newProduction('B','c','S');
	ProductionADT prod4 = newProduction('B',LAMDA, LAMDA);
	ProductionADT prod5 = newProduction('C', 'b', 'C');
	ProductionADT prod6 = newProduction('C',LAMDA, LAMDA);
	//ProductionsADT productions1 = newProductions(0);

	ProductionsADT productions1 = newProductions(6);
	setProduction(productions1,0,prod1);
	setProduction(productions1,1,prod2);
	setProduction(productions1,2,prod3);
	setProduction(productions1,3,prod4);
	setProduction(productions1,4,prod5);
	setProduction(productions1,5,prod6);

	/*addProduction(productions1,prod1);
	addProduction(productions1,prod2);
	addProduction(productions1,prod3);
	addProduction(productions1,prod4);*/

	setProductions(grammar4,productions1);


	printf("Before Nomalization\n");
	printGrammar(grammar4);
	/*Conversion*/
	AutomataADT automata1 = toAutomata(grammar4);
	printf("After Nomalization\n");
	printGrammar(grammar4);
	printAutomata(automata1);

}
Ejemplo n.º 2
0
void grammar1Test(){
	//G1 = ({A, B, C}, {a, b, c},A, {A -> aB|c, B -> aA|b})

	/*Initialization*/
	GrammarADT grammar1 = newGrammar();
	grammar1 = newGrammar();
	setDistinguished(grammar1, 'A');
	char * nonterminals1 = malloc(sizeof(char)*3);
	nonterminals1[0] = 'A';
	nonterminals1[1] = 'B';
	nonterminals1[2] = 'C';
	setNonTerminals(grammar1,nonterminals1,3);
	char * term1 = malloc(sizeof(char)*3);
	term1[0] = 'a';
	term1[1] = 'b';
	term1[2] = 'c';
	setTerminals(grammar1,term1,3);

	ProductionADT prod1 = newProduction('A','a','B');
	ProductionADT prod2 = newProduction('A','c',LAMDA);
	ProductionADT prod3 = newProduction('B','a','A');
	ProductionADT prod4 = newProduction('B',LAMDA,'b');
	//ProductionsADT productions1 = newProductions(0);

	ProductionsADT productions1 = newProductions(4);
	setProduction(productions1,0,prod1);
	setProduction(productions1,1,prod2);
	setProduction(productions1,2,prod3);
	setProduction(productions1,3,prod4);


	/*addProduction(productions1,prod1);
	addProduction(productions1,prod2);
	addProduction(productions1,prod3);
	addProduction(productions1,prod4);*/

	setProductions(grammar1,productions1);

	printf("Before Nomalization\n");
	printGrammar(grammar1);
	/*Conversion*/
	AutomataADT automata1 = toAutomata(grammar1);
	printf("After Nomalization\n");
	printGrammar(grammar1);
	printAutomata(automata1);


}
Ejemplo n.º 3
0
void init(){
	g1 = newGrammar();
	setDistinguished(g1, 'S');
	nonterminals = malloc(sizeof(char)*5);
	nonterminals[0] = 'S';
	nonterminals[1] = 'A';
	nonterminals[2] = 'B';
	nonterminals[3] = 'C';
	nonterminals[4] = 'D';
	setNonTerminals(g1,nonterminals,5);
	term = malloc(sizeof(char)*2);
	term[0] = 'a';
	term[1] = 'b';
	setTerminals(g1,term,2);

	/*S-> aA*/
	p1 = newProduction('S','A','b');
	/*A-> b*/
	p2 = newProduction('A','b',LAMDA);
	/*B-> aB*/
	p7 = newProduction('B','B','b');
	/*A-> bA*/
	p3 = newProduction('A','B','\\');
	/*A-> aB*/
	p8 = newProduction('A','B','a');
	/*B-> ab*/
	//p4 = newProduction('B','c','b');
	/*A-> bB*/
	//p5 = newProduction('A','b','B');
	/*A-> aB*/
	//p6 = newProduction('A',LAMDA,'B');


	prods = newProductions(5);
	setProduction(prods,0,p1);
	setProduction(prods,1,p2);
	setProduction(prods,2,p7);
	setProduction(prods,3,p3);
	setProduction(prods,4,p8);
//	setProduction(prods,5,p4);
//	setProduction(prods,6,p5);
//	setProduction(prods,7,p6);


	setProductions(g1,prods);
}
Ejemplo n.º 4
0
void convertToRight(GrammarADT grammar) {

    int i;
    int ml = FALSE;
    char oldistiguished = getDistinguished(grammar);
    /*if the grammar is already right there is no
     * reason to convert it*/
    if ( isRight(grammar) ) {
        return;
    }

    ProductionsADT productions = getProductions(grammar);
    int quantproductions = getQuant(productions);

    for(i = 0; i < quantproductions ; i++) {
        ProductionADT p1 = getProduction(productions, i);
        char first = getProductionComponent(p1, 0);
        char sec = getProductionComponent(p1, 1);
        char third = getProductionComponent(p1, 2);
        if(isNonTerminal(sec)) {
            addProduction(productions, newProduction(sec , third, first));
            removeParticularProduction(productions,p1);
        }
    }
    setProductions(grammar, productions);

    /*a new nonTerminal should be created ,
     * that joint the non terminals that were joined to lambda*/
    char * leftnontermssymbols = NULL;
    int size=0;
    for(i=0; i < quantproductions; i++) {
        ProductionADT p1 = getProduction(productions, i);
        char first = getProductionComponent(p1, 0);
        char sec = getProductionComponent(p1, 1);
        char third = getProductionComponent(p1, 2);
        if(sec == LAMDA && third == LAMDA) {
            addChar(&leftnontermssymbols,&size,first);
        }
    }
    /*get a new distiguished symbol*/
    char newsymbol = getNewSymbol(grammar);
    setDistinguished(grammar,newsymbol);
    /*generate new unitary productions*/
    for(i=0; i<size; i++) {
        ProductionADT newprod = newProduction(newsymbol,leftnontermssymbols[i],LAMDA);
        //printProduction(newprod);
        addProduction(productions, newprod);
    }
    /*remove all old lambda productions*/
    for(i=0; i<getQuant(productions); i++) {
        ProductionADT p = getProduction(productions,i);
        char sec = getProductionComponent(p,1);
        char third = getProductionComponent(p,2);
        /*if it is a lamda productions : delete*/
        if( sec == LAMDA && third == LAMDA ) {
            removeParticularProduction(productions,p);
        }
    }
    if(!ml) {
        addProduction(productions, newProduction(oldistiguished, LAMDA, LAMDA));
    }

    setProductions(grammar,productions);


    /*remove non terminals and terminals that are no longer there */
    actualizeTerminals(grammar);
    actualizeNonTerminals(grammar);
    actualizeProductions(grammar);
}
Ejemplo n.º 5
0
 LALR1::LALR1(const vector<Production>& productions, Production::Item& start)
     : begin("begin")
     , start(&start)
 {
     setProductions(productions);
 }