ANTLR 4 tree inject/rewrite operator

As mentioned by Sam (280Z28), ANTLR 4 does not have rewrite operators. When generating the parser, ANTLR 4 creates some listener classes that you can use to listen for “enter” and “exit” events of all parser rules. Also, ANTLR 4 supports “direct left recursive rules”, so your expression rules can be defined in a single … Read more

Build symbol table from grammar [closed]

A symbol table is just a versioned map of id’s to values. This is one solution, using a push and pop of scopes as the versioning mechanism — push a scope on entry of a scope defining rule and pop on exit. package net.certiv.metal.symbol; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.Map; import net.certiv.metal.types.ScopeType; import net.certiv.metal.util.Strings; public … Read more

When is EOF needed in ANTLR 4?

You should include an explicit EOF at the end of your entry rule any time you are trying to parse an entire input file. If you do not include the EOF, it means you are not trying to parse the entire input, and it’s acceptable to parse only a portion of the input if it … Read more

When is EOF needed in ANTLR 4?

You should include an explicit EOF at the end of your entry rule any time you are trying to parse an entire input file. If you do not include the EOF, it means you are not trying to parse the entire input, and it’s acceptable to parse only a portion of the input if it … Read more

Using ANTLR 3.3?

Let’s say you want to parse simple expressions consisting of the following tokens: – subtraction (also unary); + addition; * multiplication; / division; (…) grouping (sub) expressions; integer and decimal numbers. An ANTLR grammar could look like this: grammar Expression; options { language=CSharp2; } parse : exp EOF ; exp : addExp ; addExp : … Read more

How does the ANTLR lexer disambiguate its rules (or why does my parser produce “mismatched input” errors)?

In ANTLR, the lexer is isolated from the parser, which means it will split the text into typed tokens according to the lexer grammar rules, and the parser has no influence on this process (it cannot say “give me an INTEGER now” for instance). It produces a token stream by itself. Furthermore, the parser doesn’t … Read more