Error 20 ANTLR

523 views
Skip to first unread message

aabb

unread,
May 17, 2015, 5:48:11 AM5/17/15
to antlr-di...@googlegroups.com
hi I got this Error when I used ANTLR and I tried for hours to fix it. I hope some one could help.

context [/report INTERNAL_ERROR] 1:17 attribute arg isn't defined

error(20):  internal error:


my Grammar
grammar Hask;

////////////////Syntax Rules
prog : stat+;

stat : decFunc NL 
| impFunc NL
;

decFunc : expr ;

expr : ID '::' dec;

dec : ('[' 'Int' ']' | '[' '[' 'Int' ']' ']')  ARROW 
| dec
;


impFunc         : ID+ '=' hr;


hr          : 'map' '(' hr INT ')' hr 
            | 'zipWith' '(' ( '*'  |  '/'  |  '+'  |  '-'  )  ')' 
            | 'foldr1' '(' ( '*'  |  '/'  |  '+'  |  '-'  ) ')'
            | hr  op=('*'| '/') hr
            | hr  op=('+'| '-') hr
            | '(' hr ')'
            | ID 
            ;

/////////////Lex Rules

NL : '\r'? '\n' ;  
INT : [0-9]+ ;   
WS : [ \t]+ -> skip ;
ID   : [a-zA-Z]+ ; 
MUL : '*' ; 
DIV :   '/' ;
ADD :   '+' ;
SUB :   '-' ;
ARROW : '->' ;

Thank you

Jim Idle

unread,
May 17, 2015, 6:28:06 AM5/17/15
to antlr-di...@googlegroups.com
There are lots of things wrong with this grammar. First you should take all the 'literals' out of your parser and make LEXER rules. Next you cannot have two lever rules matching the same character sequence so you need to merge them. The parser does not give any context to the LEXER. Imagine that the lexer runs first and makes all the tokens, THEN the parser runs. 

I think you should first study the book and the example grammars. It will save you time in the long run. 

Jim





On Sun, May 17, 2015 at 2:48 AM -0700, "aabb" <ahmed...@gmail.com> wrote:

hi I got this Error when I used ANTLR and I tried for hours to fix it. I hope some one could help.

context [/report INTERNAL_ERROR] 1:17 attribute arg isn't defined

error(20):  internal error:


my Grammar
grammar Hask;

////////////////Syntax Rules
prog : stat+;

stat : decFunc NL 
| impFunc NL
;

decFunc : expr ;

expr : ID '::' dec;

dec : ('[' 'Int' ']' | '[' '[' 'Int' ']' ']')  ARROW 
| dec
;


impFunc         : ID+ '=' hr;


hr          : 'map' '(' hr INT ')' hr 
            | 'zipWith' '(' SYM ')' 
            | 'foldr1' '(' SYM ')'
            | hr  op=('*'| '/') hr
            | hr  op=('+'| '-') hr
            | '(' hr ')'
            | ID 
            ;

/////////////Lex Rules

NL : '\r'? '\n' ;  
INT : [0-9]+ ;   
SYM : '*' | '/' | '+' | '-';
WS : [ \t]+ -> skip ;
ID   : [a-zA-Z]+ ; 
MUL : '*' ; 
DIV :   '/' ;
ADD :   '+' ;
SUB :   '-' ;
ARROW : '->' ;

Thank you

--
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.
For more options, visit https://groups.google.com/d/optout.

aabb

unread,
May 17, 2015, 6:40:32 AM5/17/15
to antlr-di...@googlegroups.com
Thank you Jim. I just started reading it today :)

John B. Brodie

unread,
May 17, 2015, 12:54:12 PM5/17/15
to antlr-di...@googlegroups.com
Greetings!

I get a different error when I try to process your grammar with ANTLR V4 version 4.0b3. It complains about an improper tree structure and gives a long java traceback.

I think the problem is with your def parser rule which has infinite recursion and may be giving ANTLR's analyzer an issue.

Your def parser rule may be trivially re-written as the following cosmetically identical pair of rules:

def : phrase | def ;
phrase : ('[' 'Int' ']' | '[' '[' 'Int' ']' ']')  ARROW ;

and so you have infinitely many empty defs ending with a single phrase. infinite recursion.

Hope this helps....
   -jbb


aabb

unread,
May 17, 2015, 1:17:09 PM5/17/15
to antlr-di...@googlegroups.com
Thank you jbb. 
I fixed the grammar. However, I wonder. How to check the previous line of code. For example 
0 myFunction :: Int -> Int 
1 myFunction x = x+3

