![]() ![]() It might be easier to let the expression tree library do that for you you can transform your parse tree into an expression tree, and then turn the expression tree into a delegate, and evaluate the delegate. Spitting IL seems like overkill, but if you wanted to be really buff, you could. Then write a code generator that spits out the code necessary to evaluate the truth tables. Then write an analyzer that looks at that tree and figures stuff out, like "how many distinct free variables do I have?" Second, figure out what the grammar of your language is, and write a recursive descent parser that breaks up an IEnumerable into an abstract syntax tree that represents grammatical entities in your language. Then write a method that takes a string and returns an IEnumerable'. (IdentifierToken, OrToken, AndToken, ImpliesToken, RightParenToken.). Start by defining what the tokens of your language are, define a base class Token and a bunch of derived classes for each. This is the semantic analysis phase.Ĭompilers then have a fourth phase that humans do not, which is they generate code that represents the actions described in the language. You can figure out that the dog is the thing doing the eating, and the food is the thing being eaten. For instance, you can see that there are three parts of this sentence - the subject, the appositive, and the "his" in the object - that all refer to the same entity, namely, the dog. Once you know that the sentence is grammatical, you can then analyze the sentence to actually get meaning out of it. The grammar of English is extremely complicated, but this sentence is pretty straightforward. The next thing you do is analyze the token stream to see if the sentence is grammatical. If I give you a sentence: "The dog, Rover, ate his food.", the first thing you do is break it up into words and punctuation. The way such systems work is a formalization of how we understand natural languages. I would skip trying to use a parser generator if this is for your own edification, you'll learn more by doing it all from scratch. You'll learn a lot about how the basic parts of a compiler work. This sounds like a great personal project. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |