[antlr-interest] Disregarding subrule ordering
Bryan Ewbank
ewbank at gmail.com
Thu Dec 1 08:57:03 PST 2005
To make a perhaps subtle distinction, this is a /semantic/ error, so
is more easily done in a separate pass (or are them fighting words?).
In other words, think about adding this to a treeparser, rather than
the initial parse.
The parser would simply consume parameters passively:
object :
KW_OBJECT^ ID params
;
params :
LEFT_CURLY^
( param0 | param1 | param2 ... )*
RIGHT_CURLY!
;
Now, the tree parser would look at the params object in the context of
the ID'd object and know (from a lookup table?) which elements are
available, as well as which have been seen...
object : #( OBJECT id:ID params[#id->getText()] );
params
[ const string objName ]
{
SetOfNames legalParameters = getLegalParameters(objName);
SetOfNames reqdParameters = getRequiredParams(objName)
SetOfNames seen;
string paramName;
}
: #( LEFT_CURLY
(
paramName=parameter[legalParameters]
{
// limited to one instance
if seenParameters contains paramName exists then
error ...
add paramName to seenParameters
}
)*
)
{
// force required to exist
for every p in reqdParameters
if seenParameters does_not_contain p then
error ...
}
;
parameter
[ const SetOfNames legalParameters ]
returns [ string paramName ]
:
#( ... ??? ... )
{
if legalParameters does_not_contain paramName then
error ...
}
;
On 12/1/05, Royne Borrud <royne.borrud at gmail.com> wrote:
> Hi.
> My goal is to parse a statement similar to this:
>
> object name {
> optionalParam1
> param1
> param3
> param2
> optionalParam3
> }
>
> My problem is that while I do not want to enforce a specific ordering
> of the parameters, I do want to make sure that all three parameters
> occur once and only once, and that the optional parameters occur once
> or not at all. Is this possible without adding specific code to handle
> this? I'm beginning to think not... :/
> If there isn't, has anyone got any hints for how to go about this?
>
More information about the antlr-interest
mailing list