|Re: Customizing actions in rules [message #959190 is a reply to message #958732]
||Fri, 26 October 2012 14:18
| Henrik Lindberg
Registered: July 2009
On 2012-26-10 9:12, Markus Wolf wrote:|
> Hi there,
> I would like to know if it is possible to customize the code/grammar of
> a rule at runtime?
> What I need is "Automatic Semicolon Insertion" as declared in the ECMA5
> specification in chapter 7.9.
> In short it means that if there is an offending next token which could
> not be matched and e.g. the next char (maybe hidden token) is a newline,
> then insert a semicolon and continue parsing.
> This is hard to achieve I think. I already tried to mark my explicit
> semicolons as optional but this is no easy solution. Even with a
> semantic predicate my grammar gets ambiguous.
> Can anyone help me?
There is no easy solution. You can't modify the grammar dynamically -
there is lots of code that has been generated.
The right approach is to make those keywords optional in the grammar.
I even do that for things that are required to be there, but I then
validate it instead of giving the user a syntax error - this because the
user often gets very confusing errors because of this problem.
I offer a quick fix for it.
In the serializer, if there is no node model, I emit the optional keyword.
The most difficult is to handle the ambiguities in your language. There
is no easy fix to that. You have to follow the rules of left-factoring
(look at expression based languages), use semantic predicates, and as a
last resort turn on backtracking.
You can take a look at cloudsmith / geppetto @ gitghub - it has a quite
forgiving grammar where semantics are checked in the validator.
One other alternative I can think of is if it is possible to detect the
missing tokens in the lexer, in that case, and external lexer can emit
something like IMPLIED_COMMA token. This is more difficult as you do not
have the semantic scope available - only the text sequence.
Powered by FUDForum
. Page generated in 0.01981 seconds