I have a function declaration that has a name (myFunction), one Int input, and one Int output. When I parse the next line #1 how to check the name of the function and that it has only one variable which is( x), that is similar to the declaration? 
this is my improved grammar:


prog : stat+;


stat
: decFunc  | impFunc ;

decFunc
: ID '::' singleIn+ NL;

singleIn        
: 'Int'

               
|'[' 'Int' ']'

               
| '[' '[' 'Int' ']' ']'

               
| ('[' 'Int' ']' ARROW)

               
| ('[' '[' 'Int' ']' ']' ARROW)  

               
;
impFunc
: ID+ '=' hr NL;

hr
: 'map' '(' ID* ')'  ID*
               
| 'zipWith' '(' ('*' |'/' |'+' |'-') ')' ID+
               
| 'foldr'   '(' ('*' |'/' |'+' |'-') ')' ID+
               
| hr  op=('*'| '/') hr
               
| hr  op=('+'| '-') hr
               
| '(' hr ')'
               
| ID '(' ID* ')'
               
;



Thank you,

John B. Brodie

unread,
May 17, 2015, 2:09:52 PM5/17/15
to antlr-di...@googlegroups.com
Greetings!


On 05/17/2015 01:17 PM, aabb wrote:
Thank you jbb. 
I fixed the grammar. However, I wonder. How to check the previous line of code. For example 
0 myFunction :: Int -> Int 
1 myFunction x = x+3

I have a function declaration that has a name (myFunction), one Int input, and one Int output. When I parse the next line #1 how check the name of the function and that it has only one variable which is x.

that kind of analysis should not be performed in a parser. you will need to create a semantic analysis phase for your compiler which will take the parse tree produced by the parsing phase as input. that semantic analysis phase will walk the tree and check to ensure rules like the one you mentioned along with many others are enforced.


this is my improved grammar:


prog : stat+;


stat
: decFunc  | impFunc ;

decFunc
: ID '::' singleIn+ NL;

singleIn        
: 'Int'

               
|'[' 'Int' ']'

               
| '[' '[' 'Int' ']' ']'

               
| ('[' 'Int' ']' ARROW)

               
| ('[' '[' 'Int' ']' ']' ARROW) 

note that this rule will allow for a trailing ARROW with no result type specified. maybe better as follows:

decFunc : ID '::' formalType ( ARROW formalType )* NL ;

formalType : 'Int' | '[' formalType ']' ;

note that the formalType allows for lists of lists of lists of lists .... of Int which your original did not permit, but I suspect that is where you are headed.




               
;
impFunc
: ID+ '=' hr NL;

hr
: 'map' '(' ID* ')'  ID*
               
| 'zipWith' '(' ('*' |'/' |'+' |'-') ')' ID+
               
| 'foldr'   '(' ('*' |'/' |'+' |'-') ')' ID+
               
| hr  op=('*'| '/') hr
               
| hr  op=('+'| '-') hr
               
| '(' hr ')'
               
| ID '(' ID* ')'
               
;



Thank you,


Hope this helps....
   -jbb

ahmed...@gmail.com

unread,
May 17, 2015, 2:18:41 PM5/17/15
to antlr-di...@googlegroups.com
Thank you John for your help
Would you please give me some guide rules regarding a semantic analysis phase for the compiler. Because I know nothing about it, I just started today reading the book ANTLR4 and still at the end of chapter 4. 

Thank you,
Regards,
Ahmed
--
You received this message because you are subscribed to a topic in the Google Groups "antlr-discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/antlr-discussion/PpdEcOBIlqQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to antlr-discussi...@googlegroups.com.

Terence Parr

unread,
May 17, 2015, 2:27:54 PM5/17/15
to antlr-di...@googlegroups.com
The language implementation patterns book talks about what goes where :)

Sent from my iPhone
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.

ahmed...@gmail.com

unread,
May 17, 2015, 2:45:34 PM5/17/15
to antlr-di...@googlegroups.com
Yes, I found implementation pattern in chapter 5. I finish 4 and go immediately 5. 
thanks a lot 

aabb

unread,
May 18, 2015, 3:53:35 PM5/18/15
to antlr-di...@googlegroups.com
Many thanks for taking your precious time answering my question and for producing this amazing tool. I found the answer but not sure if is correct. I extended the BaseListener then overridden couple of methods and inside the methods I did multiple if statements if false throw run time exception with explaining the problem. and it worked for me very well. However I am not sure if this way a semantic analysis should be implemented.



