Hi,
 My name is David Ja, an undergraduate student at university of 
pennsylvania. I am attempting to start a minor research project myself. I 
been trying to adapted bison to my own purpose but after a month of trying I 
can't seem to figure out a way to make it work. All I wanted to do was to 
take advantage of bison's parser which generates the DFA, which in turn, 
allows me to step through the DFA in sequence generating a sample string 
that fits the grammar input. For example if the grammar is the grammar for 
an web address. My program will be able to generate
http://www.abc.org<http://www.abc.org>.
I have attached my "attempt" code with this email. The problem I arrive at 
is I don't seem to be able to compile it with bison code. For some reason, 
even if I leave it in the same folder as bison src, I still arrive at a 
deference to pointer error. I'm sorry if I made an elementary mistake or 
something stupid. Thank you for any help/suggestions you can give me. It is 
also possible I'm not taking advantage of bison data structure the right 
way. If so I am sorry if I wasted any of your time. This is my first attempt 
at understand the inner working of a program of this magnitude. If I am not 
being clear about something or if I email the wrong place. I am extremely 
sorry. Again thank you very much for any help you can give me. 
 Best,
David
/* Author: David Ja Date: Aug 02, 2005
 * 
 * attempt at a string generating function
 * this is not a complete code
 * the ideal situation is that this would be 
 * inserted into bison main so that it runs this
 * right after it completed generating DFAs. 
 * (after lalr)
 * Thus the DFA that is generated can be immediately
 * utilized and use to generate a string
 */

#include "state.h"
#include "gram.h"
#include "symtab.h"
#include "assoc.h"
#include "location.h"
#include "uniqstr.h"
#include "system.h"
#include "complain.h"
#include "reader.h"
#include "reduce.h"
#include "hash.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>

int stack[10];
int stackPointer;
char* myoutput;

/*
 * push onto stack
 */

int push(int currentState) 
{
        stackPointer++; // increase stack pointer
        if(stackPointer == 10) {
                printf("full stack\n");
                return 0;
        }
        else {
                stack[stackPointer] = currentState; // store current state 
number on stack
                stack[stackPointer+1] = '\0';
        }
        return 1;
}

/*
 * pop off stack
 */

int pop()
{
        if(stackPointer == -1) {
                printf("below stack\n");
                return 0;
        }
        else {
                stack[stackPointer] = '\0'; // reset the values store on stack 
to NULL (popping)
        }
        stackPointer--; // reduce stack pointer
}

/*
 * find the state to go to if reduced
 */

int findReduce(int currentState, int random) 
{
        int i;
        int length;

        // transition is goto
        if(TRANSITION_IS_GOTO(states[currentState]->transitions, random)) {
                for(i = 0; i<(states[currentState]->reductions->num); i++) {
                        // find the rule where the lhs sym is the same as teh 
access symbol of the resulting goto
                        if(states[currentState]->reductions->rules[i]->lhs == 
                                
TRANSITION_SYMBOL(states[currentState]->transitions, random)){
                                break;
                        }
                }
                
                // if not found
                if(i >= (states[currentState]->reductions->num)) {
                        return -1;
                }
                // if found
                else {
                        // get number of right hand symbol
                        length = 
rule_rhs_lengt(states[currentState]->reductions->rules[i]);

                        // pop states off the stack
                        for(i = 0; i<length; i++) {
                                pop();
                        }

                        // return the state the goto ends up
                        return 
states[currentState]->transitions->states[random]->state_number; 
                }
        }
        else {
                return -1;
        }

        return 0;
}

/*
 * step into state i and return the next state
 */

int getNextState(int currentState) 
{
        int random;
        
        // get a random number to chose a path to take in the DFA
        random = rand()%(states[currentState]->transitions->num);

        if(states[currentState]->transitions->states[random]!= NULL) {
                // if shift
                if(TRANSITION_IS_SHIFT(states[currentState]->transitions, 
random)) {
                        if(myoutput != NULL) {
                                // add the symbol to the myoutput string
                                strcat(myoutput, 
symbols[TRANSITION_SYMBOL(states[currentState]->transitions, random)]->tag);
                        }
                        else {
                                // if first symbol, make myoutput string the 
symbol
                                strcpy(myoutput, 
symbols[TRANSITION_SYMBOL(states[currentState]->transitions, random)]->tag);
                        }
                        // push state onto stack
                        push(currentState);
                        // return state that is shifted to
                        return 
states[currentState]->transitions->states[random]->state_number;
                }
                // reduce case (gotos)
                else {
                        return findReduce(currentState, random); // find the 
state where goto ends in
                }
        }

        return 0;
}

/*
 * generate a string of tokens base on bison's data structure
 */

char* stringGenerator() 
{
        int currentState;

        stack[0] = '\0'; // set stack to NULL at first
        stackPointer = -1; // stack pointer bellow stack

        currentState = getNextState(0); // take in state 0
        while(currentState != -1) {
                currentState = getNextState(currentState); // continue to take 
in state until end
        }
        
        return myoutput; // returns string output
}
_______________________________________________
Help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison

Reply via email to