Hello, Experts!
I need to create code tree for custom language like pascal.
Now I try to add code statements in tree.
I have some code in this language, for examplle:
let a = 2if ( a > 2) while ( a > 2) let a = 2 end while let a = 2end if
class BaseStatement{ internal List<BaseStatement> statements;}sealed class IfStatement : BaseStatement{}sealed class LetStatement : BaseStatement{}sealed class WhileStatement : BaseStatement{}...etc
1) I need the best and beautifull solution to select node for adding current stament. I read code by rows.
If I get "IF" in my parser, root should be IfStatement and next statements should be added to it.
If I get "END IF", I need make step up to above node. May be it should be something like switcher..
2) LetStatement has no descendants .Whether it should inherit class BaseStatement??
Hello, dpearson! Thank you very much for your advice. But I don`t ask about code parsing. I have already made this task. I have statements (code blocks) with correct expression tree. I ask about adding Statement object in code tree, in correct node. And how switch to other node when "if" or "while" statements are finished (end if, end while)..?
dpearson
I ask about adding Statement object in code tree, in correct node
I was trying to show how to parse the data and how to build up the structures (e.g. Statement) at the same time. As you parse an element, that should produce a Statement or other part of the parsed data structure. You are not really "adding objects to a tree", so much as building out the tree as you go through the parse.
Can you see how I was suggesting building up the IfStatement and how it could contain an additional Statement (which itself could be a while loop etc.)? And how we check for the termination of the next Statement using mustBe("}") - which could just as easily be mustBe("endif")?
I think I'm proposing a solution to what you asked - which is how to build up that structure of Statement and other objects into a full tree. In the end you would have a single root Statement object which would contain the entire parsed tree.
If you have already built the parser, it should already have produced this sort of data structure. If it hasn't, I think maybe you haven't really built the parser correctly?
Misha
ASKER
In my case I don`t need to analyze this code for errors. It is correct. And I get code statements (if, while, and if) in correct sequence row by row. And my ultimate goal - get code tree to translate this code to Java. (using C#)
Again, I think what I proposed is really only going to work (only generate a parse tree) when the code being analyzed is correct (valid in the language). I think what you are describing is where you already have a "lexical analyzer". That is the first step of a parser, which breaks up the text into "lexemes" which are the logical parts of the language.
E.g. From: if (x = 4) { print "Hello" } the lexemes would be: "if", "(", "x", "=", "4", ")", "{", "print", "'Hello'"
Then the parser's job is to take that stream of lexemes and convert it into a data structure (Statements etc.). Which is what the code I posted is intended to do.
Anyway, if this isn't what you think you need, I don't see another way to solve it. You need something to go over the series of symbols (lexemes) and convert them to a data structure and that's what a parser's job is. There are different ways to write parsers and I suggested one approach which I know personally works well. But you could just search for other approaches to parser writing if you like (as I say there are also tools that generate parsers from grammars - but they are often quite complicated to use).
Thank you very much for your advice. But I don`t ask about code parsing. I have already made this task. I have statements (code blocks) with correct expression tree.
I ask about adding Statement object in code tree, in correct node. And how switch to other node when "if" or "while" statements are finished (end if, end while)..?