28 ms·
In many parser generators (e.g. Yacc and ANTLR), a grammar author can specify the language semantics by including semantic actions inside the grammar. A semanti
by CalChris 9y ago
In many parser generators (e.g. Yacc and ANTLR), a grammar author can specify the language semantics by including semantic actions inside the grammar. A semantic action is a snippet of code — typically written in a different language —that produces a desired value or effect each time a particular rule is matched.
Actually, the need for that went away with ANTLR4. The grammar is now all grammar (and lexer) and the semantic actions are listeners or walkers written separately calling or overriding methods and classes generated from the grammar.
Much cleaner that way.
- bd82 9y agoIt did not exactly went away. It is still possible to embed semantics inside an Antlr4 grammar. For example see the Antlr4 EcmaScript grammar sample: https://github.com/antlr/grammars-v4/tree/master/ecmascript https://github.com/antlr/grammars-v4/tree/master/ecmascript which uses embedded code to solve the RegExp vs division operator ambiguity. Another scenario when embedding code could be preferred is optimizing for maximum performance as abstractions normally come at a performance overhead. I do agree that the default approach should be to separate the semantics unless there is a very good reason why not to...
- CalChris 9y agoI stand corrected. I rewrote my grammar with ANTLR4 and gutted all the embedded semantics. That was a good day. I'm going to hit reply now and then I'm going to take out my personal neuralyzer and forget that I ever found out that you can still embed.