Hi guys,
I have to choose an option on how to proceed with the grammar
definition for expressions. Consider the next expression to be parsed:
a < b
with the rule:
equality_expr : relational_expr (('<' | '>' | '<=' | '>=' )^
relational_expr)* ;
and relational_expr eventually converting to 'IDENTIFIER', the result is:
Rootnode: undetermined type with value '<'
Subnodes:
1 type IDENTIFIER, value 'a'
2 type IDENTIFIER, value 'b'
And the issue is in the 'undetermined type' of the root node, which is
enumerated at compile time. There is however a table which translates
the type-code to the type name (like 'IDENTIFIER' or '<') An other
issue is that all operators (that is (('<', '>', '<=' and '>=', but
also '*', '+' etc) are handled in the same manner at code generation
and it would be easy to have a single match to an OPERATOR. However,
there does not seem a way to assign type "OPERATOR' to the root node
and retaining its content.
So what are the options?
1) At startup, lookup all relevant node type names (like '<') in the
string table and store the type-code in variables. I think this will
rule out the use of switch statements (case needs cexpr, right?) and
thus not recommended.
2) Create a script that scans the string table and create
corresponding defines (takes an other script, less difficult to
understand and maintain).
3) Add a rewrite-rule that adds an 'OPERATOR' node above the tree, so:
Rootnode: type "OPERATOR' with no value
Subnode: undetermined type with value '<'
Sub-Subnodes:
1 type IDENTIFIER, value 'a'
2 type IDENTIFIER, value 'b'
The rule would look something like (not actually tested, but the
guru's say it will solve my problem):
equality_expr : relational_expr (('<' | '>' | '<=' | '>=' )
relational_expr)* ->^(OPERATOR relational_expr (('<' | '>' | '<=' |
'>=' ) relational_expr )*) ;
This is easy to scan (just one node to detect) and allows generic
processing (which is useful in this case but I don't know if it
universal useful). It does however add an other level to the tree
which is not very neat.
4) Create lex tokens for each operator (so LESS, LESS_EQUAL, PLUS,
MINUS etc etc). This way, a node with value '<' gets type LESS and
becomes easy detectable and can be used in a switch statement.
Downside of this is that we need quite a few extra lex tokens, which
makes the grammar larger and less easy to learn by heart. An other
issue is that the rules IMO are more dificult to read with those
tokens. This may be more relevant for non-native English speakers:
recognising a special character is easier than knowing it by its name.
equality_expr : relational_expr ((LESS| MORE | GREATER_EQUAL |
GREATER_EQUAL )^ relational_expr)* ;
Please tell me what option you would prefer.
Joep
--
You received this message because you are subscribed to the Google Groups
"jallib" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/jallib?hl=en.