Integration ANTLR with LLVM

249 views
Skip to first unread message

Andy

unread,
Aug 26, 2017, 12:44:59 PM8/26/17
to antlr-discussion
I have example grammar expr.g4 :
grammar expr;
assign  
: ID '=' expr;
expr
: term ( ('+' | '-') term)*;
term
: unary ( ('*' | '/') unary)*;
unary
: '-' unary | factor;
factor
: '(' expr ')'
       
| ID
       
| INT
       
;
INT
: [0-9]+ ;
ID
: [a-z]+ ;
WS
: [ \t\r\n]+ -> skip ;

I compile it under Windows: antlr -Dlanguage=Cpp expr.g4 where antlr.bat = java -cp h:\ANTLR\antlr-4.7-complete.jar;%CLASSPATH% org.antlr.v4.Tool %*
How is best way to use exprParser.cpp and exprLexer.cpp in LLVM environment like LLVM example Kaleidoscope ?

Kevin Cummings

unread,
Aug 26, 2017, 3:27:12 PM8/26/17
to antlr-di...@googlegroups.com
Use your google-foo:
>
https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwj6zu_yz_XVAhXD24MKHTigDroQFggoMAA&url=https%3A%2F%2Ftheantlrguy.atlassian.net%2Fwiki%2Fdisplay%2FANTLR3%2FLLVM&usg=AFQjCNFeSZojZLS9O6OprjBMWmjmmJEeBg

Ter wrote this for ANTLR-3. It will probably take some massaging for
ANTLR-4.

On 08/26/17 12:44, Andy wrote:
> I have example grammar expr.g4 :
> |
> grammar expr;
> assign :ID '='expr;
> expr :term (('+'|'-')term)*;
> term :unary (('*'|'/')unary)*;
> unary :'-'unary |factor;
> factor :'('expr ')'
> |ID
> |INT
> ;
> INT:[0-9]+;
> ID :[a-z]+;
> WS :[\t\r\n]+->skip ;
> |
>
> I compile it under Windows: antlr -Dlanguage=Cpp expr.g4 where antlr.bat
> = java -cp h:\ANTLR\antlr-4.7-complete.jar;%CLASSPATH% org.antlr.v4.Tool %*
> How is best way to use exprParser.cpp and exprLexer.cpp in LLVM
> environment like LLVM example Kaleidoscope ?
>
> --
> You received this message because you are subscribed to the Google
> Groups "antlr-discussion" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to antlr-discussi...@googlegroups.com
> <mailto:antlr-discussi...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

--
Kevin J. Cummings
cumm...@kjchome.homeip.net
cumm...@kjc386.framingham.ma.us
kjc...@icloud.com
Registered Linux User #1232 (http://www.linuxcounter.net/)

Andy

unread,
Aug 28, 2017, 4:40:44 AM8/28/17
to antlr-discussion
I want another way, like Kaleidoscope chapter 3: here are methods:
Value *BinaryExprAST::codegen()
Value *CallExprAST::codegen()
Function *PrototypeAST::codegen()
All returns llvm's Value or other objects derived from Value.
These methods call its children:
Value *BinaryExprAST::codegen() {
    Value *L = LHS->codegen();
    Value *R = RHS->codegen();
With ANTLR is class  toyBaseVisitor : public toyVisitor; these methods returns antlrcpp::Any. How use it? I can embed Value in Any? Or build new tree with Value nodes?

Jim Idle

unread,
Aug 28, 2017, 6:25:11 AM8/28/17
to antlr-di...@googlegroups.com
You generate a C++ parse listener then either build an AST from whence to generate choose via these calls, or make these calls directly in the listener code. It's fairly easy to do that.


From: antlr-di...@googlegroups.com <antlr-di...@googlegroups.com> on behalf of Andy <borucki...@gmail.com>
Sent: Monday, August 28, 2017 9:40:44 AM
To: antlr-discussion
Subject: [antlr-discussion] Re: Integration ANTLR with LLVM
 
--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.

Andy

unread,
Aug 28, 2017, 12:18:40 PM8/28/17
to antlr-discussion
I use antlrcpp::Any as contener. For example for int type:
I am writing:
virtual antlrcpp::Any visitNumberexpr(toyParser::NumberexprContext *ctx) override {      
     
int val = 2;
      antlrcpp
::Any any(val);
     
return any;
 
}

and I am restoring:
  virtual antlrcpp::Any visitPrimary(toyParser::PrimaryContext *ctx) override {    
      antlrcpp
::Any ret = visitChildren(ctx);
     
int val = 0;
     
if (ret.isNotNull())
        val
= (int)ret;
     
return ret;
 
}

I think it would be easy using llvm::Value

Mike Lischke

unread,
Aug 28, 2017, 12:23:13 PM8/28/17
to antlr-di...@googlegroups.com
I use antlrcpp::Any as contener. For example for int type:
I am writing:
virtual antlrcpp::Any visitNumberexpr(toyParser::NumberexprContext *ctx) override {      
     
int val = 2;
      antlrcpp
::Any any(val);
     
return any;
 
}
I think it would be easy using llvm::Value


That would not work with Visual Studio or gcc.
Message has been deleted

Andy

unread,
Aug 28, 2017, 1:04:07 PM8/28/17
to antlr-di...@googlegroups.com
works on ints but not on objects.
Is other way returns any value from visitNumberexpr to visitPrimary?

Mike Lischke

unread,
Aug 30, 2017, 3:15:21 AM8/30/17
to antlr-di...@googlegroups.com
Hi Andy,

> I check it on VS2015 and GCC.
> Is more elegant way returns any value from visitNumberexpr to visitPrimary?

I'm not sure if I understand your question correctly. Are you asking if there is a different way to return values in the visitor methods? If that's the case then I have to say: no. The way it is now is the most flexible I can imagine. Keep in mind that we cannot template functions virtual (which is what the Java runtime uses). The next best way is a variant-like datatype which can store anything. In my code I use a struct for keeping all the details for the evaluation.

However, that ANTLR4 visitor class is very simple. If it doesn't fit your environment, why don't you just write an own class to walk the parse tree? Use the existing visitor as template and create your variant with llvm::Value or whatever you prefer.

Mike
--
www.soft-gems.net

Jim Idle

unread,
Sep 6, 2017, 4:03:28 AM9/6/17
to antlr-di...@googlegroups.com
I generally use the visitor to create a model that is basically a stack based execution model (as if you were actually generating byte code that was going to use a stack for operators etc). Then I use this stack model to generate the code. You will find that a lot easier I think as you will just "execute the stack" and use it to call the relevant LLVM methods:

stackavalue
stackavalue
func2 - pop and gen code pop and gen code, declare func2 if needed, gen func2 code, etc



Jim

--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussion+unsubscribe@googlegroups.com.

Andy

unread,
Sep 12, 2017, 5:40:43 AM9/12/17
to antlr-di...@googlegroups.com
In book "The definitive ANTLR 4 Reference" in chapter "Sharing Information Among Event Methods" are described three metohds:
- function results
- stack
- map nodes - described as best method (page 125)
If I reinstall Windows, I will try this method, thanks!
Reply all
Reply to author
Forward
0 new messages