BNF grammar test case generation [closed] - unit-testing

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Does anyone have any experience with a tool that generates test strings from a BNF grammar that could then be fed into a unit test?

I don't have an answer to the tool question, but I will say it is fairly easy in any text processing language (perl/python/etc) to randomly generate sentences from a BNF grammar, and slightly more verbose in a bigger language (Java/C/etc), but it shouldn't be too hard to roll your own.
The problem with this, of course, is that it can only generate strings in the grammar, and unless your grammar is very simple, the test space is infinitely large.

I've done exactly as hazzen commented (using an embedded DSL in a scripting language). It was a mildly interesting exercise, but except for the most basic tests of e.g. parsing, it wasn't terribly useful. Most of my most interesting tests have to do with more sophisticated relationships than one can easily express in BNF (or any other context-free grammar).

If, say, you're developing a compiler, then you likely have an abstract syntax tree datatype. If so, then you could write a function to generate an random AST -- with that, you can print it to a string and feed that to your unit test. It's guaranteed to be a valid program this way, since you started with your AST.
If I were writing a compiler in Haskell or ML, this is what I would do, using QuickCheck.

Gramtest is one such tool that can generate strings from arbitrary user defined BNF grammars. You can read more details about the algorithm behind Gramtest here and some practical tips on the tool are available here.

Related

