[o:XML] Query on YAPP Usage
sudhir.parisaboyina at wipro.com
sudhir.parisaboyina at wipro.com
Thu Aug 23 12:34:56 BST 2007
Hi,
Thanks a zillion for letting all of us know on how to use YAPP. Now I am
able to generate the parser xsl file & call it from my own style sheet
using the same as illustrated by you. But I have an observation over
here. I was trying to see what this resulting YAPP parser will do if I
pass an invalid data (as my project requirement is to validate the data
with BNF rule). Hence I passed the in param in the call template as '123
+ abc', and I see this output as:
<term name="AdditiveExpression">
<term name="number">123</term>
<term name="AdditiveExpression-rest">
<term name="plus">+</term>
<term name="number">abc</term>
<term name="AdditiveExpression-rest"/>
Where as my BNF rule is as below
minus ::= '-' ;
plus ::= '+' ;
number ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' ;
Expression ::= AdditiveExpression end ;
AdditiveExpression ::= number | AdditiveExpression minus number |
AdditiveExpression plus number ;
After going through the mailing archive, I have sensed that YAPP does
not support "error detection & reporting". I want to re confirm if this
is true and also willing to know whether YAPP, in future is going to
have such enhancements done. If no, can you pls give us some guidelines
on how can we make YAPP as an error detecting + reporting tool?
Thanks & Regards,
Pnvd Sudhir
-----Original Message-----
From: Martin Klang [mailto:martin at o-xml.org]
Sent: Wednesday, August 22, 2007 7:05 PM
To: Sudhir Parisaboyina (WT01 - TES-Transport Infrastructure)
Cc: o-xml at lists.pingdynasty.com; Karuna Kosaraju (WT01 - TES-Transport
Infrastructure); Siddesh P R (WT01 - TES-Transport Infrastructure)
Subject: Re: [o:XML] Query on YAPP Usage
Hi there,
Thanks for using YAPP, apologies that documentation and examples
aren't quite up to scratch. If you would like to contribute e.g. with
code examples or docs, please feel free.
Your grammar contains a construct that is left recursive, which makes
it unparseable by recursive descent parsers - see for example http://
en.wikipedia.org/wiki/Left_recursion for more details.
YAPP is a recursive descent parser, hence the generated grammar
doesn't produce a valid parser.
The solution is quite simple, YAPP comes with a stylesheet that will
eliminate left recursion.
I managed to get your example working with the following process:
1) create YAPP BNF parser and lexer.
xalan -XSL generator.xsl -IN bnf-grammar.xml > bnf-parser.xsl
xalan -XSL tokenizer.xsl -IN bnf-grammar.xml > bnf-lexer.xsl
2) generate XML grammar from calculator BNF grammar:
xalan -XSL bnf-parser.xsl -IN calculator-grammar.bnf > calculator-
grammar.xml
3) fix left recursion problem:
xalan -XSL eliminator.xsl -IN calculator-grammar.xml > fixed-
calculator-grammar.xml
3) generate parser and lexer:
xalan -XSL generator.xsl -IN fixed-calculator-grammar.xml >
calculator-parser.xsl
xalan -XSL tokenizer.xsl -IN fixed-calculator-grammar.xml >
calculator-lexer.xsl
All files apart from calculator-grammar.bnf are supplied with YAPP, I
created it from your example as follows:
<grammar>
<terminal name="end">
<end/>
</terminal>
<ignore char=" "/>
<bnf>
minus ::= '-' ;
plus ::= '+' ;
number ::= [.0123456789] ;
Expression ::= AdditiveExpression end ;
AdditiveExpression ::= number | AdditiveExpression minus number |
AdditiveExpression plus number ;
</bnf>
</grammar>
I then called the resulting parser with this stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:p="http://www.pingdynasty.com/namespaces/parser">
<xsl:import href="calculator-lexer.xsl"/>
<xsl:import href="calculator-parser.xsl"/>
<xsl:template match="/">
<output>
<xsl:call-template name="p:Expression">
<xsl:with-param name="in" select="'123 + 456 - 789'"/>
</xsl:call-template>
</output>
</xsl:template>
</xsl:stylesheet>
... and I got this result:
<output>
<term name="Expression">
<term name="AdditiveExpression">
<term name="number">123</term>
<term name="AdditiveExpression-rest">
<term name="plus">+</term>
<term name="number">456</term>
<term name="AdditiveExpression-rest">
<term name="minus">-</term>
<term name="number">789</term>
<term name="AdditiveExpression-rest"/>
</term>
</term>
</term>
<term name="end"/>
</term>
<remainder/>
</output>
The construct called AdditiveExpression-rest has been created by
YAPP, it can be ignored, or filtered out with another XSL.
I hope this helps, let me know how it goes!
/m
The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments.
WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email.
www.wipro.com
More information about the o-xml
mailing list