|
|
ProGrammar Architecture
The ProGrammar architecture consists of the following components:
|
Parse Engine
|
The parse engine is a runtime component that scans text
from the input file and parses it according to the rules of
the grammar.
|
|
Parse Tree
|
The parse engine produces a parse tree data structure that
represents the contents of the input file. Parse tree nodes
map grammar symbols to items in the input.
|
|
API
|
Client programs interact with the parse engine by calling
API methods at runtime. These methods are used to load
grammars, parse data, process errors, and retrieve data
elements from the parse tree.
|
Platforms and Languages Supported
The runtime parse engine is currently available on 32-bit Windows platforms
as an ActiveX control, which can be called from any COM-enabled development
environment, including Visual C++, Visual Basic, C++Builder
and Delphi.
A native C++ interface is also provided, allowing Visual C++ applications
to directly call the parse engine dynamic link library or to link with the
static library.
Reusable Parsers
ProGrammar helps you to build autonomous parsers
that can be used across multiple projects, programming
languages, and platforms. Once you've developed an SQL parser, for example,
it can be used without modification by applications written in C++,
Visual Basic, or Delphi. This capability is supported in the following ways:
 |
The ProGrammar API is consistent across all programming environments
that are currently supported, and will remain consistent
for environments that will be supported in the future.
|
 |
GDL is a platform-neutral. Since the purpose of GDL is to express the
syntax of data, it does not assume anything about the programming
language or development environment used to call the parser. Hence, grammar
definitions do not contain programming language statements, which would
tie the grammar to a particular language.
|
 |
Parsing is logically separated from application semantics. ProGrammar
draws a clear distinction between how data is parsed and how parsed data
is used by the calling application. The role of the parser is to parse
data according to its syntax, while it is the job of each application to
decide what should be done with the results. By enforcing this distinction,
ProGrammar promotes the development of parsers that are independent of
any particular application.
|
Application-ProGrammar Interface (API)
The following Visual Basic example code parses an HTML page,
then searches the resulting parse tree for all hyperlinks (i.e., occurrences
of the "HREF" attribute in the document). The value of each hyperlink is
inserted into a list box for subsequent processing by the program. Note
that code related to error checking has been omitted, for brevity.
' Create an interface to the parser
Dim parser As Pgmr
Set parser = new Pgmr
' Load the HTML grammar
parser.SetGrammar ("HTML.GMR")
' Load the HTML page that will be parsed
parser.SetInputFilename ("index.htm")
' Parse the HTML page
parser.Parse
If parser.Status = pgStatusComplete Then
' Search the tree for all 'attr_name' nodes having a value of "href"
SearchID = parser.StartSearch ("/.*attr_name=""href""", 0)
Do
' Find the next node that matches the pattern
hrefNodeID = parser.FindNext (SearchID)
If hrefNodeID <> 0 Then
' find the attr_value node, relative to the
' current href node
attrNodeID = parser.Find ("^.attr_value", hrefNodeID)
' get the value of this node
AttrValue = parser.GetValue (attrNodeID)
' variable 'AttrValue' now contains the hyperlink.
' add the hyperlink to a list box
HTMLExampleForm.List1.AddItem (AttrValue)
End If
Loop Until hrefNodeID = 0
' inform the parser that we are done searching
parser.EndSearch (SearchID)
Else
' TODO: handle errors
End If
|
|