63776

ANTLR get first production

Question:

I'm using ANTLR4 and, in particular, the C grammar available in their repo (<a href="https://github.com/antlr/grammars-v4/blob/master/c/C.g4" rel="nofollow">grammar</a>). It seems that the grammar hasn't an initial rule, so I was wondering how it's possible to get it. In fact, once initialized the parser, I attach my listener, but I obtain syntax errors since I'm trying to parse two files with different code instructions:

int a;

int foo() { return 0; }

In my example I call the parser with "parser.primaryExpression();" which is the first production of the "g4" file. Is it possible to avoid to call the first production and get it automatically by ANTLR instead?

Answer1:

In addition to @GRosenberg's answer:

Also the rule enum (in the generated parser) contains entries for each rule in the order they appear in the grammar and the first rule has the value 0. However, just because it's the first rule in the grammar doesn't mean that it is the main entry point. Only the grammar author knows what the real entry is and sometimes you might even want to parse only with a subrule, which makes this decision even harder.

Answer2:

ANTLR provides no API to obtain the first rule. However, in the parser as generated, the field

public static final String[] ruleNames = ....;

lists the rulenames in the order of occurrence in the grammar. With reflection, you can access the method.

Beware. Nothing in the Antlr 'spec' defines this ordering. Simply has been true to date.

Recommend

  • How do you print an array of strings in MIPS?
  • How to initialise a huge array in MIPS assembly?
  • the answer is always wrong in this MIPS recursion . got 10, supposed to be 55
  • Switching to island mode on multi-character token
  • Overlapping rules - mismatched input
  • Use of Frame Pointer MIPS
  • ANTLR 4 extraneous input matching non lexer item
  • listView filter mistake
  • MIPS Assembly - Arrays?
  • Why we cant compare (bne-beq) a register with an immediate (Assembly-MIPS)?
  • Matlab: Dividing chunks of data randomly into equal sized sets
  • ANTLR4 Mutual left recursion
  • Having “Exception Value: The _imaging C module is not installed” with my Buildout/Python/Django/PIL
  • Mouse press slot for QComboBox - fill the items dynamically if user clicks the combobox
  • curl - How to escape < in parameter value
  • Edit assembly language code in Visual Studio while stepping through each statement
  • Upgrade project from WorkLight 6.1 to MobileFirst 7.1
  • Why doesn't a local variable live long enough for thread::scoped?
  • F#: In which memory area is the continuation stored: stack or heap?
  • What is the use of a session store?
  • Django model for a Postgres view
  • Using Laravel 5.4 pusher
  • How to retrieve information from antrun back to maven?
  • Getting media player state in windows phone 7
  • Debugging VB6 Code From Visual Studio 2010
  • Meteor helpers not available in Angular template
  • Build own AppleScript numerical error handling
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • Why do underscore prefixed variables exist?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?