What is a Parser?
A parser is a piece of software that evaluates the syntax of a script when it is executed on a web server. For scripting languages used on the web, the parser works like a compiler might work in other types of application development environments. In the same way a compiler can create object code to be used on a computer from source code, a parser can turn scripted code into readable object code or display syntax errors found when the code is executed in a browser. Parsers are commonly used in script development because they can evaluate code when the script is executed and do not require that the code be compiled first. The compilation of source code and its execution happens all in one step as opposed to a C++ or other binary compiler in which the source code is compiled into object code and then this code must be installed or run separately. The biggest difference between a parser and compiler is the environment and language which they deal with. For example, many scripts are parsed
A parser breaks data into smaller elements, according to a set of rules that describe its structure. Most data can be decomposed to some degree. For example, a phone number consists of an area code, prefix and suffix; and a mailing address consists of a street address, city, state, country and zip code. Consider the following data: (800) 555-1234 (123) 555-4321 (999) 888-7777 Because of the way these items are formatted, we recognize them as a list of phone numbers. The structure of these items may be described informally as: “A phone number consists of a three-digit area code, enclosed by parentheses, followed by a three-digit prefix, followed by a dash, followed by a four-digit suffix.” This description can be expressed more formally as a grammar. A grammar is a set of rules that describe the structure, or syntax, of a particular type of data. The following grammar describes the syntax of phone numbers: phone_number ::= “(” area_code “)” prefix “-” suffix; area_code ::= numeric; pref
Once upon a time there was a org.apache.cocoon.components.parser.Parser, but now it’s deprecated. OK, so I think I should use the Excalibur parser component, which has recently been split into a SAX parser and a DOM parser. Looking in excalibur-xmlutil-20030122.jar, I found this class: org.apache.excalibur.xml.sax.Parser. Maybe I can use that. This is my first try: org.apache.excalibur.xml.sax.Parser saxParser = null; try { saxParser = (org.apache.excalibur.xml.sax.Parser) manager.lookup(org.apache.excalibur.xml.sax.Parser.ROLE); } finally { if (saxParser != null) manager.release(saxParser); } Unfortunately, it doesn’t compile, because Parser does not implement Component, so I resolve the problem with a cast: if (saxParser != null) manager.release((Component) saxParser); I’m starting to feel a little bit confused, because I was not aware that in Java you could get away with that at compile time. Maybe I should have a look at the JLS, but since I’m lazy and my code runs, I figure I’d be