antlr

Getting plain text in antlr instead of tokens

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 04:38:27
问题 I'm trying to create a parser using antlr. My grammar is as follows. code : codeBlock* EOF; codeBlock : text | tag1Ops | tag2Ops ; tag1Ops: START_1_TAG ID END_2_TAG ; tag2Ops: START_2_TAG ID END_2_TAG ; text: ~(START_1_TAG|START_2_TAG)+; START_1_TAG : '<%' ; END_1_TAG : '%>' ; START_2_TAG : '<<'; END_2_TAG : '>>' ; ID : [A-Za-z_][A-Za-z0-9_]*; INT_NUMBER: [0-9]+; WS : ( ' ' | '\n' | '\r' | '\t')+ -> channel(HIDDEN); SPACES: SPACE+; ANY_CHAR : .; fragment SPACE : ' ' | '\r' | '\n' | '\t' ;

How to have both function calls and parenthetical grouping without backtrack

妖精的绣舞 提交于 2019-12-11 03:54:52
问题 Is there any way to specify a grammar which allows the following syntax: f(x)(g, (1-(-2))*3, 1+2*3)[0] which is transformed into (in pseudo-lisp to show order): (index ((f x) g (* (- 1 -2) 3) (+ (* 2 3) 1) ) 0 ) along with things like limited operator precedence etc. The following grammar works with backtrack = true, but I'd like to avoid that: grammar T; options { output=AST; backtrack=true; memoize=true; } tokens { CALL; INDEX; LOOKUP; } prog: (expr '\n')* ; expr : boolExpr; boolExpr :

ANTLR3 C Target - parser return 'misses' out root element

≡放荡痞女 提交于 2019-12-11 03:45:35
问题 I'm trying to use the ANTLR3 C Target to make sense of an AST, but am running into some difficulties. I have a simple SQL-like grammar file: grammar sql; options { language = C; output=AST; ASTLabelType=pANTLR3_BASE_TREE; } sql : VERB fields; fields : FIELD (',' FIELD)*; VERB : 'SELECT' | 'UPDATE' | 'INSERT'; FIELD : CHAR+; fragment CHAR : 'a'..'z'; and this works as expected within ANTLRWorks. In my C code I have: const char pInput[] = "SELECT one,two,three"; pANTLR3_INPUT_STREAM pNewStrm =

AST rewrite rule with “ * +” in antlr

懵懂的女人 提交于 2019-12-11 03:24:10
问题 I'm having a trouble about rewrite rule to convert from parsing tree into AST tree in antlr. Here's my antlr code: grammar MyGrammar; options { output= AST; ASTLabelType=CommonTree; backtrack = true; } tokens { NP; NOUN; ADJ; } //NOUN PHRASE np : ( (adj)* n+ (adj)* -> ^(ADJ adj)* ^(NOUN n)+ ^(ADJ adj)* ) ; adj : 'adj1'|'adj2'; n : 'noun1'; When I input "adj1 noun1 adj2" , the result of parse tree like this: But the AST tree after rewrite rule seem not exactly like the parse tree, the adj is

Evaluate IF subtree in homogeneus AST

别来无恙 提交于 2019-12-11 03:16:27
问题 I'm building a tree walker for an homogeneus AST (all nodes have same class), what's the correct way to evaluate an if statement? My AST for if are like this: I would something that when parses an IF block, evaluates sequentially his CONDBLOCK children and if one of them is true, the tree walker doesn't evaluate the remaining. More clearly, my tree walker is something like: ifStat : ^(IF { jump=false; } condition* defcond?) condition : { if (jump) return retval; } ^(CONDBLOCK exp block) {

Why does ANTLR require all or none alternatives be labeled?

為{幸葍}努か 提交于 2019-12-11 03:05:55
问题 I'm new to ANTLR. I just discovered that it is possible to label each alternative in a production like so: foo : a # aLabel | b # bLabel | // ... ; However, I find it unpleasant that all or none alternatives must be labeled. I needed to label just 2 alternatives of a production with 20+ branches recently, and I ended up labelling each of the others # stubLabel . Is there any reason why all or none have to be labeled? 回答1: As soon as you add a label ANTLR4 will no longer generate a context

is it possible to pass a tree (data structure) to clojure and work on it?

独自空忆成欢 提交于 2019-12-11 03:03:10
问题 tree structure build by from java code passed to clojure REPL and then using that data structure work on it. this tree is formed by ANTLR after parsing the code. 回答1: Clojure's interop with java is very good. Is there some reason you can't call ANTLR from Clojure and simply used the generated tree that way? http://clojure.org/java_interop 回答2: you could include the java class that produces the tree in your project and call it from the REPL to get an Object. then Clojure that Object to your

What to use in ANTLR4 to resolve ambiguities in more complex cases (instead of syntactic predicates)?

蹲街弑〆低调 提交于 2019-12-11 02:56:29
问题 In ANTLR v3, syntactic predicates could be used to solve ambiguitites, i.e., to explicitly tell ANTLR which alternative should be chosen. ANTLR4 seems to simply accept grammars with similar ambiguities, but during parsing it reports these ambiguities. It produces a parse tree, despite these ambiguities (by chosing the first alternative, according to the documentation). But what can I do, if I want it to chose some other alternative? In other words, how can I explicitly resolve ambiguities?

Get active Antlr rule

Deadly 提交于 2019-12-11 02:48:47
问题 Is it possible to get the "active" ANTLR rule from which a action method was called? Something like this log-function in Antlr-Pseudo-Code which should show the start and end position of some rules without hand over the $start- and $end-tokens with every log()-call: @members{ private void log() { System.out.println("Start: " + $activeRule.start.pos + "End: " + $activeRule.stop.pos); } } expr: multExpr (('+'|'-') multExpr)* {log(); } ; multExpr : atom('*' atom)* {log(); } ; atom: INT | ID {log

How to set a rule option count in ANTLR

徘徊边缘 提交于 2019-12-11 02:19:31
问题 I need help setting the count for an option match in a rule. This rule example will match (rule2) zero or more times. rule1: 'text' ( '(' rule2 ')' )* I know +,*,? for counts but what if I want the (rule2) to match 5 times? Are +,*,? the only count modifier supported for grammar so I need to enforce count in the parser listener? 回答1: what if I want the (rule2) to match 5 times? Inside the grammar, there are 2 options: 1. write it out 5 times: rule1: 'text' '(' rule2 ')' '(' rule2 ')' '('