Re: Script Parser
- From: "Phlip" <phlipcpp@xxxxxxxxx>
- Date: Thu, 31 Aug 2006 02:53:03 GMT
CJReilly wrote:
I am trying to build a simple script parser with basic-like syntax and
including the standard loops, variables, if/else statements, and such
found in most core languages.
I was hoping someone could point me to a good resource. I've tried
searching for books and articles online, but no luck. :-( Unfortunately
my comp. sci. class voted against the script parser lesson.
The secret to writing a scripting language is writing a lot of tests for it.
Each test case will define the language.
Start here:
http://www.xpsd.org/cgi-bin/wiki?RecursiveDescentParserCpp
(Ping me privately if you have trouble scraping the source off those pages.)
Now go to one of the tests:
CPPUNIT_ASSERT_CLOSE( 1.0 , parse("3.0 ^ (4-4)") );
To continue building the language, clone that line, then run all the tests
and make sure they pass. Then change that line; let's introduce a variable
notation:
CPPUNIT_ASSERT_CLOSE( 42.0 , parse("num = 42") );
Run all the tests, and predict that will fail.
Now go to the source, and trivially parse the = sign. The code I posted
makes that easy. Parse the = at the correct level of the "recursive descent
parser" that it will have the correct precedence. Pass the test, and add
more similar tests to make sure the feature works.
(Your comp. sci. class has doubtless told you one of the most important
rules of programming is to write unit tests for every line of code you
write.)
Next, add a test that uses a statement operator ; to divide statements.
Keep going until you have all the language features you want.
However, while you do that, also start here:
http://rubyforge.org/projects/rubyinstaller/
You can embed Ruby (a complete language with all those loop statements and
such) with an absurdly few lines of code. The downloader will come with some
embedding examples.
Wheel reinvention is really fun, and sometimes you actually do need one of a
different color, but never overlook the free alternatives.
--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
.
- Follow-Ups:
- References:
- Script Parser
- From: CJReilly
- Script Parser
- Prev by Date: Script Parser
- Next by Date: looking for programmers who are preparing and afraid of panflu
- Previous by thread: Script Parser
- Next by thread: looking for programmers who are preparing and afraid of panflu
- Index(es):
Relevant Pages
|