473,414 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

[Snippet] a Recursive Descent Parser via TDD - recursiveDescentParser.cpp

C++ newsgroupies:

I wrote a parser to solve math expressions like "3.0 ^(4 - 5)", or "3 / 8".
Below my sig is recursiveDescentParser.cpp, the test suite that drove the
implementation of the expression solver found in recursiveDescentParser.h,
in a parallel post.

Test-Driven Development works by accumulating assertions. Before the very
first assertion existed...

CPPUNIT_ASSERT_CLOSE( 3.0, parse("3") );

....the parse() function could not parse anything. After I got that assertion
to pass (and before writing any other assertions), parse() contained only
the simplest code possible to parse a "3.0". Naturally, I used strtod().

I added the assertions, one by one, and got each to pass. Whenever the
design of parse() became poor, I refactored it to improve its design. This
migrated the call to strtod() from parse() to its current site,
parseTerminal().

TDD can rapidly produce a robust and extensible design. When the time came
to add parentheses, then exponentiation, I did not need to refactor the
current design at all - I only extended it.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
// a recursive descent parser,
// written test-first

#include "recursiveDescentParser.h"
#include "test.h"
bool TestCase::all_tests_passed = true;
TestCase::TestCases_t TestCase::cases;
using namespace parser;

#define ASSERT_BEEF(function, beef) \
CPPUNIT_ASSERT_EQUAL(false, function.getValid()); \
CPPUNIT_ASSERT_EQUAL(beef, function.m_beef);
TEST_(TestCase, term)
{
CPPUNIT_ASSERT_CLOSE( 3.0, parse("3") );
CPPUNIT_ASSERT_CLOSE( 2.0, parse("2") );
CPPUNIT_ASSERT_CLOSE( 2.8, parse("2.8") );
CPPUNIT_ASSERT_CLOSE( 2.8, parse(" 2.8") );
CPPUNIT_ASSERT_CLOSE(-2.8, parse("-2.8") );
ASSERT_BEEF( parse(".q") , "syntax error" );
ASSERT_BEEF( parse("- 1.0"), "syntax error" );

}
TEST_(TestCase, sum)
{
CPPUNIT_ASSERT_CLOSE( 5.0, parse("3 + 2") );
CPPUNIT_ASSERT_CLOSE( 1.0, parse("3 + -2") );
CPPUNIT_ASSERT_CLOSE(11.0, parse("3 + 8") );
CPPUNIT_ASSERT_CLOSE(11.0, parse("3 + 4 + 4") );
CPPUNIT_ASSERT_CLOSE( 5.0, parse("-3 + 4 + 4") );
ASSERT_BEEF( parse("3 + .q") , "syntax error" );
ASSERT_BEEF( parse("3 + 12 +"), "syntax error" );
}
TEST_(TestCase, subtraction)
{
CPPUNIT_ASSERT_CLOSE( 1.0, parse("3 - 2") );
CPPUNIT_ASSERT_CLOSE( 5.0, parse("3 - -2") );
CPPUNIT_ASSERT_CLOSE( -5.0, parse("3 - 8") );
CPPUNIT_ASSERT_CLOSE( -5.0, parse("3 - 4 - 4") );
CPPUNIT_ASSERT_CLOSE(-11.0, parse("-3 - 4 - 4") );
CPPUNIT_ASSERT_CLOSE( -3.0, parse("-3 + 4 - 4") );
CPPUNIT_ASSERT_CLOSE( -3.0, parse("-3 - 4 + 4") );
ASSERT_BEEF( parse("3 - .q") , "syntax error" );
ASSERT_BEEF( parse("3 + 12 -"), "syntax error" );
}
TEST_(TestCase, multiplication)
{
CPPUNIT_ASSERT_CLOSE( 6.0, parse("3 * 2") );
CPPUNIT_ASSERT_CLOSE( -6.0, parse("3 * -2") );
CPPUNIT_ASSERT_CLOSE( 24.0, parse("3 * 8") );
CPPUNIT_ASSERT_CLOSE( 8.0, parse("3 * 4 - 4") );
CPPUNIT_ASSERT_CLOSE(-19.0, parse("-3 - 4 * 4") );
ASSERT_BEEF( parse("3 * .q") , "syntax error" );
ASSERT_BEEF( parse("3 - 12 *"), "syntax error" );
}
TEST_(TestCase, division)
{
CPPUNIT_ASSERT_CLOSE( 1.5 , parse("3 / 2") );
CPPUNIT_ASSERT_CLOSE( -1.5 , parse("3 / -2") );
CPPUNIT_ASSERT_CLOSE( 0.375, parse("3 / 8") );
CPPUNIT_ASSERT_CLOSE( -3.25 , parse("3 / 4 - 4") );
CPPUNIT_ASSERT_CLOSE( -3.0 , parse("-3 / 4 * 4") );
ASSERT_BEEF( parse("3 / .q") , "syntax error" );
ASSERT_BEEF( parse("3 + 12 /"), "syntax error" );
}
TEST_(TestCase, parens)
{
CPPUNIT_ASSERT_CLOSE( 1.5 , parse("(3) / 2") );
CPPUNIT_ASSERT_CLOSE( -1.5 , parse("(3 / -2)") );
CPPUNIT_ASSERT_CLOSE( 0.375, parse("3 / (8)") );
CPPUNIT_ASSERT_CLOSE( -3.0 , parse("3.0 / (4-5)") );
CPPUNIT_ASSERT_CLOSE( -3.0 , parse("(-3.0 / 4) * 4") );
CPPUNIT_ASSERT_CLOSE( -3.0 , parse("(-3.0 / 4)*4") );
CPPUNIT_ASSERT_CLOSE( -3.0 , parse("3.0 / ((((4 - 5) )) )") );
ASSERT_BEEF( parse("3.0 / (4 - 4)") , "division by zero error" );
ASSERT_BEEF( parse("(3 / .q"), "syntax error" );
ASSERT_BEEF( parse("3 + 12)"), "incomplete expression" );
}
TEST_(TestCase, exponentiation)
{
CPPUNIT_ASSERT_CLOSE( 9.0 , parse("3 ^ 2") );
CPPUNIT_ASSERT_CLOSE( 0.111111, parse("3 ^ -2") );
CPPUNIT_ASSERT_CLOSE(6561.0 , parse("3 ^ 8") );
CPPUNIT_ASSERT_CLOSE( 77.0 , parse("3 ^ 4 - 4") );
CPPUNIT_ASSERT_CLOSE( 324.0 , parse("-3 ^ 4 * 4") );
CPPUNIT_ASSERT_CLOSE( 9.0 , parse("(3) ^ 2") );
CPPUNIT_ASSERT_CLOSE( 0.111111, parse("(3^-2)") );
CPPUNIT_ASSERT_CLOSE(6561.0 , parse("3^(8)") );
}
TEST_(TestCase, exponentiationAndParens)
{
CPPUNIT_ASSERT_CLOSE( 0.333333, parse("3.0 ^(4 - 5)") );
CPPUNIT_ASSERT_CLOSE( 1.0 , parse("3.0 ^ (4-4)") );
CPPUNIT_ASSERT_CLOSE( 324.0 , parse("(-3.0 ^ 4) * 4") );
CPPUNIT_ASSERT_CLOSE( 0.333333, parse("3.0 ^ ((((4 - 5) )) )") );
ASSERT_BEEF( parse("3 ^ .q") , "syntax error" );
ASSERT_BEEF( parse("3 + 12 ^"), "syntax error" );
}
bool
runTests()
{
bool result = TestCase::runTests();
char const * determination = "";

if (result)
determination = "All tests passed!\n";
else
determination = "Tests failed...\n";

cout << determination;
OutputDebugString("\n");
OutputDebugString(determination);
OutputDebugString("\n");

return result;
}
int main() { return ! runTests(); }


