antlr

Java Tree parser output for ANTLR

。_饼干妹妹 提交于 2019-12-28 12:43:19
问题 I've found a sample template in the ANTLR website, its the Javatreeparser.g, which the site says could produce the AST that I need, but since I'm new to ANTLR, how do I make it show? What I've done so far is placing the grammar file together with my existing java grammar. But I have no idea on how to use and output the AST that I need from the file. How do I do it? 回答1: I've found a sample template in the ANTLR website, its the Javatreeparser.g, which the site says could produce the AST that

Extending simple ANTLR grammar to support input variables

て烟熏妆下的殇ゞ 提交于 2019-12-28 08:10:29
问题 I'm still on my quest for a really simple language and I know now that there are none. So I'm writing one myself using ANTLR3. I found a really great example in this answer: Exp.g: grammar Exp; eval returns [double value] : exp=additionExp {$value = $exp.value;} ; additionExp returns [double value] : m1=multiplyExp {$value = $m1.value;} ( '+' m2=multiplyExp {$value += $m2.value;} | '-' m2=multiplyExp {$value -= $m2.value;} )* ; multiplyExp returns [double value] : a1=atomExp {$value = $a1

Negating inside lexer- and parser rules

此生再无相见时 提交于 2019-12-28 02:55:26
问题 How can the negation meta-character, ~ , be used in ANTLR's lexer- and parser rules? 回答1: 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')+ ;

Defining antlr rule for identifiers with one character

荒凉一梦 提交于 2019-12-25 19:49:05
问题 i like to define a very simple rule, which should represent identifiers. This works quite well for identifiers which are longer than one characters, but for identifiers with exactly one character I get a MismatchedTokenException(64!=45). This is the rule: ID : ('a'..'z' | 'A'..'Z')+ ; What is wrong with that? Thanks! 回答1: You're absolutely right, the rule on its own is fine, but I figured out that a lot in ANTLR depends on the order of rules. I had another rule before, which did already match

Ordering lexer rules in a grammar using ANTLR4

三世轮回 提交于 2019-12-25 10:55:15
问题 I'm using ANTLR4 to generate a parser. I am new to parser grammars. I've read the very helpful ANTLR Mega Tutorial but I am still stuck on how to properly order (and/or write) my lexer and parser rules. I want the parser to be able to handle something like this: Hello << name >>, how are you? At runtime I will replace "<< name >>" with the user's name. So mostly I am parsing text words (and punctuation, symbols, etc), except with the occasional "<< something >>" tag, which I am calling a

ANTLR4 - Need an explanation on this String Literals

只愿长相守 提交于 2019-12-25 07:39:20
问题 On my assignment, I have this description for the String Lexer: "String literals consist zero or more characters enclosed by double quotes ("). Use escape sequences (listed below) to represent special characters within a string. It is a compile-time error for a new line or EOF character to appear inside a string literal. All the supported escape sequences are as follows: \b backspace \f formfeed \r carriage return \n newline \t horizontal tab \" double quote \ backslash The following are

Is it possible to make Antlr4 generate lexer from base grammar lexer instead of gener Lexer?

帅比萌擦擦* 提交于 2019-12-25 03:26:45
问题 I have lexer grammar called BasicTokens which is set of basic tokens for my language, having tokens like null , true , false etc. Now when I create parser grammar say BasicGrammar which imports refers BasicTokens and another grammar called InheritedGrammar which imports BasicGrammar . When Antlr4 generates the parser for InheritedGrammar it included all the rules already defined in BasicGrammar . Is there a way to make Antlr describe only the rules generated in InheritedGrammar and not in

is SFig language syntax efficient and clear (and better than Spring-Framework's XML DSL)?

主宰稳场 提交于 2019-12-25 01:19:09
问题 ADDENDUM EDIT: Have not accepted an answer to this as there has not been any feedback from experienced Spring Framework developers. I've been working on a replacement DSL to use for Spring-Framework applicationContext.xml files (where bean initialization and dependency relationships are described for loading up into the Spring bean factory). My motivation is that I just flat out don't like Spring's use of XML for this purpose nor do I really like any of the alternatives that have been devised

Antlr4 parser integrated code handling at the prediction time

风格不统一 提交于 2019-12-25 00:37:53
问题 I have some rules like this @parser::header { import com.almondtools.antlr4multichannel.MultiChannelTokenStream; } @parser::members { private static final int HIDDEN = Token.HIDDEN_CHANNEL; public void enableWs() { ((MultiChannelTokenStream) _input).enable(HIDDEN); } public void disableWs() { ((MultiChannelTokenStream) _input).disable(HIDDEN); } } expression : functionCallNoParen | chain ; functionCallNoParen : chain {enableWs();} Whitespace {disableWs();} expression ; chain : chainBase

Antlr check return statements in scope

烂漫一生 提交于 2019-12-24 21:40:14
问题 I was wondering how to check that all paths have a return in a function during syntax analysis. So say I have the following in the Lexer RETURN: 'return'; PRINT: 'print'; IF:'if'; ELSE: 'else'; THEN:'then'; PLUS: '+'; MINUS:'-'; EQUALS: '=='; DIGIT: '0'..'9'; OPEN:'{'; CLOSE:'}'; STRING: [a..zA..Z]+; SEMICOLON: ';'; and parser function: STRING OPEN statement CLOSE statement: RETURN expr | PRINT expr | IF expr THEN statement ELSE statement | statement SEMICOLON statement; expr: DIGIT| expr