antlr4

ANTRL4: Can't get Python ANTLR to generate a graphic of the parse tree

﹥>﹥吖頭↗ 提交于 2019-12-29 08:03:31
问题 I have a simple HelloWorld.g4 grammar (see it at the bottom). I am able to successfully generate the .py files using this: set CLASSPATH=.;antlr-complete.jar;%CLASSPATH% java org.antlr.v4.Tool -Dlanguage=Python2 HelloWorld.g4 Now I would like to use the TestRig with the -gui flag to generate a parse tree GUI. I have the ANTRL Python runtime installed ( antlr4-python2-runtime-4.5.tar.gz ). I can open a Python interpreter and type: import antlr4 and the interpreter recognizes the antlr4 module.

ANTRL4: Can't get Python ANTLR to generate a graphic of the parse tree

时间秒杀一切 提交于 2019-12-29 08:03:13
问题 I have a simple HelloWorld.g4 grammar (see it at the bottom). I am able to successfully generate the .py files using this: set CLASSPATH=.;antlr-complete.jar;%CLASSPATH% java org.antlr.v4.Tool -Dlanguage=Python2 HelloWorld.g4 Now I would like to use the TestRig with the -gui flag to generate a parse tree GUI. I have the ANTRL Python runtime installed ( antlr4-python2-runtime-4.5.tar.gz ). I can open a Python interpreter and type: import antlr4 and the interpreter recognizes the antlr4 module.

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

ANTLR get first production

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-25 09:29:00
问题 I'm using ANTLR4 and, in particular, the C grammar available in their repo (grammar). It seems that the grammar hasn't an initial rule, so I was wondering how it's possible to get it. In fact, once initialized the parser, I attach my listener, but I obtain syntax errors since I'm trying to parse two files with different code instructions: int a; int foo() { return 0; } In my example I call the parser with "parser.primaryExpression();" which is the first production of the "g4" file. Is it

ANTLR4 DefaultErrorStrategy fails to inject missing token

你说的曾经没有我的故事 提交于 2019-12-25 07:49:47
问题 I'm trying to run in TestRig the following grammar: grammar COBOLfragment; // hidden tokens WS : [ ]+ -> channel(HIDDEN); NL : '\n' -> channel(HIDDEN); // keywords PERIOD : '.'; DIVISION : 'DIVISION'; SECTION : 'SECTION'; DATA : 'DATA'; WORKING_STORAGE : 'WORKING-STORAGE'; FILE : 'FILE'; FD : 'FD'; EXTERNAL : 'EXTERNAL'; GLOBAL : 'GLOBAL'; BLOCK : 'BLOCK'; CONTAINS : 'CONTAINS'; CHARACTERS : 'CHARACTERS'; // data INTEGER : [0-9]+; ID : [A-Z][A-Z0-9]*; dataDivision : DATA DIVISION PERIOD

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

Antlr get Sub-Tokens

左心房为你撑大大i 提交于 2019-12-25 02:50:57
问题 Forgive me if my terminology is off. Lets say I have this bit of simplified grammar: // parser expr : COMPARATIVE; // lexer WS : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+; COMPARATOR : 'vs' | 'versus' ; ITEM : 'boy' | 'girl' ; COMPARATIVE :ITEM WS* COMPARATOR WS* ITEM; So this will of course match 'boy vs girl' or 'girl vs boy' , etc. But my question is that is when I create a Lexer, i.e. CharStream stream = new ANTLRInputStream("boy vs girl"); SearchLexer lex = new SearchLexer(stream);

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