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

The compound class atoms

There are two base-level atoms in the compound class grammar: an atom of a simple class name (atom), and an atom of an inverted simple class name (inversion). Each one returns a function that takes as parameters a reference to a hash of defined classes, and the atom name itself. The atom function checks if the atom exists in the classes hash (so "solaris" would evaluate to 1 on a Solaris machine, presumably) and the inversion function does the opposite.

Note that inversion only specifies the inversion of a simple class name. The inversion of a compound class is done in the "unary" rule.

The compound class logicals

The most complex part of the compound class parser is the interpretation of logical conjunctions and disjunctions. A disjunction is the logical "OR" and a conjunction is the logical "AND." That may sound strange, but it's quite sensible to mathematicians. Take two circles and let them overlap a little; the area that overlaps is the conjunction, while the whole area is the disjunction.

Conjunctions and disjunctions must be specified in a specific order and in a specific way in a recursive grammar. Explaining the exact reasons for the structure of the grammar would be well outside the scope of this article. Try to understand the sequence of rules, and consult one of the many books on parsing and grammar for computers (some excellent books on this topic disguise themselves as being on the topic of compilers).

Each conjunction or disjunction returns an array reference with a subroutine that performs the logical operation specified, plus the operands of the operation (and those operands could be logical expressions themselves). All this is interpreted in the allowed_cfrun_atom() function.

The grammar is recursive, that is, input() leads to disj() which can lead to conj(), then unary(), then back to input().

Confusing? Unfortunately, yes. Recursive grammars such as Parse::RecDescent are sometimes difficult and confusing to novice programmers, but their many benefits outweigh these initial disadvantages. I again refer you to the abundant literature on computer parsing and grammar if you are interested in more on this topic; a link to the appropriate Google category is in the Resources section.



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: Resources

First published by IBM developerWorks


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