Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > SERVER SIDE CODING > PERL TUTORIALS > THE ROAD TO BETTER PROGRAMMING: CHAPTER 8. THE TOP LEVEL AND COMPOUND CLASS PARSERS


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

The road to better programming: Chapter 8. The top-level and compound-class parsers
By Teodor Zlatanov - 2004-03-22 Page:  1 2 3 4 5 6 7

Sections and classes

After the basic elements, the grammar defines the section and class rules. A section is some word characters (we do not statically list the names of the sections, leaving the door open for easy addition of new sections during the runtime of cfperl) followed by ":" (a colon). Anything after the colon is discarded. The key action of a section is to set the global section variable $current_section to the section name and reset the global current classes variable $current_classes to "any." Note that in the "input" rule, a section comes after a compound class. That's because a section matches a compound class, whose key marker is the "::" sequence (two colons).

The classes can be detected in two ways: either by themselves, where they are a compound class name followed by "::" (two colons), or followed by a line which is to be executed. In either case, the global $current_classes variable is set to the compound class name.

The purpose of the top-level parser is to produce "cfrun atoms." These atoms are interpreted in the cfrun() function. The cfrun atoms are produced as anonymous hashes (allowing room for more elements than just "section," "classes," and "line" in the future). The anonymous hashes grab the current section and compound class from the global variables set in other parts of the grammar. Thus, the grammar behaves like a finite-state machine that moves between states to produce cfrun atoms.

Finally, the input()

The actual input() rule is a marvel of ingenuity, destined to end world hunger and bring about a new era of international cooperation. No, not really, but it is quite useful! The input() rule lists alternate input possibilities in a specific order. For instance, a class could be interpreted as a section, so it must come before a section. A class with a line could be interpreted as a class (and the line dropped) if it didn't come before a class. Thus, the order is important and you should understand why it is specified that way.

It may seem obvious, but the input() rule was in fact the most difficult part of the grammar to write correctly, since it required correct ordering of all alternate input possibilities and an understanding of all the possible input the program could get. In fact, the input() rule is the top entry point of the top-level grammar, which makes it one of the keys to the cfperl program itself. Try (on a local copy of the cfperl program, using the sample configurations from the Resources section) modifying the input() order and see how it changes the cfperl behavior.



View The road to better programming: Chapter 8. The top-level and compound-class parsers Discussion

Page:  1 2 3 4 5 6 7 Next Page: The compound classes

First published by IBM developerWorks


Copyright 2004-2024 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.