Next: parser tooling, Previous: parser combinators, Up: parsing [Index]
This is a configureable expression parser that can be used for prolog parsing as well as parsing C and C++ expressions. To note is that the data is functionally updated and hence one can store the state at will and later reinstate the parser. The parser ideom is a direct translation of the commonly described prolog parser written in prolog to guile-log. It is then extended to allow ternial operators like ?
in c as well. To use it load the module (logic guile-log parsing operator-parser)
(make-opdata)
, initiates a datastructure to containe needed operator data. The result is a fluid pointing to a functional datastructure.
(add-operator data type name level lf)
, This will add operator data to the opdata fluid data
. type
is the type of operator e.g. any of xf,yf,fx,fy,xfx,xfy,yfx
, as of the standard meaning of these operators in prolog. name
is a string or symbol representing the name of the operator and level
is the anti binding strength of the operator e.g. higher value bind less. lf
is a parser function that represents possible values to the right of the op-symbol matched, typically this will be tok-ws
, but also for e.g. ?
it can be a parser function matching the middle part of in this operayor which ends with :
.
(rem-operator data type name)
, removes from the opdata fluid data
, the slot represented by the type type
and the operator name name
.
(mk-operator-expression atom rest-atom data)
, will return a parser meta function representing the operator configuration in data
, with atom parser function atom
and a secondary choised atom if that fails in rest-atom
. The returned meta parser function, say ff
is typically turned into a parser function for the expression through:
(ff high-level)
, with high-level
, the highes level found in the table.
The generator parser f
can then be used to match an expression and outpits a syntactic tree with nodes according to e.g.
(parse ``1 + -2'' f) --> (('xfy ``+'' _ _) (#:number 1) (('yf ``-'' _ _) (#:number 2) 4 0) 2 0)
That is a binary oparator is of the form,
((type operator-name level _) term-left term-right column line)
And a unary operator is matched by
((type operator-name level _) term column line)
.
Next: parser tooling, Previous: parser combinators, Up: parsing [Index]