leothaud / antlr4-to-mlir

Automatic AST-Dialect and Lexer-Parser maker from an ANTLR4 grammar
Other
8 stars 3 forks source link

mlir #1

Closed ZhouDaXiong0413 closed 4 months ago

ZhouDaXiong0413 commented 4 months ago

Hello! I would like to ask why when I run the example you provided I can generate the dialect properly and can generate the complete mlir program from test.minijava, but when I try to generate the dialect from the .g4 file I wrote myself, the let arguments for each Op are empty and I can't generate the mlir file correctly, it's Why? Do I need to define my own dialect op?

leothaud commented 4 months ago

Hello, your problem might be linked to some limitations on the supported antlr4. Only some antlr4 grammar is supported. The value should be affected to a variable name in the antlr grammar to generate an operand. For example

mainClass: CLASS name=ID L_BRACKET PUBLIC STATIC VOID MAIN L_PARENT STRING_TOKEN L_SBRACKET R_SBRACKET argName=ID R_PARENT L_BRACKET body=statement R_BRACKET R_BRACKET;

will generate 3 elements in the arguments : $name, $argName and $body. I would gladly help you with your problem if you gave me more information on your grammar. But the antlr4-to-mlir is, for now, more of a proof of concept and must be filled with many bugs.

ZhouDaXiong0413 commented 4 months ago

Thank you very much for your reply, I show a small antlr4 file below:

grammar ProtocolLang;

protocol: (statement)* ; statement: assignment | declaration | expression ; assignment: ID '=' expression ';' ; declaration: 'int' ID ';' ; expression: ID | INT ; ID: [a-zA-Z]+ ; INT: [0-9]+ ; WS: [ \t\r\n]+ -> skip ;

leothaud commented 4 months ago

Your problem is in fact that you don't affect the values to anything, you could try to do something like that:

grammar ProtocolLang;

protocol: (statements+=statement)* ;
statement: assignment | declaration | expression ;
assignment: id=ID '=' expr=expression ';' ;
declaration: 'int' id=ID ';' ;
expression: idVal+=ID | intVal+=INT ;
ID: [a-zA-Z]+ ;
INT: [0-9]+ ;
WS: [ \t\r\n]+ -> skip ;
ZhouDaXiong0413 commented 4 months ago

Thank you very much for your reply! I have figured out the reason for my mistake