[Antlr4] for CSharp how to output AST

97 views
Skip to first unread message

Michael Powell

unread,
Jan 29, 2019, 7:48:12 PM1/29/19
to antlr-discussion

Hello,

How do I output the AST for CSharp?

options { 
  output=AST; 
}

I am getting these kinds of errors:

Severity	Code	Description	Project	File	Line	Suppression State
Error	AC0050	'^' came as a complete surprise to me	Kingdom.OrTools.Sat.Params.Antlr	path\to\src\Kingdom.OrTools.Sat.Params.Antlr\Protov2.g4	240	

Over things such as this:

SYNTAX: 'syntax';

syntaxDecl
  : SYNTAX^ '=' (
    '\'proto2\''
    | '"proto2"'
    )/*{My.OrTools.Sat.SyntaxKind.Proto2}*/ ';'
;

Eventually, I would also like to synthesize actual target language level values such as:

enum My.OrTools.Sat.SyntaxKind {
    Proto2
}

Thoughts?

Thanks in advance...

Best regards,

Michael W Powell

Mike Lischke

unread,
Jan 30, 2019, 3:17:34 AM1/30/19
to 'Fred Curts' via antlr-discussion

How do I output the AST for CSharp?

ANTLR4 generated parsers no longer produce an AST, but a syntax tree. The difference is:

AST - a more or less structured list/tree of all input tokens
Parse Tree - a structured view on the parser rules that have been matched

The parse tree consists of parser rule contexts, which have start and end tokens (or in case of terminal nodes just a single symbol), which essentially carry the same information as the AST.


options { 
  output=AST; 
}

Not supported anymore.

SYNTAX: 'syntax';

syntaxDecl
  : SYNTAX^ '=' (
    '\'proto2\''
    | '"proto2"'
    )/*{My.OrTools.Sat.SyntaxKind.Proto2}*/ ';'
;

Tree rewrite rules are no longer supported. That is ANTLR3 syntax.


Michael Powell

unread,
Jan 30, 2019, 4:17:51 AM1/30/19
to antlr-discussion
Thanks for clarifying that. 

Reply all
Reply to author
Forward
0 new messages