antlr3

How do I get an Antlr Parser rule to read from both default AND hidden channel

眉间皱痕 提交于 2019-11-27 23:13:08
I use the normal whitespace separation into the hidden channel but I have one rule where I would like to include any whitespace for later processing but any example I have found requires some very strange manual coding. Is there no easy option to read from multiple channels like the option to put the whitespace there from the beginning. Ex. this is the WhiteSpace lexer rule WS : ( ' ' | '\t' | '\r' | '\n' ) {$channel=HIDDEN;} ; And this is my rule where I would like to include whitespace raw : '{'? (~('{'))*; Basically it's a catch all rule to capture any content that does not match other

Can I add Antlr tokens at runtime?

坚强是说给别人听的谎言 提交于 2019-11-27 22:21:34
I have a situation where my language contains some words that aren't known at build time but will be known at run time causing the need to constantly rebuild / redeploy the program to take into account new words. I was wandering if it was possible in Antlr generate some of the tokens from a config file? e.g In a simplified example if I have a rule rule : WORDS+; WORDS : 'abc'; And my language comes across 'bcd' at runntime, I would like to be able to modify a config file to define bcd as a word rather than having to rebuild then redeploy. You could add some sort of collection to your lexer

ANTLR 4 tree inject/rewrite operator

谁说胖子不能爱 提交于 2019-11-27 20:13:07
In ANTLR 3 you could just do the following: andExpression : (andnotExpression -> andnotExpression) (AND? a=andnotExpression -> ^(AndNode $andExpression $a))* ; Any idea how to do it in the new version? As mentioned by Sam (280Z28), ANTLR 4 does not have rewrite operators. When generating the parser, ANTLR 4 creates some listener classes that you can use to listen for "enter" and "exit" events of all parser rules. Also, ANTLR 4 supports "direct left recursive rules", so your expression rules can be defined in a single rule as demonstrated below: grammar Expr; parse : expression EOF ; expression

Get original text of an Antlr rule

耗尽温柔 提交于 2019-11-27 18:34:01
问题 I am an ANTLR beginner and want to calculate a SHA1-Hash of symbols. My simplified example grammar: grammar Example; method @after{calculateSha1($text); }: 'call' ID; ID: 'A'..'Z'+; WS: (' '|'\n'|'\r')+ {skip(); } COMMENT: '/*' (options {greedy=false;}: .)* '*/' {$channel=HIDDEN} As the lexer removes all whitespaces the different strings callABC , call /* DEF */ ABC unfortunately get the same SHA1-Hash value. Is it possible to get the "original" text of a rule between the start- and end-token

Negating inside lexer- and parser rules

て烟熏妆下的殇ゞ 提交于 2019-11-27 08:39:42
How can the negation meta-character, ~ , be used in ANTLR's lexer- and parser rules? Bart Kiers Negating can occur inside lexer and parser rules . Inside lexer rules you can negate characters, and inside parser rules you can negate tokens (lexer rules). But both lexer- and parser rules can only negate either single characters, or single tokens, respectively. A couple of examples: lexer rules To match one or more characters except lowercase ascii letters, you can do: NO_LOWERCASE : ~('a'..'z')+ ; (the negation-meta-char, ~ , has a higher precedence than the + , so the rule above equals (~('a'..

syntactic predicates - Upgrading from Antlr 3 to Antlr 4

℡╲_俬逩灬. 提交于 2019-11-27 08:09:21
问题 I have syntactic predicated that I have to convert into the Antlr 4. The grammar is not written my me so I have no idea how to convert them in a meaningful way. These are the main variations of the grammar that I have to convert. 1. simpleSelector : elementName ((esPred)=>elementSubsequent)* | ((esPred)=>elementSubsequent)+ ; esPred : HASH | DOT | LBRACKET | COLON ; elementSubsequent : HASH | cssClass | attrib | pseudo ; 2. fragment EMS :; // 'em' fragment EXS :; // 'ex' fragment LENGTH :; //

ANTLR 4 tree inject/rewrite operator

空扰寡人 提交于 2019-11-27 04:26:17
问题 In ANTLR 3 you could just do the following: andExpression : (andnotExpression -> andnotExpression) (AND? a=andnotExpression -> ^(AndNode $andExpression $a))* ; Any idea how to do it in the new version? 回答1: As mentioned by Sam (280Z28), ANTLR 4 does not have rewrite operators. When generating the parser, ANTLR 4 creates some listener classes that you can use to listen for "enter" and "exit" events of all parser rules. Also, ANTLR 4 supports "direct left recursive rules", so your expression

Testing ANTLR Grammar

点点圈 提交于 2019-11-27 03:31:15
问题 So I've been making a grammar in Eclipse with ANTLR v3.4 and I've made one that works and I want to make sure when I edit it everything still works. I can go into the interpretter everytime but that seems like a huge waste of time. Questions: I've read about gunit but the link it gives to download gUnit: ( http://antlr.org/hudson/job/gUnit/org.antlr$gunit/lastSuccessfulBuild/ ) doesn't work. How can I get gUnit. What is the best way to test grammars? Is it actually gUnit or should I just do

How do I get an Antlr Parser rule to read from both default AND hidden channel

只谈情不闲聊 提交于 2019-11-26 23:17:22
问题 I use the normal whitespace separation into the hidden channel but I have one rule where I would like to include any whitespace for later processing but any example I have found requires some very strange manual coding. Is there no easy option to read from multiple channels like the option to put the whitespace there from the beginning. Ex. this is the WhiteSpace lexer rule WS : ( ' ' | '\t' | '\r' | '\n' ) {$channel=HIDDEN;} ; And this is my rule where I would like to include whitespace raw

antlr3 - Generating a Parse Tree

我与影子孤独终老i 提交于 2019-11-26 21:25:58
问题 I'm having trouble figuring out the antlr3 API so I can generate and use a parse tree in some javascript code. When I open the grammar file using antlrWorks (their IDE), the interpreter is able to show me the parse tree, and it's even correct. I'm having a lot of difficulties tracking down resources on how to get this parse tree in my code using the antlr3 runtime. I've been messing around with the various functions in the runtime and Parser files but to no avail: var input = "(PR=5000)",