What’s better, ANTLR or JavaCC? [closed]

There are a couple of alternatives you shouldn’t rule out: JParsec is a parser combinator framework that allows you to construct your parser entirely from code. Scala’s parser combinator framework addresses a similar concern; however, Scala’s syntax makes all of this much more readable. Then there’s also the parser combinator framework done by John Metsker, … Read more

How to specify a target package for ANTLR?

ANTLR provides a header tool which allows you to include package and imports. You include this in your *.g grammar file: @header { package org.xmlcml.cml.converters.antlr; import java.util.HashMap; } And you may need it in the Lexer as well: @lexer::header {package org.xmlcml.cml.converters.antlr;} and in case you need to add some members and code: @members { HashMap<String, … Read more

If/else statements in ANTLR using listeners

By default, ANTLR 4 generates listeners. But if you give org.antlr.v4.Tool the command line parameter -visitor, ANTLR generates visitor classes for you. These work much like listeners, but give you more control over which (sub) trees are walked/visited. This is particularly useful if you want to exclude certain (sub) trees (like else/if blocks, as in … 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

What is a ‘semantic predicate’ in ANTLR?

ANTLR 4 For predicates in ANTLR 4, checkout these stackoverflow Q&A’s: Syntax of semantic predicates in Antlr4 Semantic predicates in ANTLR4? ANTLR 3 A semantic predicate is a way to enforce extra (semantic) rules upon grammar actions using plain code. There are 3 types of semantic predicates: validating semantic predicates; gated semantic predicates; disambiguating semantic … Read more

What does “fragment” mean in ANTLR?

A fragment is somewhat akin to an inline function: It makes the grammar more readable and easier to maintain. A fragment will never be counted as a token, it only serves to simplify a grammar. Consider: NUMBER: DIGITS | OCTAL_DIGITS | HEX_DIGITS; fragment DIGITS: ‘1’..’9′ ‘0’..’9’*; fragment OCTAL_DIGITS: ‘0’ ‘0’..’7’+; fragment HEX_DIGITS: ‘0x’ (‘0’..’9′ | … Read more

How to convert a String to its equivalent LINQ Expression Tree?

Would the dynamic linq library help here? In particular, I’m thinking as a Where clause. If necessary, put it inside a list/array just to call .Where(string) on it! i.e. var people = new List<Person> { person }; int match = people.Where(filter).Any(); If not, writing a parser (using Expression under the hood) isn’t hugely taxing – … Read more