Question

I built a parser in JavaCC that given for example: let a : int =5+4; it displays:

Start
 Sxl
  Statement
   VariableDecl
    Identifier
    Type
    Expression
     SimpleExpression
      Term
       Factor
        Literal
         IntegerLiteral
      AdditiveOp
      Term
       Factor
        Literal
         IntegerLiteral
 Parsed.

that works fine however I need enhance this parser to output to an xml file and output the following output for the above input:

letNode( Identier(X), ExprNode( PlusNode( IntegerLiteral(8), IntegerLiteral(2) )))

How can this be done, do I need to use normal Java or there is some functionality in JavaCC?

Was it helpful?

Solution

It looks to me as if you are using JJT. Here are 3 ways to convert the input file to an XML file using JavaCC or JJT.

  1. It is easy to edit the generated SimpleNode class so that it outputs XML -- or whatever format you want.

  2. An alternative is to use JavaCC rather than JJT and to make SAX calls so that your parser constructs an XML document. Then it is easy to output the XML document.

  3. A third option is to stick with JJT and modify the SimpleNode class to produce an XML document (using either SAX or DOM). Then the XML document can outputted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top