Jul 22 '05 #1
0 1468

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Magnus Lie Hetland | last post by:
Hi! I've been looking at ways of dealing with nested structures in regexps (becuase I figured that would be faster than the Python parsing code I've currently got) and came across a few...
6
by: Phlip | last post by:
C++ newsgroupies: I wrote a parser to solve math expressions like "3.0 ^(4 - 5)", or "3 / 8". It's bad luck to name an interface after its implementation. This file could have been...
0
by: Phlip | last post by:
C++ newsgroupies: I wrote a parser to solve math expressions like "3.0 ^(4 - 5)", or "3 / 8". Below my sig is test.h, the file containing a tiny test framework. Using it and Test-Driven...
6
by: Jon Shemitz | last post by:
I just wrote my second method that reads an XML stream with XmlReader and spits out a instance of an object that represents the XML stream. In effect, this was a simple recursive descent parser...
9
by: seberino | last post by:
I'm a compiler newbie and curious if Python grammar is able to be parsed by a recursive descent parser or if it requires a more powerful algorithm. Chris
4
by: seberino | last post by:
I'm a compiler newbie and was curious if Python's language/grammar can be handled by a recursive descent parser. Well? Chris
18
by: Just Another Victim of the Ambient Morality | last post by:
Is pyparsing really a recursive descent parser? I ask this because there are grammars it can't parse that my recursive descent parser would parse, should I have written one. For instance: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.