Flex and Bison tools [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I want know which is best whether compiler design using either tools like Flex & Bison or C/C++.
Because I've heard that developing with tools is not worthy at all. I'm a beginner to compiler design so I need clarification about this. And other thing that I want to ask you is which of those two ways as I said above expected by a company in compiler design. Please help me!!!
This depends on complexity of your syntax you want to parse. It is not hard to implement simple grammar by hand but without experience it is hard for complex grammars.
If you don't have any experience with lexers and parsers, it would be better to start with other tools not using LR (LALR) parser but LL because they are much more intuitive for starters.
I have very good experience with ANTLR. It generates Java code but it has also C++ backend and nice grammar development tools (ANTLRWorks).
So my advice is none of those two you have chosen but start with ANTLR.
If you are not constrained by C/C++, I would recommend Eclipse Xtext. It is simple to learn, has great documentation, and has the added benefit of automatically generating an Eclipse-based IDE that supports your language, with syntax-highlighting, content assist, and other powerful features.
Xtext has its own programming language (Xtend) which is a more powerful version of Java (in a way, it is similar to Scala). Xtext uses ANTLR internally.

C++ parser generator [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I'm writing my own scripting language and I need a software tool which generates C++ code for parsing my language. I need a lexical analyzer and a parser generator which generates C++ code. It would be nice for me to be able also to generate a Visual C++ 2010 project. Suggestions?
http://en.wikipedia.org/wiki/Comparison_of_parser_generators
for C/C++: http://epaperpress.com/lexandyacc/
Or look at: Boost.Spirit:
"Spirit is a set of C++ libraries for parsing and output generation
implemented as Domain Specific Embedded Languages (DSEL) using
Expression templates and Template Meta-Programming."
Dou you really need new language? maybe it would be better to use some well known like Lua, Python?
Try with Flex and Bison. They are good lexical analizers and parser generator usefull to define new languages.
http://en.wikipedia.org/wiki/Flex_lexical_analyser
It's an old question but still might be relevant: since I was unhappy with the existing options, I recently wrote a template c++ parser generator which doesn't need any external tools (you include a header and define the grammar directly in the c++ source). It uses readable PEG grammars so there is no need for a separate lexing step. You can check it out on Github.
You have two choices: whether you create your own parser by creating an AST (abstract syntax tree), then it will be a good exercise for you but it's very long and hard to implement. Or you can use an open source solution such the ANTLR parser generator which has a grammar for C/C++ as well as the preprocessor. I've never used it so I can't say how complete its parsing of C++ is going to be. Then If you are in hurry and you want to create a good parser, you'd better use the second solution

Does every large project include a Lisp interpreter? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I had the impression that there was a paper or article somewhere that claimed every sufficiently large project (not written in a Lisp variant) contained a poorly implemented Lisp interpreter. Google turns up nothing and a quick search of SO doesn't either. Is this something well known and documented somewhere I have forgotten, or just a figment of my imagination?
An actual document or link to such an article would be appreciated, if it exists. Otherwise, I will remove the question.
What Greenspun meant when he uttered this quip was that Lisp provides a great many foundational technologies for writing good software, and that programs written in other languages informally (and inferiorly) reproduce a number of them as they grow.
Yes, this claim is Greenspun's tenth rule (actually the only rule):
Any sufficiently complicated C or Fortran program contains an ad hoc,
informally-specified, bug-ridden, slow implementation of half of
Common Lisp.
It is making a valid point about the expressiveness of Lisp-style features (particularly its kind of macros). However, it isn't serious to the degree you would write a paper on it.

What is the best way to define grammars for a text editor? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I'm masochistically writing an open-source text editor for Mac and have finally reached the point at which I want to add syntax highlighting. I've been going back and forth on various solutions for the past few days, and I've finally decided to open the question to a wider audience.
Here are the options I see:
Define languages basically with a series of regex pattern matching (similar to how TextMate defines its languages)
Define languages with a formal grammar like BNF or PEG
Using regex pattern matching seems less than ideal as it cannot formally represent a language nearly as well as a formal grammar; however, some less formal languages will have a hard time fitting into BNF (i.e. Markdown -- though I know there's a great PEG implementation).
What are the performance tradeoffs for live syntax highlighting? What about flexibility for a wide range of languages?
If I go the BNF route, Todd Ditchendorf created the awesome ParseKit framework which would work nicely out-of-the-box. Anyone know of any anything similar for PEG's?
Unless you want to fight the battle of getting a full-context free (or worse, a full context-sensitive) grammar completely correct for every language you want to process (or worse, for every dialect of the language you want to process... how many kinds of C++ are there?), for the purposes of syntax highlighting you're probably better giving up on complete correctness and accept that sometimes you'll get it wrong. In that case, regexps seem like an extremely good answer. They can also be very fast, so they won't interfere with the person doing the editing.
If you insist on doing full syntax checking/completion (I don't think you are), then you'll need that full grammar. You'll also be a very long time in producing editors for real languages.
Sometimes it is better not to be too serious. A 98% solution that you can get is better than a 100% solution that never materializes.
It might not be exactly what you need since you are writing the editor yourself, but there is an awesome framework called Xtext that will actually generate a complete editor with syntax coloring, customizable outline view and auto-completion etc., based on a grammar for your language: http://eclipse.org/Xtext

Any good advice on using emacs for C++ project? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I'm looking for a good article on using emacs as C/C++ IDE.
Something like Steve Yegge's "Effective emacs".
No specific article, really, but I've found EmacsWiki to be full of useful information. Consider checking out these entries:
CPlusPlus as a starting point for many C++-related articles, and
CppTemplate to define a template that can give you a good skeleton when you start new files
I've recently stumbled upon this article which is quite good.
EDIT: Yep the link is no longer valid. It seems like they've changed their url recently and it doesn't redirect properly. Hopefully it will be back soon. Anyway the article was called "Benjamin Rutt's Emacs C development tips". I managed to find a copy here.
I'm planning to write such article in near future, but you can now take my configuration of Cedet + Emacs, that helps me to effectively edit C++ sources.
If you'll have questions, you could ask me directly
Be aware that Emacs' C++ mode is based on only regular expressions, not a grammar. Hence, the syntax highlighting is not based strictly on the syntax of the language itself, but rather is largely based on commonplace formatting. The Emacs syntax highlighting of C++ often makes mistakes.
The problem is not limited to syntax hightlighting. The same defective design applies to the automatic formatting. All this said, I have been using only Emacs for all of my editing of C++ source code for over 20 years, since the cfront days. (I usually turn off electric key bindings, because of Emacs' defective regex-based design. Regexes do not have enough expressive power to describe the C++ syntax accurately.
I recommend ggtags and irony-mode. Other then that you may want to use helm-ag to search ("grep") for strings in your codebase.