On Sunday, May 17, 2015 at 9:45:34 PM UTC+3, aabb wrote:

To unsubscribe from this group and all its topics, send an email to antlr-discussion+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
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.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "antlr-discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/antlr-discussion/PpdEcOBIlqQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to antlr-discussion+unsubscribe@googlegroups.com.

Jim Idle

unread,
May 18, 2015, 10:31:26 PM5/18/15
to antlr-di...@googlegroups.com
You will find it a lot easier to label the elements of your rules, then you can just use things like ctx.myname.getText() etc.

To unsubscribe from this group and all its topics, send an email to antlr-discussi...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
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.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "antlr-discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/antlr-discussion/PpdEcOBIlqQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to antlr-discussi...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
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.

Nilo Roberto da Cruz Paim

unread,
May 19, 2015, 8:21:20 AM5/19/15
to antlr-di...@googlegroups.com

Hi All,

I’m working on a C# Project using Antlr 4 and StringTemplate 4. The Antlr 4 stuff is working like a charm, but my question is related to the StringTemplate stuff. Sorry if I’m posting on the wrong list…

I want to put several template files on a directory named "Templates", relative to the executable of my application, and use them. One template file, for instance, is named "CGlobals.stg".

That way, I created a TemplateGroupDirectory and loaded the template:

var group = new TemplateGroupDirectory("Templates");

var tmpl = group.GetInstanceOf("CGlobals");

 

On trying to get the instance of the template I've got a message saying that occurs a NullReferenceException.

The CGlobals.stg contains:

// Global definitions

CGlobals(date,name,device) ::=

<< 

       // Some code here…

>> 

 

What am I missing?

TIA,

Nilo Paim

Terence Parr

unread,
May 19, 2015, 10:52:05 AM5/19/15
to antlr-di...@googlegroups.com
the location of the NullReferenceException ;)  likely it cant find your Templates dir.
Ter

Nilo Roberto da Cruz Paim

unread,
May 19, 2015, 11:20:15 AM5/19/15
to antlr-di...@googlegroups.com

Thanks the prompt answer, Ter.

 

I changed the original code, supplying the complete path to the directory.

 

Anyway, same error… stack trace follows…

 

   em Antlr4.StringTemplate.TemplateGroup.LoadTemplateFile(String prefix, String unqualifiedFileName, ICharStream templateStream) na c:\dev\stringtemplate_main\antlrcs\main\Antlr4.StringTemplate\TemplateGroup.cs:linha 974

   em Antlr4.StringTemplate.TemplateGroupDirectory.LoadTemplateFile(String prefix, String unqualifiedFileName) na c:\dev\stringtemplate_main\antlrcs\main\Antlr4.StringTemplate\TemplateGroupDirectory.cs:linha 239

   em Antlr4.StringTemplate.TemplateGroupDirectory.Load(String name) na c:\dev\stringtemplate_main\antlrcs\main\Antlr4.StringTemplate\TemplateGroupDirectory.cs:linha 168

   em Antlr4.StringTemplate.TemplateGroup.LookupTemplate(String name) na c:\dev\stringtemplate_main\antlrcs\main\Antlr4.StringTemplate\TemplateGroup.cs:linha 396

   em Antlr4.StringTemplate.TemplateGroup.GetInstanceOf(String name) na c:\dev\stringtemplate_main\antlrcs\main\Antlr4.StringTemplate\TemplateGroup.cs:linha 312

   em DDLCompiler.DDLCompiler.DoCompilation(AntlrInputStream input) na D:\CTS\DDLCompiler\DDLCompiler\DDLCompiler.cs:linha 71

   em DDLCompiler.DDLCompiler.Compile(RichTextBox txtbox) na D:\CTS\DDLCompiler\DDLCompiler\DDLCompiler.cs:linha 36

   em DDLStudio.Form1.button1_Click(Object sender, EventArgs e) na D:\CTS\DDLCompiler\DDLStudio\Form1.cs:linha 22

   em System.Windows.Forms.Control.OnClick(EventArgs e)

   em System.Windows.Forms.Button.OnClick(EventArgs e)

   em System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

   em System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)

   em System.Windows.Forms.Control.WndProc(Message& m)

   em System.Windows.Forms.ButtonBase.WndProc(Message& m)

   em System.Windows.Forms.Button.WndProc(Message& m)

   em System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)

   em System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

   em System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

   em System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)

   em System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)

   em System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)

   em System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)

   em System.Windows.Forms.Application.Run(Form mainForm)

   em DDLStudio.Program.Main() na D:\CTS\DDLCompiler\DDLStudio\Program.cs:linha 18

   em System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)

   em System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)

   em Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

   em System.Threading.ThreadHelper.ThreadStart_Context(Object state)

   em System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)

   em System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)

   em System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

   em System.Threading.ThreadHelper.ThreadStart()

 

 

