The Artima Developer Community
Sponsored Link

Weblogs Forum
No More YACC and LEX

5 replies on 1 page. Most recent reply: Dec 31, 2004 9:09 AM by Gregg Wonderly

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

No More YACC and LEX (View in Weblogs)
Posted: Dec 23, 2004 9:03 PM
Reply to this message Reply
Summary
My answer to YACC and Lex, the YARD parser version 1.0, is now online at SourceForge.net.
Advertisement

Due to my distaste for parser generation tools like YACC and Lex and being such a completely unreasonable guy, I decided to write my own recursive descent parser. I first did it in Delphi, then I did it in Heron. Now I just finished version 1.0 of the YARD parser for C++ and I've posted it to SourceForge.net.

The YARD parser is a data-type agnostic CFG parser tool written using template metaprogramming techniques in C++. The YARD parser was designed to be as compact and flexible as possible.

The YARD parser has a significantly different design than its closest cousin, the boost::spirit library, because the grammar productions are defined directly as meta-functions, and there is no funky operator overloading. Semantic actions are defined as template specializations. See my earlier post on Static versus Dynamic Event Handlers in C++.

The YARD parser source code comes with a XML parser as way of a demonstration, of the ease of use etc. There is also a very simple built-in tokenizer for those who might want to use YARD as a powerful lexical analyzer.


Ivan Korostelev

Posts: 1
Nickname: ivank
Registered: Dec, 2004

Re: No More YACC and LEX Posted: Dec 24, 2004 4:06 AM
Reply to this message Reply
And what about Boost.Spirit? http://spirit.sourceforge.net As far as I can see you make exactly the same thing.

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: No More YACC and LEX Posted: Dec 24, 2004 9:24 AM
Reply to this message Reply
> And what about Boost.Spirit? http://spirit.sourceforge.net
> As far as I can see you make exactly the same thing.

They are very different, the primary difference however is:
- Spirit gramamr productions are expressed as expressions
- YARD grammar productions are expressed as types

For instance, in Spirit:

rule<> a_rule = *(a | b) & +(c | d | e);


In YARD:


struct a_rule =
re_and
<
re_star<
re_or<a, b>
>,
re_plus<
re_or3<c, d, e>
>
>;


Spirit is less verbose, and supports dynamic rules. YARD on the other hand supports only static grammar productions which makes it more compact, flexible, and efficient. Spirit as a result has a lot of limitations with regards to where and how to define rules. It is much easier to write custom rules using YARD.

If you study more closely both libraries, and try to use them, I think the differences will become apparent very quickly. I find the YARD parser far easier to use.

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: No More YACC and LEX Posted: Dec 24, 2004 8:56 PM
Reply to this message Reply
Here is another illustration of the difference between YARD / Boost.

An example in YARD:

struct StartRule {
template<typename Elem_T>
static bool Accept(ParserInputStream<Elem_T>& in) {
switch (in.GetChar()) {
case 'a' : return match<RuleA>(in);
case 'b' : return match<RuleB>(in);
case 'c' : return match<RuleC>(in);
case 'd' : return match<RuleD>(in);
default : return false;
}
}
}


The same example in Spirit:


#define BOOST_SPIRIT_SWITCH_CASE_LIMIT 10
#include "boost/spirit/switch.hpp"
rule<> rule_overall =
switch_p
[
case_p<'a'>(parser_a),
case_p<'b'>(parser_b),
case_p<'c'>(parser_c),
case_p<'d'>(parser_d),
default_p(parser_default)
];


So Yard is more like C++, but the grammar is statically defined rather than dynamically. Spirit also has a weird arbitrary upper limit on the number of case statements and doesn't use real C++ constructs. (what's switch_p / case_p / default_p ?).

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: No More YACC and LEX Posted: Dec 27, 2004 7:13 AM
Reply to this message Reply
Apparently the previous is a bad comparison. Oh well. I am not able to figure out a better comparison, Spirit has left me quite confused.

Gregg Wonderly

Posts: 317
Nickname: greggwon
Registered: Apr, 2003

Re: No More YACC and LEX Posted: Dec 31, 2004 9:09 AM
Reply to this message Reply
> Apparently the previous is a bad comparison. Oh well. I am
> not able to figure out a better comparison, Spirit has
> left me quite confused.


Maybe you could show a complete Mathematical expression parser with operator precedence in both spirit and YARD so that we could see something that is commonly found in a YACC specification?

Flat View: This topic has 5 replies on 1 page
Topic: Logging configuration using Jini Previous Topic   Next Topic Topic: Directories are not Java source file package names

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use