Comments (9)
One solution that "works" is rewriting the ifStmt
rule to:
ifStmt : 'if' ID stmt ('else' stmt | {_input.LA(1) != ELSE}?);
ELSE : 'else';
Unfortunately this solution has substantial drawbacks:
- The predicate is always evaluated, which means it could have unintended consequences if the keyword
else
is used in allowed in other contexts. To work properly, the predicate would need to only be used for disambiguating alternatives 1 and 2. - The grammar becomes tied to a particular target language.
from antlr4.
need to add this to execATN and execDFA to make it work. is it worth complexity?
// IF PREDS, MIGHT RESOLVE TO SINGLE ALT => SLL (or syntax error)
if ( D.configs.hasSemanticContext ) {
int nalts = decState.getNumberOfTransitions();
// Update DFA so reach becomes accept state with (predicate,alt)
// pairs if preds found for conflicting alts
BitSet altsToCollectPredsFrom = getConflictingAltsOrUniqueAlt(D.configs);
SemanticContext[] altToPred = getPredsForAmbigAlts(altsToCollectPredsFrom, D.configs, nalts);
if ( altToPred!=null ) {
List<DFAState.PredPrediction> predicates =
getPredicatePredictions(altsToCollectPredsFrom, altToPred);
input.seek(startIndex);
BitSet alts = evalSemanticContext(predicates, outerContext, true);
if ( alts.cardinality()==1 ) {
System.out.println("Full LL avoided");
return alts.nextSetBit(0);
}
}
}
and
// IF PREDS, MIGHT RESOLVE TO SINGLE ALT => SLL (or syntax error)
if ( s.predicates!=null ) {
System.out.println("DFA state has preds in DFA sim LL failover");
input.seek(startIndex);
BitSet alts = evalSemanticContext(s.predicates, outerContext, true);
if ( alts.cardinality()==1 ) {
System.out.println("Full LL avoided");
return alts.nextSetBit(0);
}
}
only 2 failovers to full avoided in execATN for entire parse of JavaLRParser.java, none in execDFA
from antlr4.
I was able to simplify the change through a basic refactoring (extract method), and submitted pull request parrt#126 to full address this.
from antlr4.
Pull request #124 introduces an sll
block option which is a much cleaner solution to this issue. Rather than use a predicate, the following rule may be used:
ifStmt : 'if' ID stmt (options{sll=true;} : 'else' stmt | );
ELSE : 'else';
from antlr4.
Hi,
I have a grammar with if/elseif/else statements (C# code generation), and I've run into an issue whereby a deeply nested if/else block (24 levels deep) that parses successfully in ANTLR 4.0 (albeit very slowly), now hangs on ANTLR 4.6.1 - it ran for over 2 hours without completing before I killed it. While trying to debug this issue, I ran across this thread and tried the sll = true option. However, the parsers generated with and without the sll=true option are identical. Is this option supported for C# code generation?
from antlr4.
from antlr4.
try 4.7?
from antlr4.
OK, will post to google groups - thanks.
@parrt - Latest NuGet package for code-generator is 4.6.1 so figured I'd use 4.6.1 for runtime as well to be in sync. Also ANTLR Runtime 4.6.1 package was later dated 5/30/2017 than ANTLR Standard Runtime 4.7.1 published on 5/28/2017. Will try 4.7.1 and see if it works.
from antlr4.
@gkudva The NuGet package (4.6.1) you referred to is part of a different repository. For issues with that (bugs or anything else) you should file an issue on GitHub here: https://github.com/tunnelvisionlabs/antlr4cs/
from antlr4.
Related Issues (20)
- Add some modifications for Dart runtime
- [antlr4-python3-runtime-4.13.1] ANTLR runtime and generated code versions disagree: 4.13.1!=4.11.2-SNAPSHOT HOT 14
- [Cpp] XPath.findAll does not produce the same result than python3 runtime
- Easy to reproduce baffling parse failure when using semantic predicates. Bug in parser generator? HOT 9
- [TypeScript]: The module “antlr4” or the associated type declarations were not found.ts(2307) HOT 2
- Go runtime should not depend on Go 1.22 yet HOT 4
- Go target: lexer constants with the private access if g4 filename starts with the lowercase
- [Feature request] Support range operator for tokens HOT 1
- Incorrect Handling of Syntax Errors in MySqlParser with Custom Error Strategy
- javascript runtime v4.13.1 not found api: getVocabulary()
- How to make antlr4 skip the intermediate errors and continue parsing backward when parsing Sql? HOT 1
- generate go lexer file error HOT 2
- Always error: line 1:0 mismatched input 'Token' expecting {<EOF>,Token} HOT 5
- ruleIndex property missing in ParserRuleContext itypescript type
- Syntax and other errors when using the Cpp runtime of ANTLR4 with grammars generated with antlr4.13.2-complete.jar HOT 4
- The syntax parsing time and performance did not meet my expectations HOT 2
- Go Target: Does ANTLR4 support canceled by context? HOT 5
- Adding an option to disable Tests HOT 1
- Antlr plugin has a problem in newest levels of intellij: ActionUpdateThreadOLD_EDT is deprecated and will be removed soon.
- Generate javascript parser with error HOT 2
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.