Comments (4)
Since this issue is from 2012 I'm wondering if there is any alternative to this that has been implemented (since this issue is still open)?
Does @sharwell 's branch resolve this? Is there any chance of merging that?
from antlr4.
A few comments:
createSContext
is probably a better name since it's creating a newSContext
as opposed to a newS
.- A general
createContext
rule like the following would be useful for implementing a parser interpretation mode. - For
createContext
to be useful, anaction
dispatch method like the one used in generated lexers will be necessary for executing user-specified actions. An additional method returning anObject
will be necessary for evaluating arguments to pass as necessary. - To prevent unnecessary boxing/unboxing overhead in standard parsing, argument evaluation can be inlined like it is now. Action evaluation would not suffer from the same problem, and as long as the generated
ruleName_action
methods are declaredfinal
the performance impact of dispatching the call would likely be negligible.
public ParserRuleContext<Token> createContext(int ruleIndex,
int outerAlt,
ParserRuleContext<Token> parent,
int state,
Object[] args) {
switch (ruleIndex) {
case RULE_s:
assert outerAlt == 0 : "Shouldn't recreate context for unlabeled alternatives.";
return createSContext(parent, state);
case RULE_e:
assert outerAlt >= 0 && outerAlt <= 3 : "Invalid alternative.";
assert outerAlt == 0 || parent != null : "Parent must be specified for labeled alternatives.";
switch (outerAlt) {
case 0:
return createEContext(parent, state, (int)args[0]);
case 1:
return createMultContext((EContext)parent);
case 2:
return createAddContext((EContext)parent);
case 3:
return createIntContext((EContext)parent);
default:
break;
}
break;
}
throw new IllegalArgumentException(String.format("Cannot create context for invalid rule/alt combination %d/%d.", ruleIndex, outerAlt));
}
from antlr4.
I strongly support a factory as it allows to use other allocation mechanisms. In addition to contexts, the factory should also be used to create lists for multi valued tokens. I.e. in a rule
x : (xs+=x)*
the list xs in the XContext class should be created by a factory method instead of a hardcoded new ArrayList<...>(); this would allow to replace list implementation or even recycle some lists which are no longer needed.
@sharwell: I do not see why such generic createContext method is necessary. Can you elaborate that.
from antlr4.
There are a number of constructs which are hard-coded into the generated rule methods which currently make it impossible to build an "interpreter mode" parser. In order to build an interpreter, it will be necessary to have a delegating method for context creation and action execution which can be called from inside the interpreter with simply a rule index and an alt number (context creation) or action index (action execution).
from antlr4.
Related Issues (20)
- [go] performance downgrade a lot after upgrade antlr from 4.9 to 4.13.1 HOT 8
- [C++] Building grammar with rule named "match" causes errors
- JavaScript runtime fails to parse input that can be parsed by `grun` with same grammar HOT 1
- Regex
- Go target: missing constructor call to the superclass of the generated parser or lexer HOT 2
- limits like DIGIT {min,max} in patterns HOT 1
- antlr4 lacks automatic module name, which is inconsistent to antlr4-runtime
- ANTLR 4.2 and later fail to report error with mislocated 'assoc'
- ANLTR4 Pascal target for parsing Pascal Object Notation HOT 8
- TSQL select query not parsed when it inside the quotes HOT 1
- [go target] Build errors with PartiQL parser HOT 8
- setErrorHandler method is missing from Parser class in TypeScript target
- _sharedContextCache is not thread safe HOT 1
- Unexcept parse
- Use the new version antlr4-python3-runtime 4.13.1 import latex2sympy2 error
- Use the new version antlr4-python3-runtime 4.13.1 import latex2sympy2 error HOT 1
- Lexing Issue in ANTLR4 Grammar for Fortran 2018: Token Misclassification HOT 1
- Windows CPP Symbol Conflicts (EMPTY, WIN16, WIN32, WIN64)
- Is this project out of date?
- ANTLR doesn't generate needed methods for C++ HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from antlr4.