I want to execute the example from the ANTLR python target page, but I have no idea, how to define the startRule()
-Function to enter the code to a specific rule.
Could somebody give an example to a startRule()
-Definition?
The startRule() function is just a parser rule in your grammar. Let's say your grammar, kept in a file called MyGrammar.g4
, looks like this:
grammar MyGrammar;
// A parser rule that matches zero or more ANY_CHAR tokens
// followed by the EOF (end-of-file)
startRule
: ANY_CHAR* EOF
;
// A lexer rule that matches a single character
ANY_CHAR
: .
;
then you could call startRule()
on the generated parser.
The ANTLR parser doesn't really have the concept of a start rule - you may choose any rule as your start rule when invoking it (by calling the appropriate method, e.g. start()
if you want to start at the rule start
).
This can be useful if you need to start at different rules for different use-cases.
It might still be a good idea to call your start rule something like startRule
in your grammar for readability, but it's not mandatory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With