InitialSection myvariable [3]
MainSection Description 1 "Set the variable to 1" Action 1 myvariable [1] Action 2: Import from file "c:\file.txt"
Finish myvariable
@Override
public void exitAction(ActionContext ctx)
String text = ctx.getText();
ActionParser actParser =new ActionParser(text);
List<Declaration> decl = actParser.getDeclarations();
...
}
public void exitAction(ActionContext ctx)
In principle it works. But what I do not like about this solution is that I have to make use of the getText()-method. I do not know what is the best resp. the preferable way. The ActionParser as well as the MainParser have the same grammar so both have the method.
public void exitAction(ActionContext ctx)But hence everything gets generated, the ActionContext object is different and I cannot pass the context to the ActionParser. I think this solution is not very nice due to redundant elements. Is there a better way to solve this problems?
--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Hi,
grammar MyDSLParser;
import MainSection, Finish;
mydsl : initialsection mainsection finish ;
grammar MainSectionParser;
import MainSection;
content : mainsection ;
Hi,thanks for your answers and sorry for my late response, I've been in holiday over new year. I changed my implementation so that I pass the tokenstream from one parser the other. But the problems remains the same. I have my main parser which can read the whole file derived from the grammar MyDSL.
grammar MyDSLParser;import MainSection, Finish;mydsl: initialsectionmainsectionfinish;And I have additional parser for parsing the subsections of my dsl e.g. for parsing the mainsection based on the same grammar specified in "MainSection.g4".
grammar MainSectionParser;import MainSection;content : mainsection;
Based on the "MainSectionParser.g4" I generate a parser for reading the section "mainsection" to which I pass the tokenstream. But hence I generate two parsers, al the elements in the section "mainsection" get generated two times e.g. MainSectionParserBaseListener as well as MyDSLParserBaseListener have a method exitAction(ActionContext ctx). But the ActionContext is different for both of them although it is the same grammar. This is the problem that I would like to solve more elegant.
Nevertheless I know it works by passing the tokenstream, I don't think that this solution is elegant.
--
You don't need separate grammars unless you really would have a need to parse these sections on their own in some other context and for some reason cannot just start the parse at the rule in the general grammar, which seems an unlikely situation.
You don't need separate grammars unless you really would have a need to parse these sections on their own in some other context and for some reason cannot just start the parse at the rule in the general grammar, which seems an unlikely situation.Thanks for your hints, I'll have a look at this. But the case you mentioned above, is exactly the use case I have and the reason why I want to have to separate parsers which can work together as one. Otherwise I could solve this using abstractions in the code. I need to be able to parse incomplete parts of the DSL e.g. only the mainsection, verify its syntax and generate code from an incomplete specification, which will be used later somewhere else.
--
exitMainsection(MainsectionContext ctx)
subparser = new MainsectionParser(ctx.getOriginalText(start,end));
subparser = new MainsectionParser(ctx);