antlr

Generate source code from AST with Antlr4 and StringTemplates

梦想与她 提交于 2020-01-21 11:37:39
问题 If I have an AST and modify it, can I use StringTemplates to generate the source code for the modified AST? I have successfully implemented my grammar for Antlr4. It generates the AST of a source code and I use the Visitor Class to perform the desired actions. I then modify something in the AST and I would like to generate the source code for that modified AST. (I believe it is called pretty-printing?). Does Antlr's built in StringTemplates have all the functionality to do this? Where should

ANTLR parsing and code generation with “-” operator and “-” number sign

岁酱吖の 提交于 2020-01-16 18:53:45
问题 This is my grammar: grammar FOOL; @header { import java.util.ArrayList; } @lexer::members { public ArrayList<String> lexicalErrors = new ArrayList<>(); } /*------------------------------------------------------------------ * PARSER RULES *------------------------------------------------------------------*/ prog : exp SEMIC #singleExp | let exp SEMIC #letInExp | (classdec)+ SEMIC (let)? exp SEMIC #classExp ; classdec : CLASS ID ( EXTENDS ID )? (LPAR (vardec ( COMMA vardec)*)? RPAR)? (CLPAR (

Antlr lexer semantic predicate on an alternative

一个人想着一个人 提交于 2020-01-16 12:19:07
问题 Given the grammar: grammar Test; words: (WORD|SPACE|DOT)+; WORD : ( LD |DOT {_input.LA(1)!='.'}? ) + ; DOT: '.'; SPACE: ' '; fragment LD: ~[.\n\r ]; with Antlr4 generated Lexer, for an input: test. test.test test..test The token sequence is like: [@0,0:4='test.',<1>,1:0] [@1,5:5=' ',<3>,1:5] [@2,6:14='test.test',<1>,1:6] [@3,15:15=' ',<3>,1:15] [@4,16:19='test',<1>,1:16] [@5,20:20='.',<2>,1:20] [@6,21:25='.test',<1>,1:21] [@7,26:25='<EOF>',<-1>,1:26] What puzzles why the last piece of text

LL(1) table-driven compilers with ANTLR or ANTLR3

梦想的初衷 提交于 2020-01-15 10:59:14
问题 Is it possible to create a LL(1) table-driven (non-recursive) compiler with ANTLR or ANTLR3 ? 回答1: No. However since ANTLR is open source you could modify a fork of ANTLR to do it. ANTLR builds lexers and parsers as recursive descent source code. This is why ANTLR is easy to use and popular because people can look at the source code and understand how the lexer and parser work versus looking at table entries. Because it is source code, one can also use tools to debug the source code. If ANTLR

C example of using AntLR

匆匆过客 提交于 2020-01-15 05:45:09
问题 I am wondering where I can find C tutorial/example of using AntLR. All I found is using Java language. I am focusing to find a main function which use the parser and lexer generated by AntLR. 回答1: Take a look at this document And here is an example: // Example of a grammar for parsing C sources, // Adapted from Java equivalent example, by Terence Parr // Author: Jim Idle - April 2007 // Permission is granted to use this example code in any way you want, so long as // all the original authors

ANTLR How to use lexer rules having same starting?

走远了吗. 提交于 2020-01-14 09:53:29
问题 How to use lexer rules having same starting? I am trying to use two similar lexer rules (having the same starting): TIMECONSTANT: ('0'..'9')+ ':' ('0'..'9')+; INTEGER : ('0'..'9')+; COLON : ':'; Here is my sample grammar: grammar TestTime; text : (timeexpr | caseblock)*; timeexpr : TIME; caseblock : INT COLON ID; TIME : ('0'..'9')+ ':' ('0'..'9')+; INT : ('0'..'9')+; COLON : ':'; ID : ('a'..'z')+; WS : (' ' | '\t' | '\r' | '\n') {$channel=HIDDEN;}; When i try to parse text: 12:44 123 : abc

Visualize tree from *.dot file

好久不见. 提交于 2020-01-14 05:39:10
问题 I need to visualize a tree which I have gotten from an ANTLR parser and written to a .dot file by catching console output that the ANLR dot generator produces. MyDOTTreeGenerator generator = new MyDOTTreeGenerator(); PrintStream old = System.out; try { System.setOut(new PrintStream(new FileOutputStream(graphFile))); } catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println(generator.toDOT(root)); System.out.flush(); System.setOut(old); Question is: 1. Is there a way to

Parsing a templating language

自古美人都是妖i 提交于 2020-01-14 03:59:08
问题 I'm trying to parse a templating language and I'm having trouble correctly parsing the arbitrary html that can appear between tags. So far what I have is below, any suggestions? An example of a valid input would be {foo}{#bar}blah blah blah{zed}{/bar}{>foo2}{#bar2}This Should Be Parsed as a Buffer.{/bar2} And the grammar is: grammar g; options { language=Java; output=AST; ASTLabelType=CommonTree; } /* LEXER RULES */ tokens { } LD : '{'; RD : '}'; LOOP : '#'; END_LOOP: '/'; PARTIAL : '>';

Is ANTLR an appropriate tool to serialize/deserialize a binary data format?

泪湿孤枕 提交于 2020-01-13 08:03:09
问题 I need to read and write octet streams to send over various networks to communicate with smart electric meters. There is an ANSI standard, ANSI C12.19, that describes the binary data format. While the data format is not overly complex the standard is very large (500+ pages) in that it describes many distinct types. The standard is fully described by an EBNF grammar. I am considering utilizing ANTLR to read the EBNF grammar or a modified version of it and create C# classes that can read and

Access Channels in ANTLR 4 and Parse them separately

瘦欲@ 提交于 2020-01-11 11:58:17
问题 I have included my comments in to a separate channel in ANTLR 4. in my case it is channel 2. This is my lexer grammar. COMMENT: '/*' .*? '*/' -> channel(2) ; I want to access this channel 2 and do a parse on this channel to accumulate comments. So I included that as in parsing grammar as below comment :COMMENT ; In the program string s = " paring string" AntlrInputStream input = new AntlrInputStream(s); CSSLexer lexer = new CSSLexer(input); CommonTokenStream tokens = new CommonTokenStream