antlr4

Parse and return a list of doubles using ANTLR4

纵然是瞬间 提交于 2019-12-04 14:59:52
How can I parse a file containing a decimal numbers into a List<double> in C# using ANTLR4? A complete, working example would illustrate how all the pieces go together. The input file looks like this: 12.34 45.67 89.10 TomServo This is an updated version of an older answer to a different question, showing one way to do this task using C# and ANTLR4. The Grammar grammar Values; parse : (number ( LINEBREAK | EOF ) )* ; number : NUMBER ; NUMBER : DIGIT '.' DIGIT ; DIGIT : [0-9]+ ; WS : [ \t] -> channel(HIDDEN) ; LINEBREAK : '\r'? '\n' | '\r' ; The Listener Now the class that implements the

ANTLR 4 and AST visitors

五迷三道 提交于 2019-12-04 10:34:21
问题 I'm trying to use ASTs with ANTLR4, with this files: Builder.java import org.antlr.v4.runtime.ANTLRInputStream; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.TokenStream; public class Builder { public static void main(String[] args) { CharStream input = new ANTLRInputStream("ON M1==2 && M3 == 5 && (M2 > 1 || M5 <= 5.0) " + "DO P5:42 P4:10"); ExprLexer lexer = new ExprLexer(input); TokenStream tokens = new CommonTokenStream

Python AST from ANTLR Parse Tree?

末鹿安然 提交于 2019-12-04 09:53:35
I found an ANTLRv4 Python3 grammer , but it generates a parse-tree, which generally has many useless nodes. I'm looking for a known package to get a Python AST from that parse tree. Does something like this exist? EDIT: Clarification regarding use of the Python ast package: my project is in Java and I need to parse Python files. EDIT 2: By 'AST' I mean http://docs.python.org/2/library/ast.html#abstract-grammar , while by 'parse tree' I mean http://docs.python.org/2/reference/grammar.html . The following could be a start: public class AST { private final Object payload; private final List<AST>

Antlr Extraneous Input

拟墨画扇 提交于 2019-12-04 07:34:31
I have a grammar file BoardFile.g4 that has (relevant parts only): grammar Board; //Tokens GADGET : 'squareBumper' | 'circleBumper' | 'triangleBumper' | 'leftFlipper' | 'rightFlipper' | 'absorber' | 'portal' ; NAME : [A-Za-z_][A-Za-z_0-9]* ; INT : [0-9]+ ; FLOAT : '-'?[0-9]+('.'[0-9]+)? ; COMMENT : '#' ~( '\r' | '\n' )*; WHITESPACE : [ \t\r\n]+ -> skip ; KEY : [a-z] | [0-9] | 'shift' | 'ctrl' | 'alt' | 'meta' | 'space' | 'left' | 'right' | 'up' | 'down' | 'minus' | 'equals' | 'backspace' | 'openbracket' | 'closebracket' | 'backslash' | 'semicolon' | 'quote' | 'enter' | 'comma' | 'period' |

Antlr4: Mismatched input

浪子不回头ぞ 提交于 2019-12-04 06:31:15
问题 Here's a simple grammar test I thought would be easy to parse, but I get 'mismatched input' right off the bat and I can't figure out what Antlr is looking for. The input: # include "something" program TEST1 { BLAH BLAH } My grammar: grammar ProgHeader; program: header* prog EOF ; header: '#' ( include | define ) ; include: 'include' string ; define: 'define' string string? ; string: '"' QTEXT '"' ; prog: 'program' QTEXT '{' BLOCK '}' ; QTEXT: ~[\r\n\"]+ ; BLOCK: ~[}]+ ; // don't care, example

can an element contain attribute as parsed by parser generated by ANTLR? if so, how?

偶尔善良 提交于 2019-12-04 05:52:12
问题 I am following this tutorial and successfully replicated its behavior except that I am using Antlr 4.7 instead of the 4.5 that the tutorial was using. I am trying to build a DSL for expense tracker. Was wondering if each element can have attributes? E.g. this is what it looks like now This is the code for the todo.g4 as seen in https://github.com/simkimsia/learn-antlr-web-js/blob/master/todo.g4 grammar todo; elements : (element|emptyLine)* EOF ; element : '*' ( ' ' | '\t' )* CONTENT NL+ ;

ANTLR mismatched input

纵然是瞬间 提交于 2019-12-04 05:30:33
问题 I have a simple grammar like this: grammar mygrammar; the_rule : 'abc' 'xyz' ; WS : [ \t\r\n\u000C]+ -> channel(HIDDEN) ; When I parse the text "abc xyz" with the_rule(), I get the expected string tree representation: (the_rule abc xyz) However, then I add the following lexer rule, which I think means "anything except for a w": TEXT : ~[w]+ ; Now when I parse the text "abc xyz" with the_rule(), I get an error: line 1:0 mismatched input 'abc xyz' expecting 'abc' Why would the TEXT lexer rule

Why am I getting an error when assigning tokens to a channel?

痞子三分冷 提交于 2019-12-04 04:37:09
问题 I have the following code in my .g4 file. @lexer::members{ public static final int WHITESPACE = 1; public static final int COMMENTS = 2; } WS : (' '|'\t'|'\f')+ -> channel(WHITESPACE) ; COMMENT : '//' ~('\n'|'\r')* -> channel(COMMENTS) ; LINE_COMMENT : '/*' .*? '*/' NEWLINE? -> channel(WHITESPACE) ; I'm getting the following errors: warning(155): Shiro.g4:239:34: rule 'WS' contains a lexer command with an unrecognized constant value; lexer interpreters may produce incorrect output warning(155

Antlr4 - Is there a simple example of using the ParseTree Walker?

只谈情不闲聊 提交于 2019-12-04 03:34:22
Antlr4 has a new class ParseTreeWalker. But how do I use it? I am looking for a minimal working example. My grammar file is 'gram.g4' and I want to parse a file 'program.txt' Here is my code so far. (This assumes ANTLR has run my grammar file and created all of the gramBaseListener , gramLexer , etc etc): import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.tree.*; import static org.antlr.v4.runtime.CharStreams.fromFileName; public class launch{ public static void main(String[] args) { CharStream cs = fromFileName("gram.g4"); //load the file gramLexer lexer = new gramLexer(cs); /

Is there an existing ANTLR or IRONY grammar for R?

故事扮演 提交于 2019-12-04 02:24:14
Does anyone know if there is an existing existing ANTLR or IRONY grammar for R? Many thanks. I've built an R grammar for the early access "Honey Badger" ANTLR v4 release, if you'd like to give it a look. See the ANTLR v4 Examples page . Source for v4 is https://github.com/antlr/antlr4 . binary jar here: http://antlr.org/download/antlr-4.0ea-complete.jar For R you want http://www.antlr.org/wiki/download/attachments/28049418/R.g4 I'd guess a good place to look would be R to C Compiler (RCC) that was developed by John Garvin at Rice 来源: https://stackoverflow.com/questions/5705564/is-there-an