antlr4

ANTLR4 - DotGenerator example [duplicate]

回眸只為那壹抹淺笑 提交于 2020-01-06 02:14:30
问题 This question already has answers here : ANTLR4 parse tree to DOT using DOTGenerator (2 answers) Closed last year . Where I can find example how to use org.antlr.v4.tool.DotGenerator in ANTLR4? As I understand, it replaces DOTTreeGenerator in ANTLR4. 回答1: I am also interested in an answer to your question and didn't find a fully convincing one yet. Assuming that you are interested in Displaying the ParseTree here is an alternative way to at least get a visual representation: /** * show the

antlr4 can't extract literal into token

余生长醉 提交于 2020-01-05 09:05:10
问题 I have the following grammar and am trying to start out slowly, working up to move complex arguments. grammar Command; commands : command+ EOF; command : NAME args NL; args : arg | ; arg : DASH LOWER | LOWER; //arg : DASH 'a' | 'x'; NAME : [_a-zA-Z0-9]+; NL : '\n'; WS : [ \t\r]+ -> skip ; // spaces, tabs, newlines DASH : '-'; LOWER: [a-z];//'a' .. 'z'; I was hoping (for now) to parse files like this: cmd1 cmd3 -a If I run that input through grun I get an error: $ java org.antlr.v4.gui.TestRig

antlr4 multiline string parsing

大兔子大兔子 提交于 2020-01-05 09:02:38
问题 If I have a ONELINE_STRING fragment rule in an antlr4 lexer that identifies a simple quoted string on one line, how can I create a more general STRING rule in the lexer that will concatenate adjacent ONELINE_STRING's (ie, separated only by whitespace and/or comments) as long as they each start on a different line? ie, "foo" "bar" would be parsed as two STRING tokens, "foo" followed by "bar" while: "foo" "bar" would be seen as one STRING token: "foobar" For clarification: The idea is that

How do I parse PDF strings with nested string delimiters in antlr?

社会主义新天地 提交于 2020-01-05 04:45:28
问题 I'm working on parsing PDF content streams. Strings are delimited by parentheses but can contain nested unescaped parentheses. From the PDF Reference: A literal string shall be written as an arbitrary number of characters enclosed in parentheses. Any characters may appear in a string except unbalanced parentheses (LEFT PARENHESIS (28h) and RIGHT PARENTHESIS (29h)) and the backslash (REVERSE SOLIDUS (5Ch)), which shall be treated specially as described in this sub-clause. Balanced pairs of

What is the ANTLR4 equivalent of a ! in a lexer rule?

荒凉一梦 提交于 2020-01-05 02:51:13
问题 I'm working on converting an old ANTLR 2 grammar to ANTLR 4, and I'm having trouble with the string rule. STRING : '\''! ( ~('\'' | '\\' | '\r' | '\n') )* '\''! ; This creates a STRING token whose text contains the contents of the string, but does not contain the starting and ending quotes, because of the ! symbol after the quote literals. ANTLR 4 chokes on the ! symbol, ( '!' came as a complete surprise to me (AC0050) ) but if I leave it off, I end up with tokens that contain the quotes,

ANTLR4 does not find grammar on import

柔情痞子 提交于 2020-01-05 01:58:06
问题 I am trying to split my ANTLR4 grammar in multiple files so i can test them more easily, i am using gradle as a build tool in a java project. Both grammar compile correctly by separate but when i add the import to my main grammar i get the next compilation error error(110): kanekotic/specflow/rider/SpecflowFeature.g4:3:7: can't find or load grammar SpecflowScenario the inherited grammar looks like: grammar SpecflowScenario; @header { package kanekotic.specflow.rider; } scenario : 'Scenario: '

ANTLR: Get text representation of a sub lexer rule

风格不统一 提交于 2020-01-05 01:40:08
问题 consider the following lexer rules in ANTLR4: ID: [a-z]+; INT: [0-9]+; ARRAY: ID '[' INT ']'; Is it possible in a tree walking scenario where I have access to ctx.ARRAY() (where ctx is a subclass of ParserRuleContext that was generated out of a parser rule) to get the text representation of the lexer rules ID and INT ? I currently fetch the whole text representation with ctx.ARRAY().getText() and parse the contents of ID and INT using regexes and was just wondering if there is a 'cleaner' out

ANTLR: Get text representation of a sub lexer rule

空扰寡人 提交于 2020-01-05 01:40:07
问题 consider the following lexer rules in ANTLR4: ID: [a-z]+; INT: [0-9]+; ARRAY: ID '[' INT ']'; Is it possible in a tree walking scenario where I have access to ctx.ARRAY() (where ctx is a subclass of ParserRuleContext that was generated out of a parser rule) to get the text representation of the lexer rules ID and INT ? I currently fetch the whole text representation with ctx.ARRAY().getText() and parse the contents of ID and INT using regexes and was just wondering if there is a 'cleaner' out

General stategy for designing Flexible Language application using ANTLR4

非 Y 不嫁゛ 提交于 2020-01-04 09:04:01
问题 Requirement: I am trying to develop a language application using antlr4. The language in question is not important. The important thing is that the grammar is very vast ( easily >2000 rules!!! ). I want to do a number of operations Extract bunch of informations. These can be call graphs, variable names. constant expressions etc. Any number of transformations: if a loop can be expanded, we go ahead and expand it If we can eliminate dead code we might choose to do that we might choose to rename

Checked exceptions in visitors

倖福魔咒の 提交于 2020-01-03 08:43:30
问题 I am learning ANTLR4 and I have no previous experience with parser generators. When I define my own visitor implementation, I have to override methods of the BaseVisitor (I am looking for instance at the EvalVisitor class at page 40 of the book). In case my method implementation possibly throws an exception, what should I do? I cannot use a checked exception since the original method has an empty throws clause. Am I expected to use unchecked exceptions? (this seems a bad Java design). For