antlr4

ANTLR: Help on Lexing Errors for a custom grammar example

不想你离开。 提交于 2019-12-05 05:41:34
问题 What approach would allow me to get the most on reporting lexing errors? For a simple example I would like to write a grammar for the following text (white space is ignored and string constants cannot have a \" in them for simplicity): myvariable = 2 myvariable = "hello world" Group myvariablegroup { myvariable = 3 anothervariable = 4 } Catching errors with a lexer How can you maximize the error reporting potential of a lexer? After reading this post: Where should I draw the line between

How to implement the visitor pattern for nested function

ε祈祈猫儿з 提交于 2019-12-05 03:38:26
I am a newbie to Antlr and I wanted the below implementation to be done using Antlr4. I am having the below-written functions. 1. FUNCTION.add(Integer a,Integer b) 2. FUNCTION.concat(String a,String b) 3. FUNCTION.mul(Integer a,Integer b) And I am storing the functions metadata like this. Map<String,String> map=new HashMap<>(); map.put("FUNCTION.add","Integer:Integer,Integer"); map.put("FUNCTION.concat","String:String,String"); map.put("FUNCTION.mul","Integer:Integer,Integer"); Where, Integer:Integer,Integer represents Integer is the return type and input params the function will accespts are

What is minimal sample Gradle project for ANTLR4 (with antlr plugin)?

天大地大妈咪最大 提交于 2019-12-05 02:05:39
I have created new Gradle project, added apply plugin: 'antlr' and dependencies { antlr "org.antlr:antlr4:4.5.3" to build.gradle . Created src/main/antlr/test.g4 file with the following content grammar test; r : 'hello' ID; ID : [a-z]+ ; WS : [ \t\r\n]+ -> skip ; But it doesn't work. No java source files generated (and no error occurred). What I missed? Project is here: https://github.com/dims12/AntlrGradlePluginTest2 UPDATE I found my sample is actually works, but it put code into \build\generated-src which I was not expecting :shame: I will add onto other answers here. Issue 1 : Generated

How can i see the live parse tree using Antlr4 Ide in Eclipse?

て烟熏妆下的殇ゞ 提交于 2019-12-05 00:45:38
I'm new using Antlr4 but I know that exist a plugin for Eclipse. I have a simple question...After I created the g4 file how can I visualize the live parse tree in order to see the tree of an input expression? Thanks Currently there is no provision for viewing live parse tree in ANTLR 4 IDE for Eclipse . Meanwhile, you can see the parse tree using -gui switch in the command line. It also provides a feature of saving the parse tree as PNG. After installing Antlr4Ide plugin in Eclipse: Window>Show View>Other, Antlr4>Parse tree Activate g4 file Click parsing rule in g4 file, Parse tree now shows

How Lexer lookahead works with greedy and non-greedy matching in ANTLR3 and ANTLR4?

北城以北 提交于 2019-12-04 21:35:01
If someone would clear my mind from the confusion behind look-ahead relation to tokenizing involving greery/non-greedy matching i'd be more than glad. Be ware this is a slightly long post because it's following my thought process behind. I'm trying to write antlr3 grammar that allows me to match input such as: "identifierkeyword" I came up with a grammar like so in Antlr 3.4: KEYWORD: 'keyword' ; IDENTIFIER : (options {greedy=false;}: (LOWCHAR|HIGHCHAR))+ ; /** lowercase letters */ fragment LOWCHAR : 'a'..'z'; /** uppercase letters */ fragment HIGHCHAR : 'A'..'Z'; parse: IDENTIFIER KEYWORD EOF

Ignoring whitespace (in certain parts) in Antlr4

不打扰是莪最后的温柔 提交于 2019-12-04 20:02:10
I am not so familiar with antlr. I am using version 4 and I have a grammar where whitespace is not important in some parts (but it might be in others, or rather its luck). So say we have the following grammar grammar Foo; program : A* ; A : ID '@' ID '(' IDList ')' ';' ; ID : [a-zA-Z]+ ; IDList : ID (',' IDList)* ; WS : [ \t\r\n]+ -> skip ; and a test input foo@bar(X,Y); foo@baz ( z,Z) ; The first line is parsed correctly whereas the second one is not. I don't want to polute my rules with the places where whitespace is not relevant, since my actual grammar is more complicated than the toy

Get Preprocessor lines with antlr4 with parsing C Code

▼魔方 西西 提交于 2019-12-04 17:27:21
I am using Antlr4 to parse C code and I am using the following grammar to parse: Link to C.g4 The above grammar by default does not provide any parsing rules to get preprocessor statements. I changed the grammar slightly to get the preprocessor lines by adding the following lines externalDeclaration : functionDefinition | declaration | ';' // stray ; | preprocessorDeclaration ; preprocessorDeclaration : PreprocessorBlock ; PreprocessorBlock : '#' ~[\r\n]* -> channel(HIDDEN) ; And in Java I am using the following listener to get the preprocessor lines @Override public void

Intellij will not recognize antlr generated source code

℡╲_俬逩灬. 提交于 2019-12-04 17:17:34
问题 I am having trouble getting Intellij to recognize the generated source code from antlr4. Any reference to the generated code appears as errors, code completion doesn't work, etc. I am using maven and the antlr4-maven-plugin to generate the code. My code, referencing the generated code compiles and builds fine under maven. The generated code is under /target/generated-sources/antlr4, which is what Intellij expects. I have tried the usual fixes such as reimport maven projects, update folders,

ANTLR4: Whitespace handling

谁都会走 提交于 2019-12-04 15:50:41
问题 I have seen many ANTLR grammars that use whitespace handling like this: WS: [ \n\t\r]+ -> skip; // or WS: [ \n\t\r]+ -> channel(HIDDEN); So the whitespaces are thrown away respectively send to the hidden channel. With a grammar like this: grammar Not; start: expression; expression: NOT expression | (TRUE | FALSE); NOT: 'not'; TRUE: 'true'; FALSE: 'false'; WS: [ \n\t\r]+ -> skip; valid inputs are ' not true ' or ' not false ' but also ' nottrue ' which is not a desired result. Changing the

ANTLR 4 : How to know the existence of subpart in rule

江枫思渺然 提交于 2019-12-04 15:45:18
I have this code : varDeclaration : type ID ('=' expression)? ';' ; So, not always ('=' expression) exist. But, sometimes, I want to process this part, but don't know it exist or not in this context. I'm using ANTLR 4 (and often using Listener), how can I know this. Thanks :) In your listener ( exitVarDeclaration ) or visitor ( visitVarDeclaration ) check whether ctx.expression() == null . If null, then ('=' expression) didn't exist. If non-null, then it did exist. 来源: https://stackoverflow.com/questions/16392152/antlr-4-how-to-know-the-existence-of-subpart-in-rule