visitor questions.

Sep 9, 2008 at 10:17 PM
I'm new to this field and have already started building terminals(class), non terminals(class), scanner(class), and parser(class) manually based on traditional model.  The traditional approach is nice since these are all classes + different types i can use a reflective visitor to make different representations of the ast.

Example I could support:
industry format -> AST -> visitor to "my format"
MANUALLY create AST -> visitor to recreate industry format

the AST acts as the medium that allows the conversion to any format using visitors.

Therefore if i needed to i should be able to create the AST without irony or parser.
Would i be able to accomplish this goal (AST as the medium) with Irony?


Since i will be using NonTerminal, Terminal classes in Irony will that mean that my visitor will need to contain logic to differentiate between terminals?
no method overloading since everything is NonTerminal, Terminal?  or is it encouraged to inherit from these classes?


thanks

leblanc meneses
Coordinator
Sep 10, 2008 at 4:46 AM
Edited Sep 10, 2008 at 4:48 AM

Hi

Not quite sure what you mean by traditional model, but basically things in Irony are done quite similar to what happens in other parser tools. I think you confuse grammar terms (terminals and nonterminals) with elements of output AST tree - nodes - objects of type AstNode or derived classes. Terminals and NonTerminals are defined in grammar, and stay there. Parser produces AstNodes (or derived class instance) for each reduce operation corresponding to a production of nonterminal's rule. The exact type of AST node can be specified in NonTerminal's NodeType property. There are some basic AstNode classes already defined in Irony, but you can define more of your own. And then create "reflective" visitor as you call it.

The alternative way would be to not define custom AST node classes; parse would create all nodes as instances of AstNode class. To distinguish nodes during visiting/processing, you can use "Term" property of each node which holds the terminal/nonterminal that corresponds to this node. You create a visitor with a big switch statement in Visit method for dispatching to appropriate processing:

switch(node.Term.Name) {

  case "Identifier":  DoStuff(); break;

  case "Expression": doOtherStuff(); break;
....
}

Hope this helps
Roman

Sep 13, 2008 at 7:27 PM
Edited Sep 14, 2008 at 9:27 PM
Thanks Roman,

I'm trying to create a parser for an industry standard of RS274X
I created the grammer that interprets the first top level object: Polygon Area Fill (G36, G37) definition:

G36*
X2Y4D02*
X7D01*
Y2D01*
X11Y6D01*
X7Y9D01*
X7D01*
X2D01*Y4D01*G37*


this will now be mapped to .net gdi+ FillPolygon.
FillPolygon Method (Brush, array<Point>[]())


Here is the output of my visitor:
BeginVisit PolygonAreaFillDefinition
BeginVisit G36
BeginVisit *
BeginVisit PointDefinition
BeginVisit AxisDefinition
BeginVisit X
BeginVisit NumberMakePlus
BeginVisit 2
BeginVisit AxisDefinition
BeginVisit Y
BeginVisit NumberMakePlus
BeginVisit 4
BeginVisit D02
BeginVisit *
BeginVisit PolygonAreaFillPointMakePlus
BeginVisit PolygonAreaFillPoint
BeginVisit AxisDefinition
BeginVisit X
BeginVisit NumberMakePlus
BeginVisit 7
BeginVisit D01
BeginVisit *
BeginVisit PolygonAreaFillPoint
BeginVisit AxisDefinition
BeginVisit Y
BeginVisit NumberMakePlus
BeginVisit 2
BeginVisit D01
BeginVisit *
BeginVisit PolygonAreaFillPoint
BeginVisit PointDefinition
BeginVisit AxisDefinition
BeginVisit X
BeginVisit NumberMakePlus
BeginVisit 1
BeginVisit 1
BeginVisit AxisDefinition
BeginVisit Y
BeginVisit NumberMakePlus
BeginVisit 6
BeginVisit D01
BeginVisit *
BeginVisit PolygonAreaFillPoint
BeginVisit PointDefinition
BeginVisit AxisDefinition
BeginVisit X
BeginVisit NumberMakePlus
BeginVisit 7
BeginVisit AxisDefinition
BeginVisit Y
BeginVisit NumberMakePlus
BeginVisit 9
BeginVisit D01
BeginVisit *
BeginVisit PolygonAreaFillPoint
BeginVisit AxisDefinition
BeginVisit X
BeginVisit NumberMakePlus
BeginVisit 7
BeginVisit D01
BeginVisit *
BeginVisit PolygonAreaFillPoint
BeginVisit AxisDefinition
BeginVisit X
BeginVisit NumberMakePlus
BeginVisit 2
BeginVisit D01
BeginVisit *
BeginVisit PolygonAreaFillPoint
BeginVisit AxisDefinition
BeginVisit Y
BeginVisit NumberMakePlus
BeginVisit 4
BeginVisit D01
BeginVisit *
BeginVisit G37
BeginVisit *


How should i go about converting this tree into new Point(x, y) objects and creating a Graphic.FillPolygon method call?
Seems like i'll have to implement: http://en.wikipedia.org/wiki/State_pattern so i can maintain state and interpret the tree into meaningful parts. (meaning i'm still parsing)
Or i could extend astnode and create my own top level PolygonAreaFillAstNode.
Or i can send a visitor that simply is to recreate the same tree with specific areas of the tree optimized for easier parsing.
what is the recommended approach to interpret the tree to call Graphic.FillPolygon ?


Also when i call compiler.Parse(txt)
if my grammer or input is not correct it returns null instead of an Irony.Compiler.AstNode.  So how do i get errors back to know where it failed?  character position?



thanks,
-lm




Coordinator
Sep 15, 2008 at 3:20 PM
About your parser and .NET objects conversion - I don't understand at all what you're trying to do. If possible, please send me a zip with your code and comments.
Errors in parsing - errors are available through context.Errors property.
Roman
Sep 16, 2008 at 5:30 AM
http://resources.robusthaven.com/

download: RS274XParser.zip 
About the project:
Basically the only irony stuff are 3 files:
RS274XGrammar.cs -
            this.Root = PolygonAreaFillDefinition;  // most of the grammer after PolygonAreaFillDefinition is not used...

RS274XParser.cs
RS274XFileVisitor.cs

all the other files was my attempt to manually create my own parser/scanner/ before i found irony.

the documentation folder in the project contains the industry format specification.

there is unit tests for my stuff and i started one file to test the irony grammar i created.
IronyVersion.cs, to successfully run the tests modify App.config to point to an absolute path.



=============
Like i said i'm creating the ast tree for polygonareafilldefinition but i'm not seeing how i should preceed from the tree representation to what i really want a bitmap file.

- lm

Coordinator
Sep 16, 2008 at 8:58 PM
Edited Sep 16, 2008 at 10:51 PM

Got it - I'll have a look at it and reply directly to you shortly

Roman

 PS Please contact me directly, let's handle details offline thru direct contact