Original Post
I am at the point where I have the ability to read in a file describing the grammar of a language, with regular expressions defining a token, and symbols that are either terminal or non-terminal, similar to a BNF description of a language. However, I am now having a hard time figuring out how to parse the input based on this information.
The grammar is stored in a sort of tree. The grammar starts with an array of non-terminals; one of them is designated as the starting point. Within each one is a group of alternatives, which constitute the definitions of the non-terminal symbol. Each alternative has a list of nodes, which are one of a literal string, a token defined by one of the aforementioned regular expressions, or a pointer to a non-terminal, which points to one of the non-terminals in the original array. In this way, the grammar is stored in a structure that can be iterated through. So, my first idea was to use a finite automation, to have a state machine with various threads to iterate through, since I read the lexemes from a sequential file, and I cannot move backwards in the file, so using recursion will not be an option, as it requires that it go back and re-evaluate the lexemes if the current path turns out to be the wrong one.
So, the problem is, it's difficult to conceptualize, as I cannot figure out how to evaluate a non-terminal within a non-terminal, then return back to evaluating the topmost non-terminal without recursion. I hear it is very posible to use a state machine, and someone somewhere has done it. Can anyone give advice on what worked or might make more sense?
The grammar is stored in a sort of tree. The grammar starts with an array of non-terminals; one of them is designated as the starting point. Within each one is a group of alternatives, which constitute the definitions of the non-terminal symbol. Each alternative has a list of nodes, which are one of a literal string, a token defined by one of the aforementioned regular expressions, or a pointer to a non-terminal, which points to one of the non-terminals in the original array. In this way, the grammar is stored in a structure that can be iterated through. So, my first idea was to use a finite automation, to have a state machine with various threads to iterate through, since I read the lexemes from a sequential file, and I cannot move backwards in the file, so using recursion will not be an option, as it requires that it go back and re-evaluate the lexemes if the current path turns out to be the wrong one.
So, the problem is, it's difficult to conceptualize, as I cannot figure out how to evaluate a non-terminal within a non-terminal, then return back to evaluating the topmost non-terminal without recursion. I hear it is very posible to use a state machine, and someone somewhere has done it. Can anyone give advice on what worked or might make more sense?