TIA,

Nilo

 

P.S.: I’m a big fan of Antlr since version 2. Congrats on this great piece of software. Thanks to Sam and others too.


No virus found in this message.
Checked by AVG - www.avg.com
Version: 2015.0.5941 / Virus Database: 4342/9816 - Release Date: 05/19/15

Terence Parr

unread,
May 19, 2015, 2:31:56 PM5/19/15
to antlr-di...@googlegroups.com
hiya. i don’t have a C# mechanism handy. can you pass me the source code at that line?
Ter

Nilo Roberto da Cruz Paim

unread,
May 19, 2015, 4:26:16 PM5/19/15
to antlr-di...@googlegroups.com
Hi, Ter.

Sure I can...

TemplateGroup group = new TemplateGroupDirectory(@"D:\CTS\DDLCompiler\DDLCompiler\bin\Debug\Templates");

MessageBox.Show(group.ToString());

var f = group.GetInstanceOf("CGlobals");

f.Add("name", "Nilo Roberto C Paim");
f.Add("date", DateTime.Now.ToString("D"));
f.Add("device", visitor.device);

MessageBox.Show(f.Render());

The line 71 is the line containing the GetInstanceOf.

"group.ToString()" shows me "D:\CTS\DDLCompiler\DDLCompiler\bin\Debug\Templates".

If needed, I can send you the whole project. It's on an initial state, and I'll expand it in several points, including the grammar and visitor.

TIA,
Nilo


-----Mensagem original-----
De: antlr-di...@googlegroups.com [mailto:antlr-di...@googlegroups.com] Em nome de Terence Parr
Enviada em: terça-feira, 19 de maio de 2015 15:32
Para: antlr-di...@googlegroups.com
Assunto: Re: RES: [antlr-discussion] C# StringTemplate throwing NullReferenceException
-----

Terence Parr

unread,
May 20, 2015, 2:16:13 PM5/20/15
to antlr-di...@googlegroups.com
hi. ok, so inside group.GetInstanceOf it gets a null pointer exception in the load? Hmm… I was hoping it was something obvious from the outside but it’s not, assuming you have verified that that absolute path is not only present but visible according to permissions. I don’t have the C# I don’t think; Sam probably has that in one of his repositories.

Ter

Nilo Roberto da Cruz Paim

unread,
May 20, 2015, 3:19:00 PM5/20/15
to antlr-di...@googlegroups.com
Thanks, Ter.

I've verified, and the absolute path is present and visible actually.

And when I use a StreamReader to get the template's contents into a string and create a TemplateGroupString using this string, everything works. I think this can be a solution.

Anyway, I'll make more tests, improve something else, and maybe Sam can help me later.

Thanks again, Ter.

Nilo
From Brazil.

-----Mensagem original-----
De: antlr-di...@googlegroups.com [mailto:antlr-di...@googlegroups.com] Em nome de Terence Parr
Enviada em: quarta-feira, 20 de maio de 2015 15:16
Para: antlr-di...@googlegroups.com
Assunto: Re: RES: RES: [antlr-discussion] C# StringTemplate throwing NullReferenceException
Version: 2015.0.5941 / Virus Database: 4347/9816 - Release Date: 05/19/15

Sam Harwell

unread,
May 22, 2015, 3:55:51 AM5/22/15
to antlr-di...@googlegroups.com
Can you post the issue to the issue tracker? This repo is the C# ports of ANTLR 3, ST3, and ST4:
https://github.com/antlr/antlrcs/issues

Thanks,
Sam

Nilo Roberto da Cruz Paim

unread,
May 22, 2015, 7:44:05 AM5/22/15
to antlr-di...@googlegroups.com
Sam,

Posted!

Thanks for this nice piece of software.

Best regards,
Nilo

-----Mensagem original-----
De: antlr-di...@googlegroups.com [mailto:antlr-di...@googlegroups.com] Em nome de Sam Harwell
Enviada em: sexta-feira, 22 de maio de 2015 04:56
Para: antlr-di...@googlegroups.com
Assunto: RE: RES: RES: [antlr-discussion] C# StringTemplate throwing NullReferenceException
Version: 2015.0.5941 / Virus Database: 4347/9832 - Release Date: 05/21/15

Reply all
Reply to author
Forward
0 new messages