diff --git a/doc/index.md b/doc/index.md index 1e5ea370c..979de3e60 100644 --- a/doc/index.md +++ b/doc/index.md @@ -45,9 +45,9 @@ This documentation is a reference and summarizes grammar syntax and the key sema * [Semantic Predicates](predicates.md) -* Options +* [Options](options.md) -* ANTLR Tool Command Line Options +* [ANTLR Tool Command Line Options](tool-options.md) * Runtime Libraries and Code Generation Targets diff --git a/doc/options.md b/doc/options.md new file mode 100644 index 000000000..acac918d9 --- /dev/null +++ b/doc/options.md @@ -0,0 +1,52 @@ +# Options + +There are a number of options that you can specify at the grammar and rule element level. (There are currently no rule options.) These change how ANTLR generates code from your grammar. The general syntax is: + +``` +options { name1=value1; ... nameN=valueN; } // ANTLR not target language syntax +``` + +where a value can be an identifier, a qualified identifier (for example, a.b.c), a string, a multi-line string in curly braces `{...}`, and an integer. + +## Grammar Options + +All grammars can use the following options. In combined grammars, all options except language pertain only to the generated parser. Options may be set either within the grammar file using the options syntax (described above) or when invoking ANTLR on the command line, using the `-D` option. (see Section 15.9, [ANTLR Tool Command Line Options](https://theantlrguy.atlassian.net/wiki/display/ANTLR4/ANTLR+Tool+Command+Line+Options).) The following examples demonstrate both mechanisms; note that `-D` overrides options within the grammar. + +## Rule Options + +There are currently no valid rule-level options, but the tool still supports the following syntax for future use: + +``` +rulename +options {...} + : ... + ; +``` + +## Rule Element Options + +Token options have the form `T` as we saw in Section 5.4, [Dealing with Precedence, Left Recursion, and Associativity](http://pragprog.com/book/tpantlr2/the-definitive-antlr-4-reference). The only token option is assocand it accepts values left and right. Here’s a sample grammar with a left-recursive expression rule that specifies a token option on the `^` exponent operator token: + +``` +grammar ExprLR; + +expr : expr '^' expr + | expr '*' expr // match subexpressions joined with '*' operator + | expr '+' expr // match subexpressions joined with '+' operator + | INT // matches simple integer atom + ; + +INT : '0'..'9'+ ; +WS : [ \n]+ -> skip ; +``` + +Semantic predicates also accept an option, per [Catching failed semantic predicates](http://pragprog.com/book/tpantlr2/the-definitive-antlr-4-reference). The only valid option is the fail option, which takes either a string literal in double-quotes or an action that evaluates to a string. The string literal or string result from the action should be the message to emit upon predicate failure. + +``` +ints[int max] + locals [int i=1] + : INT ( ',' {$i++;} {$i<=$max}? INT )* + ; +``` + +The action can execute a function as well as compute a string when a predicate fails: `{...}?` diff --git a/doc/tool-options.md b/doc/tool-options.md new file mode 100644 index 000000000..fe5d4a399 --- /dev/null +++ b/doc/tool-options.md @@ -0,0 +1,161 @@ +# ANTLR Tool Command Line Options + +If you invoke the ANTLR tool without command line arguments, you’ll get a help message: + +```bash +$ antlr4 +ANTLR Parser Generator Version 4.5 + -o ___ specify output directory where all output is generated + -lib ___ specify location of grammars, tokens files + -atn generate rule augmented transition network diagrams + -encoding ___ specify grammar file encoding; e.g., euc-jp + -message-format ___ specify output style for messages in antlr, gnu, vs2005 + -long-messages show exception details when available for errors and warnings + -listener generate parse tree listener (default) + -no-listener don't generate parse tree listener + -visitor generate parse tree visitor + -no-visitor don't generate parse tree visitor (default) + -package ___ specify a package/namespace for the generated code + -depend generate file dependencies + -D