[antlr-interest] Idea: semantic grammar

Johannes Luber JALuber at gmx.de
Thu May 22 09:09:53 PDT 2008


> Hi,
> I don't know if there is anything like this about at the moment, or even
> if
> it's a good idea.  What I've been thinking about is having a grammar that
> defines the semantics of a language that parsers can then import.  It
> would
> basically be a list of the possible types of semantic error that could
> occur, along with any information necessary to generate an error message,
> and an action that creates the message.
> 
> >>From this a number of exception classes would be generated, along with a
> MessageCreator that would generate error messages based on the actions
> supplied.  Inside the parser grammar, when semantic predicates are
> declared,
> the type of error would be indicated, and from this the correct exception
> would be thrown.  The parser would then ask its ErrorReporter to handle
> the
> error, which would use the MessageCreator to create the error message, and
> would then add it to a list / print to the console / whatever.
> 
> >>From this, "catalogs" of pre-defined semantic errors could be provided
> along
> with the correct messages to display for these errors.  Obviously this
> wouldn't define the actual semantics of the language (since you'd still
> need
> to implement this in actions), but it would simplify the error reporting
> part since users would just import the correct semantics, then indicate
> when
> a certain semantic error should be thrown.
> 
> Any thoughts?
> Adam

A nice idea but probably infeasible. There are too many semantic differences for basically one thing - recently I read an article about "PHP sucks" and saw that references were handled in 4.3, 4.4 and 5 differently. Granted, 4.3 was leaky and 4.4 made it too complicated, so restricting options to sensible ones has a benefit, but if you want to be able to parse even the most idiotic things, you have to able to define arbitrary semantics. And then you can use the target language itself. 

Maybe Yggdrasil of Loring Craymer can do that in target independent manner, but it would be basically still Turing equivalent. At least you can do far more things target independent in Yggdrasil than in ANTLR - just a shame, that he didn't got around to finish it yet.

Johannes
-- 
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen! 
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer


More information about the antlr-interest mailing list