antlr

Wrong rule matched

别说谁变了你拦得住时间么 提交于 2019-12-24 18:52:25
问题 I have the following in the lexer INTEGER : DIGIT+; NOT: '!'; MINUS:'-'; PLUS:'+'; fragment DIGIT: '0'..'9'; I have the following in the parser expr: intLiteral | UnaryOp expr; intLiteral: (PLUS|MINUS)? INTEGER; UnaryOp: NOT|MINUS; When I use grun to test it with -2, I get it being matched to UnaryOp expr instead of just intLiteral. In other words, the minus sign is being detected as a UnaryOp. Why would this be occuring and is there a way to fix it? 回答1: The practice is to use all CAPITALS

ANTLR4 - Generate code from non-file inputs?

霸气de小男生 提交于 2019-12-24 17:26:13
问题 Where do we start to manually build a CST from scratch? Or does ANTLR4 always require the lex/parse process as our input step? I have some visual elements in my program that represent code structures. e.g. a square represents a class, while a circle embedded within that square represents a method. Now I want to turn those into code. How do I use ANTLR4 to do this, at runtime (using ANTLR4.js)? Most of the ANTLR examples seem to rely on lexing and parsing existing code to get to a syntax tree.

Canonicalizing token text in ANTLR

那年仲夏 提交于 2019-12-24 17:00:11
问题 Is there a way in ANTLR to mark certain tokens as having canonical output? For example, given the grammar (excerpt) words : FOO BAR BAZ FOO : [Ff] [Oo] [Oo] BAR : [Bb] [Aa] [Rr] BAZ : [Bb] [Aa] [Zz] SP : [ ] -> channel(HIDDEN); words will match "FOO BAR BAZ", "foo bar baz", "Foo bAr baZ", etc. When I call TokenStream#getText(Context) , it'll return the tokens' actual text concatenated together. Is there a way to "canonicalize" this output such that no matter what the input, all FOO tokens

ANTLR3 Dynamic quotes in lexer

流过昼夜 提交于 2019-12-24 16:32:57
问题 I need to match something like the Perl regexp matcher m/my regex!*/ where the quotes can be any character from a range. So the above is the same as m%my regex!*% A naive guess of a lexer rule would be REGEX: 'm' quote=. (~(quote))* quote; but that does not work, because the latter quote is not referring to the quote= but to some rule. I can do it with a lot of own code, like REGEX: 'm' quote=. { ... implement the loop and final match myself ... } ; but somehow I think there should be a

Are there any good examples to references where setBuildParseTree = false?

雨燕双飞 提交于 2019-12-24 13:27:38
问题 I'm using an antlr for a simple CSV parser. I'd like to use it on a 29gig file, but it runs out of memory on the ANTLRInputStream call: CharStream cs = new ANTLRInputStream(new BufferedInputStream(input,8192)); CSVLexer lexer = new CSVLexer(cs); CommonTokenStream tokens = new CommonTokenStream(lexer); CSVParser parser = new CSVParser(tokens); ParseTree tree = parser.file(); ParseTreeWalker walker = new ParseTreeWalker(); walker.walk(myListener, tree); I tried to change it to be an unbuffered

ANTLR: Extraneous input problem

孤者浪人 提交于 2019-12-24 12:06:40
问题 I have a grammar file Bookings.g and the file I try to parse with that grammar. Why am I getting these errors? line 1:12 extraneous input '"pcc"' expecting String line 3:0 mismatched input '}' expecting ScenarioPart1 line 7:13 mismatched input '"R"' expecting String line 9:8 mismatched input '"Doll"' expecting String line 15:8 mismatched input '"Train"' expecting String line 21:8 mismatched input '"Ball"' expecting String line 34:0 missing EOF at '}' Grammar grammar Bookings; @members {

linking xtext editor support with external ANTLR parser

时光怂恿深爱的人放手 提交于 2019-12-24 12:01:49
问题 My current project (name it IoTSuite) takes high-level specifications, parses them, and generates code in Java and Android. In this project, I have written ANTLR grammar to parse the high-level specification and I have used StringTemplate for the code generator. However, due to nice editor support and syntax coloring features, I have used the xtext grammar(same as the ANTLR grammar, but it has been written in xText). Now, I perform the following three steps: Step 1: I have written xtext

What is the wrong with the simple ANTLR grammar?

假装没事ソ 提交于 2019-12-24 10:31:07
问题 I am writing an ANTLR grammar to parse a log files, and faced a problem. I have simplified my grammar to reproduce the problem as followed: stmt1: '[ ' elapse ': ' stmt2 ; stmt2: '[xxx' ; stmt3: ': [yyy' ; elapse : FLOAT; FLOAT : ('0'..'9')+ '.' ('0'..'9')* ; When I used the following string to test the grammar: [ 98.9: [xxx I got the error: E:\work\antlr\output\__Test___input.txt line 1:9 mismatched character 'x' expecting 'y' E:\work\antlr\output\__Test___input.txt line 1:10 no viable

Parse Parenthesis as atoms ANTLR

帅比萌擦擦* 提交于 2019-12-24 10:19:59
问题 I'm trying to match balanced parentheses such that, a PARAMS tree is created if a match is made, else the LPARAM and RPARAM tokens are simply added as atoms to the tree... tokens { LIST; PARAMS; } start : list -> ^(LIST list); list : (expr|atom)+; expr : LPARAM list? RPARAM -> ^(PARAMS list?); atom : INT | LPARAM | RPARAM; INT : '0'..'9'+; LPARAM : '('; RPARAM : ')'; At the moment, it will never create a PARAMS tree, because in the rule expr it will always see the end RPARAM as an atom,

ANTLR parse assignments

北城余情 提交于 2019-12-24 09:22:38
问题 I want to parse some assignments, where I only care about the assignment as a whole. Not about whats inside the assignment. An assignment is indiciated by ':=' . (EDIT: Before and after the assignments other things may come) Some examples: a := TRUE & FALSE; c := a ? 3 : 5; b := case a : 1; !a : 0; esac; Currently I make a difference between assignments containing a 'case' and other assignments. For simple assignments I tried something like ~('case' | 'esac' | ';') but then antlr complained