[antlr-interest] AST construction - tree (samples)

Prados Valiente Enrique Enrique.PradosValiente at reale.es
Tue Aug 24 01:27:44 PDT 2010


Hi all,

I attached updated gramar from Andrew Haritonkin.

In this grammar there are no instructions for AST construction. That means that ANTLR will create just a plain list of tokens. I need a nice tree.

Any sample grammar using instructions for AST construction, for generate tree ??

Please, any help will be very appreciated.

Thanks in advanced.

 
>
>
>Read wiki pages and documentation on antlr.org to know how to use generated parser in your application.

In this grammar there are no instructions for AST construction. That means that ANTLR will create just a plain list of tokens. If you want a nice tree you have to do that yourself.

Also PL/SQL is case insensitive language. How to make case insensitive parser with ANTLR v3 you can find here:
http://www.antlr.org/wiki/pages/viewpage.action?pageId=1782
Class ANTLRNoCaseFileStream is what you need, or similar one for string stream.

Here is an example how to parser array of statements:

            foreach(string statement in statements)
            {
                PLSQL3Lexer lexer = new PLSQL3Lexer(null);
                PLSQL3Parser parser = new PLSQL3Parser(null);

                try
                {
                    ICharStream input = new CaseInsensitiveStringStream(statement);
                    lexer.CharStream = input;
                    CommonTokenStream tokens = new CommonTokenStream(lexer);
                    parser.TokenStream = tokens;
                    parser.start_rule();
                }
                catch (Exception exc)
                {
                    IToken token = parser.TokenStream.LT(1);
                    System.Console.WriteLine("Exception:\n{0}",
String.Format("{0}:{1} at '{2}'({3}): {4}", token.Line, token.CharPositionInLine, token.Text, token.Type, exc.ToString()));
                    errors++;
                }

Hope it helps,
Andrew

On Fri, Aug 20, 2010 at 9:56 AM, Prados Valiente Enrique <Enrique.PradosValiente at reale.es> wrote:
> Hi all,
>
>
>
>
>
> I use AntlrWorks 1.4, and open PLSQL3.g file and do Generate -> 
> Generate Code
>
>
>
> And I get this error:
>
>
>
> [09:46:44] error(206): PLSQL3.g:791:4: Alternative 2: after matching 
> input such as ID DOT ID DOT ID DOT ID DOT decision cannot predict what 
> comes next due to recursion overflow to expr_add from sql_expression 
> and to expr_mul from expr_add
>
>
>
> Log file is hs_err_pid5036.log
>
>
>
>
>
>
>
> Now, I test with Antlr3.exe (.net) from here
>
>
>
> ANTLR Tool Binaries
>
> ANTLR v3.1.x binaries for .NET framework. No dependence on the Java runtime.
> antlr3.exe and StringTemplate and ANTLR runtime libraries it depends on.
> Completely independent version written by Sam Harwell (ported from Java).
>
>
>
> I execute this command
>
> E:\Trabajo\SqlParser\ANTLR\ANTLR-3.1-2009-06-28>Antlr3.exe
> E:\Trabajo\SqlParser\
>
> ANTLR\plsql3.g
>
>
>
> But I get this error:
>
>
>
> error(10):  internal error:  : System.Exception: Error parsing 
> E:\Trabajo\SqlPar
>
> ser\ANTLR\plsql3.g: 'backtrack' not expected ';'
>
>    en Antlr3.Tool.GrammarSpelunker.Match(String expecting) en 
> c:\dev\stringtempl
>
> ate_main\antlrcs\main\Antlr3\Tool\GrammarSpelunker.cs:línea 89
>
>    en Antlr3.Tool.GrammarSpelunker.Options() en 
> c:\dev\stringtemplate_main\antlr
>
> cs\main\Antlr3\Tool\GrammarSpelunker.cs:línea 155
>
>    en Antlr3.Tool.GrammarSpelunker.Parse() en 
> c:\dev\stringtemplate_main\antlrcs
>
> \main\Antlr3\Tool\GrammarSpelunker.cs:línea 112
>
>    en Antlr3.AntlrTool.SortGrammarFiles() en 
> c:\dev\stringtemplate_main\antlrcs\
>
> main\Antlr3\AntlrTool.cs:línea 595
>
>    en Antlr3.AntlrTool.Process() en
> c:\dev\stringtemplate_main\antlrcs\main\Antl
>
> r3\AntlrTool.cs:línea 431
>
> Match at offset 258 in file:line:column 
> c:\dev\stringtemplate_main\antlrcs\main\
>
> Antlr3\Tool\GrammarSpelunker.cs:89:17
>
>
>
> Options at offset 89 in file:line:column 
> c:\dev\stringtemplate_main\antlrcs\main
>
> \Antlr3\Tool\GrammarSpelunker.cs:155:17
>
>
>
> Parse at offset 332 in file:line:column 
> c:\dev\stringtemplate_main\antlrcs\main\
>
> Antlr3\Tool\GrammarSpelunker.cs:112:21
>
>
>
> SortGrammarFiles at offset 166 in file:line:column 
> c:\dev\stringtemplate_main\an
>
> tlrcs\main\Antlr3\AntlrTool.cs:595:17
>
>
>
> Process at offset 91 in file:line:column 
> c:\dev\stringtemplate_main\antlrcs\main
>
> \Antlr3\AntlrTool.cs:431:17
>
>
>
> error(8):  file E:\Trabajo\SqlParser\ANTLR\plsql3.g contains grammar 
> PLSQL3; nam
>
> es must be identical
>
> warning(200): E:\Trabajo\SqlParser\ANTLR\plsql3.g:776:3: Decision can 
> match inpu
>
> t such as "{'ASC', 'DESC'}" using multiple alternatives: 1, 2
>
> As a result, alternative(s) 2 were disabled for that input
>
> error(206): E:\Trabajo\SqlParser\ANTLR\plsql3.g:791:4: Alternative 2: 
> after matc
>
> hing input such as ID DOT ID DOT ID DOT ID DOT decision cannot predict 
> what come
>
> s next due to recursion overflow to expr_add from sql_expression and 
> to expr_mul
>
>  from expr_add
>
>
>
>
>
> any solution please ¿????
>
>
>
> Thanks in advanced
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PLSQL3.g
Type: application/octet-stream
Size: 41747 bytes
Desc: PLSQL3.g
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20100824/a4073065/attachment.obj 


More information about the antlr-interest mailing list